简体   繁体   English

正确的模式以与不同的相关实体进行一对多处理

[英]Proper pattern to handle one-to-many with different related entities

I have a C# project and I use Entity Framework as the ORM. 我有一个C#项目,并且使用Entity Framework作为ORM。 I have a User , which is able to pay to many banks. 我有一个User ,可以向许多银行付款。 Each bank is a separate entity and each one of them is described by different fields. 每个银行都是一个单独的实体,每个实体由不同的字段描述。

The problem is - one User can have no-or-many different Bank s. 问题是-一个User可以拥有多个不同的Bank I'm not really sure how to model this - the temporary solution was to add an additional UserBank entity, which is in 1:1 realtionship with User . 我不确定如何建模-临时解决方案是添加一个额外的UserBank实体,该实体与User处于1:1关系。 The classes look (more or less) like that: 这些类看起来(或多或少)如下:

public class User
{
  public virtual UserBank Banks { get; set; }
  // (...)
}

and

public class UserBank
{
  public virtual User { get; set; }
  public virtual Bank1 { get; set; }
  public virtual Bank2 { get; set; }
  public virtual Bank3 { get; set; }
}

In that scenario, User may or may not have accounts in any of the Bank s. 在这种情况下, User在任何Bank可能有也可能没有帐户。 If he doesn't have one, the value will be NULL . 如果他没有,则该值为NULL

It's not perfect - but at least User doesn't need to know anything about Bank types. 这并不完美-但至少User不需要了解任何有关Bank类型的信息。 The problem is that if I want to add a new Bank , I need to modify UserBank entity, which is violating the O in SOLID , which I try to always follow. 问题是,如果我想添加一个新的Bank ,我需要修改UserBank实体,这违反了SOLIDO ,我一直尝试遵循。

Additionally, I don't like the idea that UserBank has to store N empty (NULL) columns for N banks if User doesn't have any bank accounts. 另外,如果User没有任何银行帐户,我不喜欢UserBank必须为N个银行存储N个空(NULL)列的想法。 Is there a proper pattern to handle that situation? 是否有适当的模式来处理这种情况?


EDIT 编辑

I am aware that it's a relatively easy problem in raw SQL. 我知道在原始SQL中这是一个相对容易的问题。 The problem here is how to do that with Entity Framework with code-first. 这里的问题是如何使用代码优先的Entity Framework做到这一点。

This looks like a classical m:n relationship. 这看起来像是经典的m:n关系。 Each User can have different banks and each bank will have many users. 每个用户可以有不同的银行,每个银行将有许多用户。

You need three tables: 您需要三个表:

  • User (UserID, Name, ...) 用户(用户ID,名称等)
  • Bank (BankID, Name, ...) 银行(BankID,名称等)
  • UserBank(UserBankID, UserID FK-to-User, BankID FK-to-Bank, FurtherDetails ...) UserBank(UserBankID,UserID FK到用户,BankID FK到银行,MoreDetails ...)

If you want to ensure, that a user cannot have multiple connections with the same bank, you might place a unique key on the combination of UserID/BankID or let this be the PK of you mapping table. 如果要确保用户不能与同一银行有多个连接,则可以在UserID / BankID的组合上放置一个唯一键,或者将其作为映射表的PK。 But I don't think this was correct actually... 但我认为这实际上是不正确的...

The entity tables (User/Bank) hold a description of their object, but nothing more. 实体表(用户/银行)持有对象的描述,但仅此而已。 The mapping table gets all the needed data which describes the connection between these two (date of creation, active, account (which should be a FK to another entity table) and so on...) 映射表获取描述这两者之间的连接的所有必需数据(创建日期,活动日期,帐户(应为与另一个实体表的FK),等等)。

UPDATE example to illustrate a m:n -relationship UPDATE示例以说明m:n关系

User 用户

  1 John
  2 Jane
  3 Tim

Bank 银行

 1 Bank of England
 2 Deutsche Bank
 3 Crash & Desaster

UserBank UserBank

 1 1 1 --> User 1 is connected with Bank 1 --> John uses BoE  
 2 1 2 --> John uses Deutsche Bank too  
 3 2 1 --> And Jane is at Deutsche Bank  
 4 3 3 --> while Tim is at C&D  

UPDATE 2 更新2

No experience with EF code first, but this would be the line in C#: 首先没有使用EF代码的经验,但这将是C#中的代码行:

public class User
{
  public int UserID{get;set}
  public string Name...
  public List<UserBank> MyBanks{ ... fetch by this.UserID ... }
}
public class Bank
{
  public int BankID{get;set}
  public string Name{get;set;}
  public List<UserBank> MyUsers{... fetch by this.BankID ... }
}

public class UserBank
{
  public int UserBankID{get;set}
  public User{get;set;}
  public Bank{get;set;} 
}

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

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