EF4 code第一:如何添加关系不添加导航属性 [英] EF4 Code First: how to add a relationship without adding a navigation property
问题描述
我应该如何定义使用code第一,但不使用任何导航属性的关系?
How should I define relationships using Code First but without using any navigation properties?
previously我已经定义了一对多和多对多的在关系的两端使用导航属性。和相应的关系将在数据库中创建。这里的课程看起来怎么样(我已经转换了多对多的关系,一对多的简单)。
Previously I have defined One-Many and Many-Many by using navigation properties in both ends of the relationship. And the appropriate relationships are created in the database. here's a stripped down version of how the classes look like (I've converted the Many-Many relationships to one-many for simplicity).
public class User
{
public string UserId { get; set; }
public string PasswordHash { get; set; }
public bool IsDisabled { get; set; }
public DateTime AccessExpiryDate { get; set; }
public bool MustChangePassword { get; set; }
public virtual Role Role { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Right> Rights { get; set; }
}
public class Right
{
public Guid RightId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual Role Role { get; set; }
}
不过,如果我删除了导航属性,正在创建无关系。这里的类会是什么样子。
However, if I remove the navigation properties, no relationships are being created. Here's how the classes would look like.
public class User
{
public string UserId { get; set; }
public string PasswordHash { get; set; }
public bool IsDisabled { get; set; }
public DateTime AccessExpiryDate { get; set; }
public bool MustChangePassword { get; set; }
public int Role RoleId { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Right
{
public Guid RightId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int RoleId { get; set; }
}
通知,而不是一个导航属性,我有相关的表的主键。一切都被在表上创建 - 除了关系。所以,我怎么这样做?
notice that instead of a navigation property, I have the primary key of the related table. Everything gets created on the table - except the relationship. So how do I get about doing this?
顺便说一句,我在的DbContext的OnModelCreating方法,但都无济于事尝试各种组合。任何帮助深表AP preciated!
BTW, I've tried various combinations in the OnModelCreating method of the dbcontext but to no avail. Any help is much appreciated!
谢谢,
梅尔
推荐答案
我相信你使用$ C $当c一总是需要在至少一方导航属性。然后,你就能够映射它:
I believe you always need navigation property on at least one side when using code-first. Then you will be able to map it:
public class User
{
public string UserId { get; set; }
public string PasswordHash { get; set; }
public bool IsDisabled { get; set; }
public DateTime AccessExpiryDate { get; set; }
public bool MustChangePassword { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class Right
{
public Guid RightId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public int RoleId { get; set; }
public Role Role { get; set; }
}
public class TestContext : DbContext
{
public TestContext() : base("Entities")
{}
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<User>()
.HasRequired(r => r.Role)
.WithMany()
.HasForeignKey(r => r.RoleId);
modelBuilder.Entity<Right>()
.HasRequired(r => r.Role)
.WithMany()
.HasForeignKey(r => r.RoleId);
}
}
这篇关于EF4 code第一:如何添加关系不添加导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!