Entity Framework one-to-zero-or-one foreign key association

橙三吉。 提交于 2019-12-08 00:49:35

问题


I am in the process of changing the back-end of an existing application to use Entity Framework Code First. I've used the built-in tool in Visual Studio 2015 to generate POCO classes based on my existing database. This worked perfectly for the most part, except for two classes, with a one-to-zero-or-one relationship. These are my (simplified) classes:

public class Login
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public int TeamMemberId { get; set; }
    public virtual TeamMember TeamMember { get; set; }
}

public class TeamMember
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public virtual Login Login { get; set; }
}

With the following configuration:

public class LoginTypeConfiguration : EntityTypeConfiguration<Login>
{
    public LoginTypeConfiguration()
    {
        this.HasRequired(e => e.TeamMember)
            .WithOptional(e => e.Login);

        this.Property(e => e.TeamMemberId)
            .HasColumnName("TeamMember_Id");
    }
}

This results in the following migration:

CreateTable(
    "dbo.Logins",
    c => new
        {
            Id = c.Int(nullable: false, identity: true),
            TeamMember_Id = c.Int(nullable: false),
        })
    .PrimaryKey(t => t.Id)
    .ForeignKey("dbo.TeamMembers", t => t.Id)
    .Index(t => t.Id);

For some reason EF creates a foreign key on [Logins].[Id] instead of [Logins].[TeamMember_Id]. I've already tried decorating my navigation property with a ForeignKey attribute, but this did not work. Is there a way to get it to create a foreign key on [Logins].[TeamMember_Id] instead?


回答1:


I ended up creating one-to-many relationship, with a [NotMapped] property for Login.

My classes:

public class Login
{
    public int TeamMemberId { get; set; }
    public virtual TeamMember TeamMember { get; set; }
}

public class TeamMember
{
    [NotMapped]
    public virtual Login Login
    {
        get { return Logins.FirstOrDefault(); }
    }

    public virtual ICollection<Login> Logins { get; set; }
}

With the following configuration:

public class LoginTypeConfiguration : EntityTypeConfiguration<Login>
{
    public LoginTypeConfiguration()
    {
        this.Property(e => e.TeamMemberId)
            .HasColumnName("TeamMember_Id");
    }
}



回答2:


For that you can write your code first like this. Entity Framework will generate foreign key TeamMemberId automatically for you. For more information: Entity Framework Tutorial - Code First Conventions

public class Login
{
   public int Id { get; set; } //foreign key declaration
   public int TeamMemberId { get; set; }
   public  TeamMember TeamMember { get; set; }
}

public class TeamMember
{
   public int TeamMemberId { get; set; }
   public Ilist<Login> Login { get; set; }
}


来源:https://stackoverflow.com/questions/38868054/entity-framework-one-to-zero-or-one-foreign-key-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!