Code First中的Code First创建无效的导航属性

时间:2015-07-05 18:49:44

标签: asp.net-mvc entity-framework ef-code-first

我正在使用Entity Framework从现有数据库表生成模型。在“实体数据模型向导”中,我选择了“数据库中的代码优先”选项,为我提供了POCO类。每个类镜像数据库表,并包含与表中列对应的属性。

问题是,当我运行项目时,我得到与实际数据库中不存在的导航属性相关的错误。这些是典型的EF生成导航属性:

Invalid column name 'Attribute_AttributeId' and Invalid column name 'Shift_ShiftId'

数据库中没有实际的字段,因为(我相信)这些动态导航字段是在运行时创建的:

enter image description here

为了完整性,这里是'Driver'的模型,它导致了这个错误。有很多“虚拟”属性在运行时创建一个新类(动态代理)来创建加载导航属性的逻辑:

namespace IntrinsicDataLoader.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class Driver
    {
        public Driver()
        {
            DriverTrackings = new HashSet<DriverTracking>();
            DriverVehicles = new HashSet<DriverVehicle>();
        }

        public int DriverId { get; set; }

        public Guid DriverGuid { get; set; }

        public long ActivityLogId { get; set; }

        [Required]
        [StringLength(10)]
        public string DriverNumber { get; set; }

        public long TransportProvideId { get; set; }

        [Required]
        [StringLength(50)]
        public string FirstName { get; set; }

        [Required]
        [StringLength(50)]
        public string LastName { get; set; }

        public int? ShiftPatternId { get; set; }

        [Required]
        [StringLength(50)]
        public string Password { get; set; }

        [StringLength(50)]
        public string Status { get; set; }

        public DateTime? LastActive { get; set; }

        [StringLength(50)]
        public string DriverType { get; set; }

        [StringLength(50)]
        public string AKA { get; set; }

        [StringLength(500)]
        public string HomeAddress { get; set; }

        [StringLength(100)]
        public string Email { get; set; }

        [StringLength(20)]
        public string MobilePhone { get; set; }

        [StringLength(20)]
        public string OtherPhone { get; set; }

        [StringLength(10)]
        public string Gender { get; set; }

        [StringLength(50)]
        public string Ethnicity { get; set; }

        public DateTime StartDate { get; set; }

        [Required]
        [StringLength(50)]
        public string Badge { get; set; }

        public DateTime BadgeExpiry { get; set; }

        [Required]
        [StringLength(50)]
        public string BadgeType { get; set; }

        [Required]
        [StringLength(50)]
        public string LicenceNumber { get; set; }

        public DateTime LicenceExpiry { get; set; }

        public DateTime? SchoolBadgeExpiry { get; set; }

        [StringLength(20)]
        public string NINumber { get; set; }

        public bool? ApplyVAT { get; set; }

        public decimal? VATRate { get; set; }

        public decimal? Balance { get; set; }

        public decimal? CommissionPercentage { get; set; }

        [StringLength(50)]
        public string PoliceDisclosure { get; set; }

        public DateTime? ProofOfAddressSupplied { get; set; }

        public DateTime? AgreementSignDate { get; set; }

        public int? PhotoId { get; set; }

        public bool? IsActive { get; set; }

        public bool? IsDeleted { get; set; }

        [Column(TypeName = "date")]
        public DateTime? DateUpdated { get; set; }

        [Column(TypeName = "date")]
        public DateTime? DateInserted { get; set; }

        [StringLength(50)]
        public string UpdatedBy { get; set; }

        [StringLength(50)]
        public string InsertedBy { get; set; }

        public virtual DriverActivityLog DriverActivityLog { get; set; }

        public virtual TransportProvider TransportProvider { get; set; }

        public virtual ICollection<DriverTracking> DriverTrackings { get; set; }

        public virtual ICollection<DriverVehicle> DriverVehicles { get; set; }

        public virtual Attribute Attribute { get; set; }

        public virtual Shift Shift { get; set; }
    }
}

错误与我正在使用的联结表特别相关。我理解,作为联结表的唯一功能是创建多对多关系,EF只是将其转换为多对多关系 - 因此联结表本身变得透明 - 就像它不存在 - 但能力访问关系仍然存在。为了支持这一点,我没有为'DriverAttribute'或'DriverShift'生成任何类,而是为其他相关表做,例如'Attribute','Shift','TransportProvider','DriverVehicle'等。

这一切都很好,但我坚持这个错误。我需要做什么来修改模型类以停止错误。从我目前的知识位置看,一切似乎都是正确定义的。

当我尝试引用Driver模型时出现问题。以下是相关表格的更详细视图 - 包括那些没有错误的表格:

enter image description here

1 个答案:

答案 0 :(得分:0)

您似乎缺少导航属性:

public int ShiftId{ get; set; }
public int AttibuteId { get; set; }

这些会将您的Driver表与这些虚拟关系链接起来:

 public virtual Attribute Attribute { get; set; }
 public virtual Shift Shift { get; set; }

此致