简体   繁体   中英

EF6 1:optional + 1:many

got a little Problem with EF6 Code First (in a MVC Web App).

Enum to classify an Account in a "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

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)

I tried hundred of modelBuilder methods to give the EF6 the necessary infos, but no success. Has someone a hint, to give the DbModelBuilder in the "protected override void OnModelCreating" method the correct relations data?

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. 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. You could specify them, but then you'd have to use fluent API to map those properties as foreign keys for specific tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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