简单的一对多关系(关联)在 EF 设计器中失败

发布于 2024-10-09 11:55:17 字数 678 浏览 3 评论 0原文

我尝试在实体框架设计器中设置简单的一对多关系。

这些表是类别 (1)事务 (N)。这就是我所做的:

Add "association"

 End1 = Category, multiplicity 1, navigation property=Transaction
 End2 = Transaction, multiplicity Many, navigation property = Category

构建它时出现错误“未指定映射”。好吧,有道理。所以我添加了这个映射:

 Category
   Category.CategoryID = Transaction.CategoryID

但是映射设计器还会自动添加事务表的映射,我不知道如何删除或如何设置:

 Transaction
    Transaction.TransactionID = ???

将其留空似乎最有效,但这给了我:错误 3024“必须指定映射所有关键属性(TransactionID)”

并尝试将其设置为假 int 属性,只是希望这是一个编译器错误。但这给了我错误 3002 和 3003。

我不知道该怎么办。关联不是应该这样使用吗?

I tried setting up a simple one-to-many relation in Entity Frameworks designer.

The tables are Category (1) and Transaction (N). Here's what I did:

Add "association"

 End1 = Category, multiplicity 1, navigation property=Transaction
 End2 = Transaction, multiplicity Many, navigation property = Category

Building it gave me the error "No mapping specified". Ok, makes sense. So I added this mapping:

 Category
   Category.CategoryID = Transaction.CategoryID

But the mapping designer also automatically adds a mapping for the Transaction table, which I cannot figure out how to delete or how to setup:

 Transaction
    Transaction.TransactionID = ???

Leaving it empty seems most valid, but that gives me: Error 3024 "Must specify mapping for all key properties (TransactionID)"

And trying to set it to a fake int property just hoping it's a compiler bug. But that gives me errors 3002 and 3003.

I dont get what to do. Isnt Associations meant to be used this way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

冰雪之触 2024-10-16 11:55:17

我建议创建(或从数据库导入)一个类别实体和一个事务实体。根据需要向每个添加标量属性。接下来,右键单击您的实体,单击表映射,然后将实体属性映射到表字段。例如,对于 Category 实体,将 CategoryID 字段映射到 CategoryID 属性。对其他实体执行相同的操作。然后创建关联。

请注意,通过公开的外键链接的关联没有任何映射。
顺便说一句,您可能还想添加导航属性。

I suggest creating (or importing from the database) an entity for Catagory and an entity for Transaction. Add scalar properties to each as needed. Next, right-click on your entity, click Table Mapping, and map your entity properties to the table fields. For example, for the Category entity, map CategoryID field to a CategoryID property. Do the same for the other entity. THEN create the association.

Note that associations linked by exposed foreign keys do not have any mappings.
BTW, you'll probably want to add navigation properties as well.

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