Unidirectional One-To-One relationship in Entity Framework

前端 未结 2 1651
眼角桃花
眼角桃花 2020-12-05 22:06

Below example of one-to-one relationship throws an exception

public class User
{
    public int Id { get; set; }
    public Address Address { get; set; }
}

         


        
相关标签:
2条回答
  • 2020-12-05 22:53

    While the answer provided by Eranga is correct and creates a shared primary key association between User and Address, you might not want to use it due to the limitations this mapping type has.

    Here is another way of creating a 1:1 association which is called one-to-one foreign key association:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Address>()
                    .HasRequired(a => a.User)
                    .WithOptional(u => u.Address)
                    .Map(m => m.MapKey("UserId"));
    }
    

    EF Code First recognizes this as a 1:1 association hence allowing you to have a bidirectional association between User and Address.

    Now all you need to do is to define a Unique Key constraint on the UserId column to make your relationship a true one to one on your database side. One way for doing so is using a Seed method that has been overridden in a custom initializer class:

    class DbInitializer : DropCreateDatabaseAlways<Context>
    {
        protected override void Seed(Context context)
        {
            context.Database.ExecuteSqlCommand("ALTER TABLE Addresses ADD CONSTRAINT uc_User UNIQUE(UserId)");
        }
    }
    


    The above code will result in the following schema:

    enter image description here

    0 讨论(0)
  • 2020-12-05 23:02

    You need to use fluent API to map the relationship as shared primary key.

    public class MyContext : DbContext
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
             modelBuilder.Entity<Address>()
                 .HasRequired(a => a.User)
                 .WithOptional(u => u.Address);
        }
    }
    
    0 讨论(0)
提交回复
热议问题