gpt4 book ai didi

linq - 无效的列名实体猜测不存在的外键

转载 作者:行者123 更新时间:2023-12-02 09:34:58 24 4
gpt4 key购买 nike

我不太明白这里发生了什么。根据我的研究,我发现该实体正在尝试猜测外键的名称,但它不存在,因此它会抛出“无效的列名称”错误。问题是相关模型/表没有任何外键。所以我真的很困惑。

这是引发错误的代码:

foreach (TechnologyProjectPlanModel result in results)
{
//get approvers for plan
int id = result.Id;

try
{
List<ApprovalModel> approvers = db.ApprovalModels.Where(m => m.FormId == result.Id).Select(m => m).ToList(); //ERROR HERE

if (approvers != null)
{
result.Approvers = approvers.ToList();
}
}
catch (Exception e)
{
}
}

这是ApprovalModel:

public class ApprovalModel
{
[Key]
public int Id { get; set; }

public int ApprovalProcessId { get; set; }
public int FormId { get; set; }
public int UserId { get; set; }
public bool? Approved { get; set; }
}

这是 foreach 循环中引用的 TechnologyProjectPlanModel:

public class TechnologyProjectPlanModel
{
[Key]
public int Id { get; set; }

public int FormId { get; set; }
public int UserId { get; set; }
public string FormType { get; set; }
public int Status { get; set; }
public int Hidden { get; set; }
public DateTime DateSubmitted { get; set; }
public DateTime DateFinalized { get; set; }

public List<QuoteUploadsModel> Quotes { get; set; }
public List<ApprovalModel> Approvers { get; set; }

[Required]
[Display(Name = "Please Select Your School")]
public string School { get; set; }

[Required]
[Display(Name = "Requestor")]
public string Requestor { get; set; }

[Display(Name = "Title")]
public string Title { get; set; }

[Required]
[DataType(DataType.PhoneNumber)]
[Display(Name = "Phone Number")]
[Phone]
public string PhoneNumber { get; set; }

[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email Address")]
[EmailAddress]
public string Email { get; set; }

[Required]
[Display(Name = "Project Title")]
public string ProjectTitle { get; set; }

[Display(Name = "Requested Completion Date")]
public DateTime RequestedCompletionDate { get; set; }

[Required]
[Display(Name = "Project Description")]
public string ProjectDescription { get; set; }

[Display(Name = "Teacher/Room Number")]
public string TeacherGroup { get; set; }

[Display(Name = "1")]
public bool Grade1 { get; set; }
[Display(Name = "2")]
public bool Grade2 { get; set; }
[Display(Name = "3")]
public bool Grade3 { get; set; }
[Display(Name = "4")]
public bool Grade4 { get; set; }
[Display(Name = "5")]
public bool Grade5 { get; set; }
[Display(Name = "6")]
public bool Grade6 { get; set; }
[Display(Name = "7")]
public bool Grade7 { get; set; }
[Display(Name = "8")]
public bool Grade8 { get; set; }
[Display(Name = "9")]
public bool Grade9 { get; set; }
[Display(Name = "10")]
public bool Grade10 { get; set; }
[Display(Name = "11")]
public bool Grade11 { get; set; }
[Display(Name = "12")]
public bool Grade12 { get; set; }
[Display(Name = "Kindergarten")]
public bool Kindergarten { get; set; }

[Display(Name = "Describe how this plan will be continued if events cause programs or equipment to no longer be available. For example, if equipment purchased needs repair what funding source will be used for repair or replacement? For programs that have annual subscription fees, what funds will be used to continue the program from year to year?")]
public string Sustainability { get; set; }

public bool MultipleFundingSource { get; set; }

[Required(ErrorMessage="*")]
[Display(Name = "Funding Source")]
public string FundingSource1 { get; set; }

[Required(ErrorMessage = "*")]
public string FundingSource2 { get; set; }

[Required(ErrorMessage = "*")]
public string FundingSource3 { get; set; }

[Required(ErrorMessage = "*")]
public string FundingSource4 { get; set; }

[Required(ErrorMessage = "*")]
public string FundingSource5 { get; set; }

[Required]
[Display(Name = "Total Estimated Project Costs:")]
public float TotalEstimatedProjectCosts { get; set; }

//----------------------Additional Information

[Display(Name = "Additional Comments:")]
public string AdditionalComments { get; set; }

[Display(Name = "Additional Supporting Documents:")]
public string AdditionalSupportingDocuments { get; set; }
}

确切的错误是:

An error occurred while executing the command definition. See the inner exception for details.

Inner Exception:

Invalid column name 'TechnologyProjectPlanModel_Id'.

该列名称未在我的代码中的任何位置引用,因此它必须从某些内容中推断出来。

有什么想法吗?谢谢!

最佳答案

Entity Framework 依靠约定来确定它认为您的数据库是什么样子。在本例中,它认为 ApprovalModel 表应该具有 TechnologyProjectPlanModel 表的外键。将您的实体缩减到相关字段,它为什么这么认为就变得显而易见了:

public class ApprovalModel
{
}

public class TechnologyProjectPlanModel
{
public List<ApprovalModel> Approvers { get; set; }
}

用数据库术语来说,为了每个 TechnologyProjectPlanModel 存在多个 ApprovalModel,最有可能存在的关系是 ApprovalModel > 拥有 TechnologyProjectPlanModel 的外键。

如何设置TechnologyProjectPlanModel.Approvers?如果它与 Entity Framework 没有任何关系,并且您不希望它尝试通过其约定填充此属性,您可以通过告诉它该属性未像这样映射来显式告诉它不要做出该假设:

[NotMapped]
public List<ApprovalModel> Approvers { get; set; }

如果您确实有这种关系,则需要为 EF 提供更多上下文,以便它不会做出最佳猜测假设。例如,如果外键确实存在,请将其与相关导航属性一起放置在 ApprovalModel 中,该属性可以进一步告诉 EF 您的数据库是什么样的:

public class ApprovalModel
{
public int TechnologyProjectPlanModelId { get; set; }

// ForeignKey attribute usually not necessary unless you need to tell EF
// about a property that doesn't follow the usual "{OtherEntityName}Id"
// naming convention.
[ForeignKey("TechnologyProjectPlanModelId")]
public TechnologyProjectPlanModel TechnologyProjectPlanModel { get; set; }
}

public class TechnologyProjectPlanModel
{
public List<ApprovalModel> Approvers { get; set; }
}

关于linq - 无效的列名实体猜测不存在的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27364153/

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