gpt4 book ai didi

asp.net-mvc - 数据库中的代码优先创建无效的导航属性

转载 作者:行者123 更新时间:2023-12-04 16:08:20 25 4
gpt4 key购买 nike

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

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

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

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

enter image description here

为了完整起见,这是导致此错误的“驱动程序”模型。有很多“虚拟”属性在运行时创建一个新类(动态代理)来创建加载导航属性的逻辑:
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”等)生成了任何类。

一切都很好,但我被这个错误困住了。我需要做什么修改模型类来阻止错误。从我目前的知识立场来看,一切似乎都被正确定义。

当我尝试引用驱动程序模型时出现问题。这是相关表的更详细 View - 包括那些没有错误的表:

enter image description here

最佳答案

您似乎缺少导航属性:

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

这些会将您的 Driver 表与这些虚拟关系链接起来:
 public virtual Attribute Attribute { get; set; }
public virtual Shift Shift { get; set; }

问候,

关于asp.net-mvc - 数据库中的代码优先创建无效的导航属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com