Unidirectional One-To-One relationship in Entity Framework

前端 未结 2 1662
眼角桃花
眼角桃花 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 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
    () .HasRequired(a => a.User) .WithOptional(u => u.Address); } }

提交回复
热议问题