一对多关系概念模型中的实体框架关联

发布于 2025-01-01 13:31:39 字数 327 浏览 5 评论 0原文

我有两个实体 CUSTOMER 和 ORDER..从 CUSTOMER 到 ORDER 存在一对多关系,其中 CustomerID 是客户的主键和 ORDER 中的外键..现在我想从 ORDER 实体中的 CUSTOMER 实体添加客户名称属性...我已复制此属性并将其粘贴到 ORDER 表中,并添加了 CUSTOMER 表并将此属性映射到 CUSTOMER 表的相同属性..但是当我尝试验证它时却给了我一个错误

3024:从第 239 行开始映射片段时出现问题:必须指定 EntitySet ORDER 的所有关键属性 (ORDER.OrderID) 的映射

I have two entity CUSTOMER and ORDER..there is one to many relation from CUSTOMER to ORDER where CustomerID is primary key for customer and foreign key in ORDER..now I want to add customer name property from CUSTOMER entity in ORDER entity...I have copied this property and paste it in ORDER table and have added CUSTOMER table and map this property to the CUSTOMER table's same property..but when i trying to validate it vs giving me a Error that is

3024: Problem in mapping fragments starting at line 239:Must specify
mapping for all key properties (ORDER.OrderID) of the EntitySet ORDER

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

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

发布评论

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

评论(1

冷夜 2025-01-08 13:31:39

这在映射中是不可能的。您无法通过这种方式将 Customer 表中的属性添加到 Order 实体中。将属性从多个表映射到同一实体具有非常严格的规则,在这种情况下是不可能的。

您可以在 Order 类中公开客户的姓名,而无需在映射中定义它。创建 Order 类的部分部分并添加自定义计算属性(未映射):

public partial class Order
{
    public string CustomerName 
    {
        get 
        {
            // Customer is navigation property to Customer entity
            return Customer.Name;
        }
    }
}

这将需要使用您的 Order 加载 Customer (急切加载)或使用延迟加载。此外,此属性不能在 Linq-to-entities 查询中使用。

That is not possible in mapping. You cannot add property from Customer table into Order entity this way. Mapping properties from multiple tables to the same entity has very strict rules and it is not possible for this case.

You can expose customer's name in your Order class without defining it in the mapping. Create partial part of Order class and add custom computed property (non mapped):

public partial class Order
{
    public string CustomerName 
    {
        get 
        {
            // Customer is navigation property to Customer entity
            return Customer.Name;
        }
    }
}

This will require loading Customer with your Order (eager loading) or using lazy loading. Also this property cannot be used in Linq-to-entities queries.

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