简体   繁体   English

EF 4 Code First - 我该如何建模?

[英]EF 4 Code First - How do I model this?

I am trying out EF and Code First approach我正在尝试 EF 和 Code First 方法

I have an Account model which looks like我有一个看起来像的帐户模型

public class Account
{
   public virtual int AccountID {get; set;}
   public virtual string AccountName {get; set;}
   public virtual Account ParentAccount {get; set;}
   public virtual Contact AccountOwner {get; set}
}

Contact class is a simple class联系类是一个简单的类

public class Contact
{
   public virtual int ContactID {get; set;}
   public virtual string ContactName {get; set;}
}

Now I have no idea how to add IDs to the account class.现在我不知道如何将 ID 添加到帐户类。

Suppose I have set up the context correctly, what modifications are needed to the Account class so that假设我已经正确设置了上下文,需要对 Account 类进行哪些修改,以便

  1. I can add an Account as the parent of another Account我可以添加一个帐户作为另一个帐户的父级

  2. I can access the ParentAccount of a particular Account我可以访问特定帐户的 ParentAccount

  3. I can access the AccountOwner of a particular Account我可以访问特定帐户的 AccountOwner

I am new to this.我是新来的。 Any help would be appreciated任何帮助,将不胜感激

Here's how I would solve this.这是我将如何解决这个问题。

Add foreign key properties to your classes.将外键属性添加到您的类。 I'm talking about ParentAccountId and AccountOwnerId .我说的是ParentAccountIdAccountOwnerId To make navigating things a bit easier, I have also added a collection of child accounts to Account and a collection of owned accounts to Contact.为了使导航更容易一些,我还向 Account 添加了一组子帐户,向 Contact 添加了一组自有帐户。 Note that it's not necessary to make "normal" properties virtual.请注意,没有必要将“正常”属性设为虚拟。 Only navigational properties should be made virtual.只有导航属性应该是虚拟的。

public class Account
{
    public int AccountID {get; set;}
    public string AccountName {get; set;}
    public int? ParentAccountId { get; set; }
    public int? AccountOwnerId { get; set; }

    public virtual Account ParentAccount { get; set; }
    public virtual ICollection<Account> ChildAccounts { get; set; }

    public virtual Contact AccountOwner { get; set; }

    public Account()
    {
        ChildAccounts = new List<Account>();
    }
}

public class Contact
{
    public int ContactID { get; set; }
    public string ContactName { get; set; }

    public virtual ICollection<Account> OwnedAccounts { get; set; }

    public Contact()
    {
        OwnedAccounts = new List<Account>();
    }
}

Next, create a mapping for the Account class to explain to EF how to setup the relationships.接下来,为 Account 类创建一个映射,以向 EF 解释如何设置关系。

public class AccountMapping : EntityTypeConfiguration<Account>
{
    public AccountMapping()
    {
        HasOptional(x => x.ParentAccount).WithMany(x => x.ChildAccounts).HasForeignKey(x => x.ParentAccountId);
        HasOptional(x => x.AccountOwner).WithMany(x => x.OwnedAccounts).HasForeignKey(x => x.AccountOwnerId);
    }
}

Finally, add the mapping to your DbContext class.最后,将映射添加到 DbContext 类。

public MyContext : DbContext
{
    public DbSet<Account> Accounts { get; set; }
    public DbSet<Contact> Contacts { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new AccountMapping());
    }
}

Note that I have assumed that the AccountOwner and ParentAccount are optional.请注意,我假设 AccountOwner 和 ParentAccount 是可选的。 If they are required, simply change the type of the foreign properties from int?如果需要,只需将外部属性的类型从int? to int and change the HasOptional in the mappings to HasRequired .int和更改HasOptional在映射HasRequired

public class Account
{
   [Key]
   public virtual int AccountID {get; set;}
   public virtual string AccountName {get; set;}
   public virtual Account ParentAccount {get; set;}
   public virtual Contact AccountOwner {get; set}
   public virtual IEnummerable<Account> ChiledAccount {get; set}
}

public class Contact
{
   [Key]
   public virtual int ContactID {get; set;}
   public virtual string ContactName {get; set;}
}

You can do this by without using Fluent API.That is by default conventions are maintained by EF.B'cos your mappings are simple.您可以在不使用 Fluent API 的情况下做到这一点。默认情况下,EF.B 维护约定,因为您的映射很简单。

You're having Account (1) : Contact (m) Relationship.您拥有帐户 (1) :联系人 (m)关系。

So try with below models所以尝试使用以下模型

public class Account
{
   public int Id {get; set;}
   public string AccountName {get; set;}
      
   public virtual ICollection<Contact> AccountOwners { get; set; }
}


public class Contact
{
   public int Id {get; set;}
   public string ContactName {get; set;}

   public virtual Account ParentAccount {get; set;}
}

Then Your database tables will be created like below:然后您的数据库表将被创建如下:

Accounts Table

Id            int             NotNull
AccountName   nvarchar(100)   AllowNull

   
Contacts Table

Id            int             NotNull
ContactName   nvarchar(100)   AllowNull
Account_Id    int             NotNull

If you need to do advance mappings then you have to learn Fluent API.如果您需要进行高级映射,那么您必须学习 Fluent API。

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

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