EF6 1:可选+ 1:很多
EF6 Code First(在MVC Web App中)遇到了一些问题。
枚举在“AccountCircle”中对帐户进行分类:
public enum AccountType
{
Circle1,
Circle2,
Circle3,
Circle4,
Circle5
}
账户主类:
[Table("Accounts")]
public class AccountModel
{
public AccountModel()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string Name { get; set; }
public string EMail { get; set; }
}
主要的公司模式
[Table("Companys")]
public class CompanyModel
{
public CompanyModel()
{
this.AccountCircle = new AccountCircleModel();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public string Name { get; set; }
public int? idAccountCircle { get; set; }
public AccountCircleModel AccountCircle { get; set; }
}
单圈的类别:
[Table("AccountCircles")]
public class AccountCircleModel
{
public AccountCircleModel()
{
this.Member = new List<AccountCirleMemberModel>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public int idCompany { get; set; }
public CompanyModel Company { get; set; }
public List<AccountCirleMemberModel> Member { get; set; }
}
最后但并非最不重要的是账户本身带有附加信息什么类型的成员:
[Table("AccountCircleMember")]
public class AccountCirleMemberModel
{
public AccountCirleMemberModel()
{
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int id { get; set; }
public AccountType Typ { get; set; }
public int idAccount { get; set; }
public virtual AccountModel Account { get; set; }
public int idAccountCircle { get; set; }
public AccountCircleModel AccountCircle { get; set; }
}
和DbContext
public class TestContext : DbContext
{
public TestContext()
: base()
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// modelBuilder Infos.....
base.OnModelCreating(modelBuilder);
}
#region Tables
public DbSet<AccountModel> Accounts { get; set; }
public DbSet<CompanyModel> Companys { get; set; }
public DbSet<AccountCircleModel> AccountCircles { get; set; }
#endregion
}
所以有一个公司,它有一个类型为“AccountCircle”的可选属性(1:可选)在Accountcircle中,有一个单独的枚举帐户列表(AccountCirleMemberModel 1:many)
我尝试了百种modelBuilder方法,为EF6提供必要的信息,但没有成功。 有人提示,在“protected override void OnModelCreating”方法中给予DbModelBuilder正确的关系数据?
非常感谢提前! 蒙特
不知道这是否回答你的问题,但如果你想指定数据库中的表之间的关系,使用EF Code First,你必须对你的“导航”属性使用修饰符virtual
- 映射到另一个表的属性。 所以代码如下所示:
[Table("Companys")]
public class CompanyModel
{
// other properties and the rest of the code here
public virtual AccountCircleModel AccountCircle { get; set; }
}
[Table("AccountCircles")]
public class AccountCircleModel
{
// other properties and the rest of the code here
public virtual CompanyModel Company { get; set; }
public virtual ICollection<AccountCirleMemberModel> Member { get; set; }
}
[Table("AccountCircleMember")]
public class AccountCirleMemberModel
{
// other properties and the rest of the code here
public virtual AccountModel Account { get; set; }
public virtual AccountCircleModel AccountCircle { get; set; }
}
您不需要添加可用作外键的属性--FEF会照顾到这一点。 您可以指定它们,但是您必须使用流畅API将这些属性映射为特定表的外键。
链接地址: http://www.djcxy.com/p/49689.html下一篇: Model backing a DB Context has changed; Consider Code First Migrations