1

我正在尝试 EF 和 Code First 方法

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

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

联系人类是一个简单的类

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

现在我不知道如何将 ID 添加到帐户类。

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

  1. 我可以添加一个帐户作为另一个帐户的父级

  2. 我可以访问特定帐户的 ParentAccount

  3. 我可以访问特定帐户的 AccountOwner

我是新来的。任何帮助,将不胜感激

4

3 回答 3

1

这是我将如何解决这个问题。

将外键属性添加到您的类。我说的是ParentAccountIdAccountOwnerId。为了使导航更容易一些,我还在 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>();
    }
}

接下来,为 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);
    }
}

最后,将映射添加到您的 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());
    }
}

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

于 2013-01-06T10:22:13.017 回答
0
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;}
}
于 2013-01-06T08:39:51.217 回答
0

您可以在不使用 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;}
}

然后您的数据库表将如下创建:

Accounts Table

Id            int             NotNull
AccountName   nvarchar(100)   AllowNull

   
Contacts Table

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

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

于 2013-01-06T09:10:29.223 回答