我更习惯手动sql查询,但我不得不承认EF核心上下文和模型更方便,因为我不需要手动打开和关闭数据库。一切都焕然一新。但我真的不知道一个非常简单的 LINQ 查询(我认为类似于 sql 查询)如何返回一个错误,其字段名甚至不在我的任何模型上。
[Index(nameof(Name), IsUnique = true)]
internal class modelLibraryCustomer
{
[Key]
public string Id { get; set; }
public string Name { get; set; }
public string CompanyName { get; set; }
public string Telephone { get; set; }
public string Additional { get; set; }
public bool Active { get; set; }
}
[Index(nameof(IdSubProduct), nameof(EffectiveDate), IsUnique = true)]
internal class modelDataProductSubProductRate
{
[Key]
public string Id { get; set; }
public string IdSubProduct { get; set; }
public string? IdCustomer { get; set; }
public DateOnly EffectiveDate { get; set; }
public decimal Rate { get; set; }
public string Additional { get; set; }
public virtual modelLibraryCustomer Name { get; set; }
}
var query = (from rm in this.contextSQL.ProductSubProductRate
join c in this.contextSQL.LibraryCustomer on rm.IdCustomer equals c.Id
select new { rm.Id, rm.Name, rm.Rate }).ToList();
this.contextSQL.ProductSubProductRate.Load();
MySqlConnector.MySqlException:“‘on 子句’中存在未知列‘p.NameId’”
我的模型/数据库中没有一列包含 NameId。
您需要告诉 EF Core
modelLibraryCustomer
表的外键是什么。按照惯例,它正在寻找不存在的NameId
。请参阅:https://learn.microsoft.com/en-us/ef/core/modeling/relationships/conventions#discovering-foreign-key-properties
假设
IdCustomer
是外键,您需要将此信息告知模型,这可以通过属性来完成:
[ForeignKey(nameof(IdCustomer)]
public virtual modelLibraryCustomer Name { get; set; }