Fluent API,EF 4.1:继承和外键问题

发布于 2024-11-02 04:52:24 字数 843 浏览 1 评论 0原文

有几个简单的类:

第一类:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小猫一只 2024-11-09 04:52:24

我认为 Publications 表中不可能有相同的外键列。 Publication.AuthorUser.Topics 不能是同一个关联的端点。您可以拥有一个不是 TopicPublication 实例和对 User 的引用:

User user = new User() { Topics = new List<Topic>() };
Publication publication = new Publication();

publication.Author = user;

user.Topics.Add(???);

At ???您无法添加 publication,因为它不是 Topic 实例。 user.Topics 必须引用 publication 之外的另一个对象,这意味着这些端点不能属于同一关联。

编辑

如果您只想与数据库中的单个外键列建立一个关联,则必须将 Author 属性从 Publication 移至 < code>Topic 或让 User 类中的集合引用 Publication 而不是 Topic

public ICollection<Publication> Publications { get; set; }

I don't think that it's possible to have the same foreign key column in the Publications table. Publication.Author and User.Topics cannot be the endpoints of one and the same association. You could have a Publication instance which isn't a Topic and a reference to a User:

User user = new User() { Topics = new List<Topic>() };
Publication publication = new Publication();

publication.Author = user;

user.Topics.Add(???);

At ??? you can't add publication because it isn't a Topic instance. user.Topics must refer to another object than publication 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 from Publication to Topic or let the collection in your User class refer to Publication instead of Topic:

public ICollection<Publication> Publications { get; set; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文