0
public class Admin : EntityTypeConfiguration<Admin>
{
    //[ForeignKey("Blog")] -- If I enable this, it compiles
    public int AdminId { get; set; }
    public string AdminName { get; set; }
    public string AdminPicture { get; set; }

    //[Required] -- Or If I enable this, it compiles
    public virtual Blog Blog { get; set; }
}

public class Blog : EntityTypeConfiguration<Blog>
{
    public int BlogId { get; set; }
    public string BlogName { get; set; }
    public string BlogUrl { get; set; }

    public virtual Admin Admin { get; set; }

    public Blog()
    {
        HasRequired(a => a.Admin).WithRequiredPrincipal(b=>b.Blog);
    }
}

As long as I am defining HasRequired and WithRequiredPrincipal keys, why VS still creates below error.

Unable to determine the principal end of an association between the types 'Dummy.Models.Blog' and 'Dummy.Models.Admin'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

Second thing is, even I enable [Required] or [ForeingKey] attr., in edmx designer, I only see 1 - 0..1 But I must see 1 - 1 (both end required)

Ozgur Erdogan
  • 793
  • 1
  • 6
  • 9
  • Sorry, I meant; As long as I am defining HasRequired and WithRequiredPrincipal keys, it compiles fines otwerwise VS still creates below error. – Ozgur Erdogan Feb 05 '16 at 22:59

1 Answers1

1

1-1 relationship is not possible at database level, because you can't insert two rows at the same time. 1-1 is only possible at class validation level.

To make a 1-1 relationship, the primary key of the dependant entity must be the foreign key of the principal entity; that's the only way to make a 1-1 relationship. So, you have to make the following changes (considering that you are using EF Code First):

public class Admin
{
    public int AdminId { get; set; }
    public string AdminName { get; set; }
    public string AdminPicture { get; set; }

    public virtual Blog Blog { get; set; }
}

Blog should not have its own BlogId, because a blog belongs to an admin, and the admin can have only one blog (1-1 relationship). If you create a BlogId, with an AdminId FK, you would be making a 1-n relationship. Furthermore, do not mix the entity class with the mapping class, they should be different things. See the example below:

public class Blog
{
    public int AdminId { get; set; } //PK AND FK
    public string BlogName { get; set; }
    public string BlogUrl { get; set; }

    public virtual Admin Admin { get; set; }         
}

Creating the relationship with a mapping class:

public class BlogMapping : EntityTypeConfiguration<Blog>
{
    public BlogMapping()
    {
        HasKey(i => i.AdminId);

        HasRequired(a => a.Admin)
            .WithRequiredDependent(i => i.Blog);
    }
}

Register the mapping inside the dbContext class:

public class MyDbContext : DbContext
{
    public DbSet<Admin> Admins { get; set; }
    public DbSet<Blog> Blogs { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new BlogMapping());
    }
}

This will generate the following migration:

CreateTable(
    "dbo.Admins",
    c => new
        {
            AdminId = c.Int(nullable: false, identity: true),
            AdminName = c.String(),
            AdminPicture = c.String(),
        })
    .PrimaryKey(t => t.AdminId);

CreateTable(
    "dbo.Blogs",
    c => new
        {
            AdminId = c.Int(nullable: false),
            BlogName = c.String(),
            BlogUrl = c.String(),
        })
    .PrimaryKey(t => t.AdminId)
    .ForeignKey("dbo.Admins", t => t.AdminId)
    .Index(t => t.AdminId);

Hope this helps!

Fabio Luz
  • 11,534
  • 1
  • 22
  • 40
  • Thanks a lot. 1st, why do I see, in edmx designer 1 .. 0.1 instead 1 .. 1 relation in between two tables? Because it is not possible at database level? 2nd, I am mixing entity class with the mapping class to be able to see them at once. Would that cause issues? – Ozgur Erdogan Feb 06 '16 at 12:21
  • Why do you have an edmx designer? Are you using database-first or code-first? See this thread for helping http://stackoverflow.com/questions/5446316/code-first-vs-model-database-first. And yes, you shouldn't mix entity class with mapping class. Otherwise, you won't be able to use them, you'll get an error – Fabio Luz Feb 06 '16 at 15:40
  • I am using code first, to see relations on diagram, after db is created, I am creating edmx with https://github.com/ErikEJ/SqlCeToolbox Maybe it is mistranlating relation not sure.. I do not get any error when mixing them. You can try my codes to see the results. – Ozgur Erdogan Feb 06 '16 at 17:34
  • I just noted something, as long as you define key prop. with HasKey on Blogs it does not matter if it is BlogId or AdminId right? – Ozgur Erdogan Feb 07 '16 at 18:57
  • So can I correct what you wrote above: "Blog should not have its own BlogId, because a blog belongs to an admin, and the admin can have only one blog (1-1 relationship). If you create a BlogId, with an AdminId FK, you would be making a 1-n relationship." – Ozgur Erdogan Feb 08 '16 at 19:07