EF 4.1 代码优先:如何设计和映射这些实体?
我有 3 个实体:Member
、AuthenticationToken
和 Email
。
- 每个
Member
可能有许多AuthenticationTokens
- 每个
AuthenticationToken
可能有一个或零个电子邮件
- 每个
成员
可能有零个或一个PrimaryEmail
(来自Emails
表)。 实际上,PrimaryEmail
是AuthenticationToken
的关联Email
之一
,所以我有:
public class Member {
public int MemberId { get; set; }
public int? PrimaryEmailId { get; set; }
public virtual Email PrimaryEmail { get; set; }
public virtual ICollection<AuthenticationToken> AuthenticationTokens { get; set; }
}
public class AuthenticationToken {
public int AuthenticationTokenId { get; set; }
public int MemberId { get; set; }
public virtual Member Member { get; set; }
public virtual Email Email { get; set; }
}
public class Email {
public int EmailId { get; set; } // is same as AuthenticationTokenId that the email associated with it
}
根据我上面解释的设计,我可以添加 Member 和 AuthenticationToken,但是当我想将电子邮件附加到 Member 或 AuthenticationToken(或两者)时,我给出以下错误:
INSERT 语句与 FOREIGN KEY 约束等冲突。
这个设计正确吗??? 如何设计我的表(和实体)来实现我的目的? 如何以代码优先方式映射我的实体?请问你有什么想法吗?
I have 3 entities: Member
, AuthenticationToken
, and Email
.
- Each
Member
may has manyAuthenticationTokens
- Each
AuthenticationToken
may has one or zeroEmail
- Each
Member
may has zero or onePrimaryEmail
(fromEmails
table). Really thePrimaryEmail
is one of theAuthenticationToken
s's associatedEmail
So I have:
public class Member {
public int MemberId { get; set; }
public int? PrimaryEmailId { get; set; }
public virtual Email PrimaryEmail { get; set; }
public virtual ICollection<AuthenticationToken> AuthenticationTokens { get; set; }
}
public class AuthenticationToken {
public int AuthenticationTokenId { get; set; }
public int MemberId { get; set; }
public virtual Member Member { get; set; }
public virtual Email Email { get; set; }
}
public class Email {
public int EmailId { get; set; } // is same as AuthenticationTokenId that the email associated with it
}
With design I explained above, I can add Member and AuthenticationToken, but when I want to attach a Email to a Member or AuthenticationToken (or both) I give this error:
The INSERT statement conflicted with the FOREIGN KEY constraint etc.
Is this design correct???
How can I design my tables (and entities) to achieve my purpose?
And how can I map my entities in code-first? Have you any idea please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我觉得默认约定无法理解我时,我个人使用 EF 4.1 中的 Fluent API 来配置我的所有实体,因此我将使用 Fluent API 进行回答。
以下是我如何设置模型:
这是我的上下文和流畅的配置:
我将在这里尽力解释我为什么这样设计的推理。首先,在您的模型中,
Member
似乎应该是根聚合(其他实体的老板)。我的意思是身份验证令牌
没有任何意义,除非它属于特定的成员
。Email
本身也没有任何意义,除非它属于Member
或属于AuthenticationToken
。因此,AuthenticationToken
没有属性来查明它所附加的Member
(要查明这一点,您首先需要一个Member
并且而不是仅仅看看它的收藏)。本质上,一切都围绕着Member
对象。如果没有Member
,则无法创建AuthenticationToken
。如果没有Member
或AuthenticationToken
,则无法创建Email
。我不完全确定您对 EF 4.1 中的流畅 API 是否满意,因此如果您有任何问题,请发表评论,我会尽力回答。我还提供了一个小型示例应用程序,用于构建和验证上面介绍的模型。如果您想运行该程序(它是一个小型控制台应用程序),您只需修改 App.config 中的连接字符串以指向您的 SQL Server 实例。
我担心的一件事是
Email
可以同时属于Member
和AuthenticationToken
。我的担忧来自于我必须设置一些有趣的级联删除。但是,我不知道您的所有要求,并且此设置似乎工作得很好,因此这可能不是问题。示例控制台应用程序
I personally use the fluent API in EF 4.1 to configure all of my entities when I don't feel the default conventions will understand me, so I will answer using the fluent API.
Here is how I would set up the models:
And this is my context and fluent configuration:
I will do my best here to explain my reasoning as to why I designed it like this. First of all, it appears that in your model
Member
should be the root aggregate (the boss of the other entities). What I mean is anAuthentication Token
makes no sense unless it belongs to a specificMember
. AnEmail
also makes no sense alone unless it either belongs to aMember
or belongs to anAuthenticationToken
. For this reasonAuthenticationToken
does not have a property to find out whatMember
it is attached to (to find this out you first need aMember
and than just look at its collection). Essentially, everything revolves around aMember
object. Without aMember
anAuthenticationToken
cannot be created. And without aMember
or anAuthenticationToken
anEmail
cannot be created.I'm not entirely sure how comfortable you are with the fluent API in EF 4.1, so if you have any questions leave a comment and I will do my best to answer them. I have also included a small sample application that I used to build and verify the model I presented above. If you want to run the program (it is a small Console app) you just have to modify the connection string in App.config to point to your instance of SQL Server.
One thing that concerns me is the fact that
Email
can belong to both aMember
and anAuthenticationToken
. My concern comes from the fact that I had to setup some interesting cascade deletes. I don't know all of your requirements, however, and this setup appears to work just fine so that may not be an issue.Example Console Application