简体   繁体   English

实体框架一对零或一个外键关联

[英]Entity Framework one-to-zero-or-one foreign key association

I am in the process of changing the back-end of an existing application to use Entity Framework Code First. 我正在更改现有应用程序的后端以使用Entity Framework Code First。 I've used the built-in tool in Visual Studio 2015 to generate POCO classes based on my existing database. 我已使用Visual Studio 2015中的内置工具基于现有数据库生成POCO类。 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] . 由于某些原因,EF在[Logins].[Id]上创建了一个外键,而不是在[Logins].[TeamMember_Id]上创建了一个外键。 I've already tried decorating my navigation property with a ForeignKey attribute, but this did not work. 我已经尝试过用ForeignKey属性装饰导航属性,但这没有用。 Is there a way to get it to create a foreign key on [Logins].[TeamMember_Id] instead? 有没有办法让它在[Logins].[TeamMember_Id]上创建外键?

For that you can write your code first like this. 为此,您可以像这样首先编写代码。 Entity Framework will generate foreign key TeamMemberId automatically for you. 实体框架将自动为您生成外键TeamMemberId 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; }
}

I ended up creating one-to-many relationship, with a [NotMapped] property for Login. 我最终使用[NotMapped]属性创建了一对多关系。

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");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有许多一对零或一对一关系的实体框架 - Entity framework with many one-to-zero-or-one relationships Entity Framework Core 一对一或零(共享主键关联)外键冲突插入列表 - Entity Framework Core Insert list of one-to-one-or-zero (shared primary key association) foreign key conflict 实体框架(EF)代码优先级联删除一对一或零关系 - Entity Framework (EF) Code First Cascade Delete for One-to-Zero-or-One relationship 当两个实体都可以独立存在时,Entity Framework核心为一对零或一对 - Entity Framework core one-to-zero-or-one when both entities can exist stand alone 如何与Entity Framework建立一对一或零关系,并在子实体上公开外键? - How do I setup a one-to-one-or-zero relationship with Entity Framework AND expose the foreign key on the child entity? 使用实体框架一对一的外键 - Foreign key one to one with Entity Framework 如何在Entity Framework中使用流利的API为一对一..one关系指定外键属性 - How to specify foreign key property for one to zero..one relation using fluent api in Entity Framework 使用EF7映射一对一或一 - Mapping One-to-Zero-or-One with EF7 实体框架一个表中有两个外键 - Entity Framework Two foreign key in one table 如何配置这种一对零或一的关系? - How to configure this one-to-zero-or-one relationship?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM