Entity framework EF 4代码优先-如何对此建模?

Entity framework EF 4代码优先-如何对此建模?,entity-framework,entity-framework-4,ef-code-first,Entity Framework,Entity Framework 4,Ef Code First,我正在尝试EF和代码优先的方法 我有一个帐户模型,看起来像 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} }

我正在尝试EF和代码优先的方法

我有一个帐户模型,看起来像

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类是一个简单的类

public class Contact
{
   public virtual int ContactID {get; set;}
   public virtual string ContactName {get; set;}
}
现在我不知道如何向account类添加ID

假设我已正确设置了上下文,那么需要对Account类进行哪些修改,以便

  • 我可以添加一个帐户作为另一个帐户的父帐户

  • 我可以访问特定帐户的父帐户

  • 我可以访问特定帐户的帐户所有者

  • 我是新来的。如有任何帮助,将不胜感激
    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;}
    }
    
    { [关键] 公共虚拟int AccountID{get;set;} 公共虚拟字符串AccountName{get;set;} 公共虚拟帐户父帐户{get;set;} 公共虚拟联系人帐户所有者{get;set} 公共虚拟IEnumerable ChiledAccount{get;set} } 公共类联系人 { [关键] 公共虚拟int联系人ID{get;set;} 公共虚拟字符串ContactName{get;set;} }
    公共类账户
    {
    [关键]
    公共虚拟int AccountID{get;set;}
    公共虚拟字符串AccountName{get;set;}
    公共虚拟帐户父帐户{get;set;}
    公共虚拟联系人帐户所有者{get;set}
    公共虚拟IEnumerable ChiledAccount{get;set}
    }
    公共类联系人
    {
    [关键]
    公共虚拟int联系人ID{get;set;}
    公共虚拟字符串ContactName{get;set;}
    }
    
    您可以通过不使用Fluent API来实现这一点。也就是说,默认约定由EF维护。因为您的映射很简单

    您拥有帐户(1):联系人(m)关系

    因此,请尝试以下模型

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

    如果您需要进行高级映射,那么您必须学习Fluent API。

    您可以通过不使用Fluent API来实现这一点。也就是说,默认约定由EF.B'维护,因为您的映射很简单

    您拥有帐户(1):联系人(m)关系

    因此,请尝试以下模型

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

    如果您需要进行高级映射,那么您必须学习Fluent API。

    以下是我将如何解决此问题的方法

    将外键属性添加到类中。我说的是
    ParentAccountId
    AccountOwnerId
    。为了使导航更容易一些,我还向Account添加了一组子帐户,并向Contact添加了一组自有帐户。请注意,没有必要将“正常”属性设置为虚拟。只有导航属性应设置为虚拟

    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>();
        }
    }
    
    公共类帐户
    {
    public int AccountID{get;set;}
    公共字符串AccountName{get;set;}
    public int?ParentAccountId{get;set;}
    public int?AccountOwnerId{get;set;}
    公共虚拟帐户父帐户{get;set;}
    公共虚拟ICollection子帐户{get;set;}
    公共虚拟联系人帐户所有者{get;set;}
    公共帐户()
    {
    ChildAccounts=新列表();
    }
    }
    公共类联系人
    {
    public int ContactID{get;set;}
    公共字符串ContactName{get;set;}
    公共虚拟ICollection OwnedAccounts{get;set;}
    公众联络()
    {
    OwnedAccounts=新列表();
    }
    }
    
    接下来,为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);
        }
    }
    
    公共类AccountMapping:EntityTypeConfiguration
    {
    公共帐户映射()
    {
    HasOptional(x=>x.ParentAccount)。带有多个(x=>x.ChildAccounts)。HasForeignKey(x=>x.ParentAccountId);
    HasForeignKey(x=>x.AccountOwnerId);
    }
    }
    
    最后,将映射添加到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());
        }
    }
    
    public MyContext:DbContext
    {
    公共数据库集帐户{get;set;}
    公共数据库集联系人{get;set;}
    模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
    {
    modelBuilder.Configurations.Add(newaccountmapping());
    }
    }
    

    注意,我假设AccountOwner和ParentAccount是可选的。如果需要,只需将外部属性的类型从
    int?
    更改为
    int
    ,并将映射中的
    HasOptional
    更改为
    HasRequired

    ,我将如何解决这个问题

    将外键属性添加到类中。我说的是
    ParentAccountId
    AccountOwnerId
    。为了使导航更容易一些,我还向Account添加了一组子帐户,并向Contact添加了一组自有帐户。请注意,没有必要将“正常”属性设置为虚拟。只有导航属性应设置为虚拟

    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>();
        }
    }
    
    公共类帐户
    {
    public int AccountID{get;set;}
    公共字符串AccountName{get;set;}
    public int?ParentAccountId{get;set;}
    public int?AccountOwnerId{get;set;}
    公共虚拟帐户父帐户{get;set;}
    公共虚拟ICollection子帐户{get;set;}
    公共虚拟联系人帐户所有者{get;set;}
    公共帐户()
    {
    ChildAccounts=新列表();
    }
    }
    公共类联系人
    {
    public int ContactID{get;set;}
    公共字符串ContactName{get;set;}
    公共虚拟ICollection OwnedAccounts{get;set;}
    公众联络()
    {
    OwnedAccounts=新列表();
    }
    }
    
    接下来,为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);
        }
    }
    
    公共类AccountMapping:EntityTypeConfiguration
    {
    公共帐户映射()
    {
    HasOptional(x=>x.ParentAccount)。带有多个(x=>x.ChildAccounts)。HasForeignKey(x=>x.ParentAccountId);
    HasForeignKey(x=>x.AccountOwnerId);
    }
    }
    
    最后,将映射添加到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());
        }
    }
    
    public MyContext:DbContext
    {
    公共数据库集帐户{get;set;}