简体   繁体   English

EF6 1:可选+ 1:许多

[英]EF6 1:optional + 1:many

got a little Problem with EF6 Code First (in a MVC Web App). EF6 Code First(在MVC Web应用程序中)遇到了一些问题。

Enum to classify an Account in a "AccountCircle": 枚举将“ AccountCircle”中的帐户分类:

public enum AccountType
    {
        Circle1,
        Circle2,
        Circle3,
        Circle4,
        Circle5
    }

Main class for Accounts: 帐户主类:

[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; }
}

The main Company-Model 主要公司型号

[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; }
}

Class for a single circle: 单圈课:

[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; }
}

and last but not least the account itself with an additinal information what type of member: 最后但并非最不重要的一点是,具有其他信息的帐户本身是什么类型的成员:

[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; }
}

And the DbContext 还有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
}

So there is a Company, which has an optional property of type "AccountCircle" (1:optional) In the Accountcircle, there is a List of Accounts with a separate enum (AccountCirleMemberModel 1:many) 因此,有一家公司,其属性类型为“ AccountCircle”(1:可选)。在Accountcircle中,有一个带有单独枚举的帐户列表(AccountCirleMemberModel 1:许多)

I tried hundred of modelBuilder methods to give the EF6 the necessary infos, but no success. 我尝试了数百种modelBuilder方法来为EF6提供必要的信息,但没有成功。 Has someone a hint, to give the DbModelBuilder in the "protected override void OnModelCreating" method the correct relations data? 有没有人暗示要给“受保护的覆盖无效OnModelCreating”方法中的DbModelBuilder提供正确的关系数据?

Big thanks in advance! 预先感谢! monte 蒙特

Not sure if this answers your question, but if you want to specify the relationships between tables in the DB, using EF Code First, you have to use modifier virtual for your "navigation" properties - those that map to another table. 不知道这是否能回答您的问题,但是如果您想使用EF Code First在数据库中指定表之间的关系,则必须将修饰符virtual用于“导航”属性-那些映射到另一个表的属性。 So the code would look like: 因此,代码如下所示:

[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; }
}

You don't need to add properties that would serve as foreign keys - EF would take care of that. 您不需要添加可用作外键的属性-EF会解决这一问题。 You could specify them, but then you'd have to use fluent API to map those properties as foreign keys for specific tables. 您可以指定它们,但随后必须使用fluent API将这些属性映射为特定表的外键。

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

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