适合不同相关实体处理一对多的模式 [英] Proper pattern to handle one-to-many with different related entities
问题描述
用户
,可以支付给许多银行。每个银行是一个单独的实体,每个银行都由不同的领域描述。 问题是 - 一个用户
可以没有或许多不同 / code>秒。我不太确定如何建模 - 临时解决方案是添加一个额外的
UserBank
实体,与 1
。类似于(或多或少)类似于
public class User
{
public virtual UserBank银行{get;组; }
//(...)
}
和
public class UserBank
{
public virtual User {get;组; }
public virtual Bank1 {get;组; }
public virtual Bank2 {get;组; }
public virtual Bank3 {get;组; }
}
在这种情况下,用户
可能或可能没有任何银行
中的帐户。如果他没有,值将为 NULL
。
这不完美 - 但至少用户
不需要知道关于银行
类型。问题是,如果我想添加一个新的银行
,我需要修改 UserBank
实体,这违反了 O
在 SOLID
中,我试图一直遵循。
<另外,我不喜欢
UserBank
必须为N个银行存储N个空(NULL)列的想法,如果 User
没有任何银行账户。有没有适当的模式来处理这种情况? 编辑
我知道在原始SQL中这是一个比较容易的问题。这里的问题是如何使用实体框架进行代码优先。
这看起来像一个古典的 m:n
关系。每个用户可以有不同的银行,每个银行将有很多用户。
您需要三个表:
- 用户(UserID,Name,...)
- 银行(BankID,Name,...)
- UserBank UserBankID,用户ID FK到用户,BankID FK到银行,更多细节...)
如果你想确保,用户不能与同一个银行拥有多个连接,您可以在UserID / BankID的组合上放置一个唯一的密钥,或者让它成为映射表的PK。但我不认为这是正确的...
实体表(User / Bank)持有他们的对象的描述,但没有更多。映射表获取描述这两者之间的连接的所有需要的数据(创建日期,活动的,帐户(应该是另一个实体表的FK等)...)
更新示例来说明一个 m:n
-relationship
用户
1 John
2 Jane
3 Tim
银行
1英格兰银行
2德意志银行$ b 3碰撞& Desaster
UserBank
code> 1 1 1 - >用户1与Bank 1连接 - >约翰使用BoE
2 1 2 - >约翰也使用德意志银行也
3 2 1 - >简在德意志银行
4 3 3 - >而Tim位于C& D
更新2
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;}
}
I have a C# project and I use Entity Framework as the ORM. I have a User
, which is able to pay to many banks. 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. 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
. 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. If he doesn't have one, the value will be NULL
.
It's not perfect - but at least User
doesn't need to know anything about Bank
types. 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.
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. Is there a proper pattern to handle that situation?
EDIT
I am aware that it's a relatively easy problem in raw SQL. The problem here is how to do that with Entity Framework with code-first.
This looks like a classical m:n
relationship. Each User can have different banks and each bank will have many users.
You need three tables:
- User (UserID, Name, ...)
- Bank (BankID, Name, ...)
- UserBank(UserBankID, UserID FK-to-User, BankID FK-to-Bank, FurtherDetails ...)
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. 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...)
UPDATE example to illustrate a m:n
-relationship
User
1 John
2 Jane
3 Tim
Bank
1 Bank of England
2 Deutsche Bank
3 Crash & Desaster
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
No experience with EF code first, but this would be the line in 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;}
}
这篇关于适合不同相关实体处理一对多的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!