Fluent API,EF 4.1:继承和外键问题
有几个简单的类:
第一类:
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
// ...
public ICollection<Topic> Topics { get; set; }
}
第二类:
public class Publication
{
public int PublicationId { get; set; }
public string Title { get; set; }
/ ...
public User Author { get; set; }
}
第三类:
public class Topic: Publication
{
public string TopicContent { get; set; }
// ...
}
为我的模型创建数据库后,我有以下数据库结构:
Users
UserId
UserName
Publications 。
和
我得到两个字段Author_UserId
您所看到的,
正如
User_UserId
在表 Publications 中具有相同的角色
如何使用 Fluent API 或数据注释将此字段合并为一个字段?
There are several simple classes:
The first class:
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
// ...
public ICollection<Topic> Topics { get; set; }
}
The second class:
public class Publication
{
public int PublicationId { get; set; }
public string Title { get; set; }
/ ...
public User Author { get; set; }
}
The third class:
public class Topic: Publication
{
public string TopicContent { get; set; }
// ...
}
After creating a database for my model I have the following stucture of the database:
Users
UserId
UserName
Publications
PublicationId
Title
TopicContent
Author_UserId
User_UserId
As you can see I get two fields Author_UserId and User_UserId having identical role in the table Publications.
How can I merge this fields into one field using Fluent API or Data Annotation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
Publications
表中不可能有相同的外键列。Publication.Author
和User.Topics
不能是同一个关联的端点。您可以拥有一个不是Topic
的Publication
实例和对User
的引用:At ???您无法添加
publication
,因为它不是Topic
实例。user.Topics
必须引用publication
之外的另一个对象,这意味着这些端点不能属于同一关联。编辑
如果您只想与数据库中的单个外键列建立一个关联,则必须将
Author
属性从Publication
移至 < code>Topic 或让User
类中的集合引用Publication
而不是Topic
:I don't think that it's possible to have the same foreign key column in the
Publications
table.Publication.Author
andUser.Topics
cannot be the endpoints of one and the same association. You could have aPublication
instance which isn't aTopic
and a reference to aUser
:At ??? you can't add
publication
because it isn't aTopic
instance.user.Topics
must refer to another object thanpublication
which means that those endpoints cannot belong to the same association.Edit
If you want only one single association with only a single foreign key column in the database you must either move the
Author
property fromPublication
toTopic
or let the collection in yourUser
class refer toPublication
instead ofTopic
: