繁体   English   中英

EF6 1:可选+ 1:许多

[英]EF6 1:optional + 1:many

EF6 Code First(在MVC Web应用程序中)遇到了一些问题。

枚举将“ 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:许多)

我尝试了数百种modelBuilder方法来为EF6提供必要的信息,但没有成功。 有没有人暗示要给“受保护的覆盖无效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; }
}

您不需要添加可用作外键的属性-EF会解决这一问题。 您可以指定它们,但随后必须使用fluent API将这些属性映射为特定表的外键。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM