这可能是重复的,但真诚地,我花了不眠之夜试图找出如何实现这种关系。基本上,银行客户可以拥有许多他们主要的账户,但也可以是任何其他账户的共同所有权。
这是我的客户模型:
public class Customer
{
public int CustomerNumber { get; set;}
public string Name { get; set;}
public ICollection<Account> Accounts { get; set;}
public virtual ICollection<JointAccount>? JointAccounts { get; set;}
}
我的
Account
模型看起来像这样:
public class Account
{
public string AccountNumber {get; set;}
public string Name {get; set;}
public Customer Customer {get; set;}
public virtual ICollection<JointAccount>? JointOnwers {get; set;} = new List<JointAccount>();
}
然后是
JointAccount
的模型,如下所示:
public class JointAccount
{
public int CustomerNumber { get; set; }
public Customer Customers { get; set; }
public int AccountNumber { get; set; }
public Account Accounts { get; set; }
}
EF Core 配置是这样的:
public class MemberConfig : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.HasKey(x => x.CustomerNumber);
builder.HasMany( a => a.Accounts)
.WithMany(a => a.AccountNumber)
.IsRequired();
}
}
public class AccountConfig : IEntityTypeConfiguration<Account>
{
public void Configure(EntityTypeBuilder<Account> builder)
{
builder.HasKey(x => x.AccountNumber);
builder.HasMany<JointAccount>(jo => jo.JointOnwers)
.WithMany(jo => jo.MemberNumber);
}
}
public class JointAccountConfig : IEntityTypeConfiguration<JointAccount>
{
public void Configure(EntityTypeBuilder<JointAccount> builder)
{
builder.HasKey(x => new {x.CustomerNumber, x.AccountNumber});
}
}
到目前为止,我已经根据我的理解尝试了不同的变化,但对我来说似乎并不正确。如果有好心人能解释一下,我将不胜感激:
您认为可以帮助我更好地理解这个概念的任何资源都将受到高度赞赏。
提前非常感谢您
如果我理解正确的话:
一个客户可以有多个“主账户”:一对多
一个客户还可以拥有多个“联名账户”,并且多个客户可以共享自己的“联名账户”:多对多
所以实体可能是这样的:
public class Customer
{
[Key]
public int CustomerNumber { get; set; }
public string Name { get; set; }
public IEnumerable<Account> PrimaryAccounts { get; set; }
public IEnumerable<Account> JointAccounts { get; set; }
}
public class Account
{
[Key]
public string AccountNumber { get; set; }
public string Name { get; set; }
public Customer PrimaryOwner{ get; set; }
public IEnumerable<Customer> JointOwners { get; set; }
}
DbContext:
public class MyContext :DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseMySql("server=10.96.5.5;database=WA37;user=root;password=xxxxxxx", new MySqlServerVersion(new Version()));
}
public DbSet<Account> Accounts { get; set; }
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasMany(x => x.PrimaryAccounts)
.WithOne(x => x.PrimaryOwner);
modelBuilder.Entity<Customer>()
.HasMany(x => x.JointAccounts)
.WithMany(x => x.JointOwners);
}
}
这些导航属性允许您使用模型构建器显式配置关系。通常你需要他们来做一些查询。比如当你想查询一个账号的联名拥有者时:
IEnumerable<Customer> jointowners=_mycontext.Accounts.FirstOrDefault(x=x.AccountNumber="xxx").jointOwners;
参考:
https://www.entityframeworktutorial.net/efcore/configure-one-to-many-relationship-using- Fluent-api-in-ef-core.aspx
https://www.entityframeworktutorial.net/efcore/configure-many-to-many-relationship-in-ef-core.aspx