无法确定'Vehicle_VehicleClass'关系的主要结尾。多个添加的实体可以具有相同的主键

时间:2015-02-17 02:57:45

标签: c# entity-framework

不确定为什么从VehicleClass到Vehicle的1:N关系不起作用。 尝试了不同的映射作为下面的注释代码。我做过其他1:N关系。不确定这个有什么不同。

        modelBuilder.Entity<Vehicle>()
            .HasRequired(x => x.VehicleClass)
            .WithMany(x => x.Vehicles)
            .HasForeignKey(x => x.VehicleClassId)
            .WillCascadeOnDelete(false);

        //modelBuilder.Entity<VehicleClass>()
        //    .HasMany(x => x.Vehicles)
        //    .WithRequired(x => x.VehicleClass)
        //    .HasForeignKey(x => x.VehicleClassId)
        //    .WillCascadeOnDelete(false);

        //modelBuilder.Entity<Vehicle>()
        //    .HasRequired(x => x.VehicleClass)
        //    .WithRequiredPrincipal()
        //    .WillCascadeOnDelete(false);

        //modelBuilder.Entity<Vehicle>()
        //    .HasOptional(x => x.VehicleClass)
        //    .WithOptionalPrincipal();
        //    .WillCascadeOnDelete(false);

        //modelBuilder.Entity<Vehicle>()
        //    .HasRequired(x => x.VehicleClass)
        //    .WithRequiredDependent()
        //    .WillCascadeOnDelete(false);

        //modelBuilder.Entity<Vehicle>()
        //    .Ignore(x => x.VehicleClass);

DomainBase.cs

public abstract class DomainBase : IEntity
{
    public virtual int Id { get; set; }
    public virtual bool IsDeleted { get; set; }

    public virtual DateTime CreatedDate { get; set; }
    public virtual int CreatedByUserId { get; set; }

    public virtual DateTime? LastModifiedDate { get; set; }
    public virtual int? LastModifiedByUserId { get; set; }

    public virtual void ConvertDatesToGmt()
    {
        this.CreatedDate = this.CreatedDate.UtcToGmt();

        if (this.LastModifiedDate.HasValue)
        {
            this.LastModifiedDate = this.LastModifiedDate.Value.UtcToGmt();
        }
    }
}

VehicleClass.cs

public class VehicleClass : DomainBase
{
    public virtual string ClassName { get; set; }
    public virtual string Make { get; set; }
    public virtual string Model { get; set; }
    public virtual string ModelVariant { get; set; }
    public virtual string Configuration { get; set; }
    public virtual string ProductionName { get; set; }
    public virtual DateTime? ProductionStartDate { get; set; }
    public virtual DateTime? ProductionEndDate { get; set; }
    public virtual int? GrossVehicleWeightKg { get; set; }
    public virtual decimal? WidthMeters { get; set; }
    public virtual decimal? LengthMeters { get; set; }
    public virtual decimal? HeightMeters { get; set; }
    public virtual Int16? WheelbaseMm { get; set; }
    public virtual Int16? PayloadRangeLowKg { get; set; }
    public virtual Int16? PayloadRangeHighKg { get; set; }
    public virtual bool IsHGV { get; set; }

    public virtual IList<Vehicle> Vehicles { get; set; }
}

Vehicle.cs

public class Vehicle : DomainBase
{
    public virtual string Registration { get; set; }
    public virtual DateTime ServiceStartDate { get; set; }
    public virtual DateTime? ServiceEndDate { get; set; }
    public virtual string Region { get; set; }

    public virtual int VehicleClassId { get; set; }
    public virtual VehicleClass VehicleClass { get; set; }

    public virtual IList<VehicleDefect> VehicleDefects { get; set; }
    public virtual IList<VehicleInspection> VehicleInspections { get; set; }
}

1 个答案:

答案 0 :(得分:0)

fk在db中存储了name_id。我必须在EF配置中指定它。误导性错误信息。