c# - 属性名称 'ProductId' 已定义

标签 c# entity-framework entity-framework-6

我有一个实体,该实体使用 FK ProductId 建立关系,然后我使用复合键 ProductId 和 VehicleId 在同一实体上建立另一个关系。这是行不通的。我明白了

One or more validation errors were detected during model generation:

ProductId: Name: Each property name in a type must be unique. Property name 'ProductId' is already defined.

配置代码

public class BookingConfiguration : EntityTypeConfiguration<Booking>
    {
        public BookingConfiguration()
        {    
            ...

            HasRequired(b => b.Product)
                .WithMany(p => p.Bookings)
                .Map(m =>
                {
                    m.MapKey("ProductId");
                });

            HasRequired(b => b.Vehicle)
                .WithMany(v => v.Bookings)
                .Map(m =>
                {
                    m.MapKey("ProductId","VehicleId");
                });
        }
    }

最佳答案

Steve Greene 让我走上正轨,我将 ProductIdVehicleId 添加到实体并使用

HasRequired(b => b.Vehicle)
    .WithMany()
    .HasForeignKey(b => new {b.ProductId, b.VehicleId});

从域的角度来看,我不喜欢向实体添加外键,因此如果有人有更好的解决方案,请告诉我。

关于c# - 属性名称 'ProductId' 已定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32523285/

相关文章:

mysql - Entity Framework 和数据库不可知编程,可能性?

c# - Web API 的动态 LINQ 查询

c# - Entity Framework - Distinct 和 Max 生成巨大(且缓慢)的查询

c# - EF6 Model First 无法导入函数

c# - 接口(interface)和异步方法

c# - Linq 中按元素分组

C# 二进制数组到十进制?

c# - 通用 orderby 子句的动态表达式

c# - MVC Seed 不将数据放入数据库

c# - 对 ASP.NET WebMethod 的 jQuery AJAX 调用