[英]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.