C# 为什么我会得到“无效列名'Table_TableId'(外键已经配置了Fluent API)?
我正在用C重构现有项目.NET中的代码…我正在使用反向POCO生成模型并配置外键--这是一个非常大的数据库 在我尝试查询一个表之前,一切都正常……反向POCO生成的模型包含以下引用客户表的FKs:C# 为什么我会得到“无效列名'Table_TableId'(外键已经配置了Fluent API)?,c#,.net,windows,entity-framework,ef-fluent-api,C#,.net,Windows,Entity Framework,Ef Fluent Api,我正在用C重构现有项目.NET中的代码…我正在使用反向POCO生成模型并配置外键--这是一个非常大的数据库 在我尝试查询一个表之前,一切都正常……反向POCO生成的模型包含以下引用客户表的FKs: public virtual Customer Customer_CustomerId { get; set; } public virtual Customer Customer1 { get; set; } public virtual Customer MatchingCustomer
public virtual Customer Customer_CustomerId { get; set; }
public virtual Customer Customer1 { get; set; }
public virtual Customer MatchingCustomer { get; set; }
但当我尝试使用LINQ进行选择时,会引发一个异常:
列名“Customer\u CustomerId”无效
从我所读到的,我所需要的就是用Fluent API映射FKs…Reverse POCO还为我提供了每个模型的配置文件,这是这些FKs的配置
HasOptional(a => a.Customer_CustomerId).WithMany(b => b.MapCustomerMatches_CustomerId).HasForeignKey(c => c.CustomerId).WillCascadeOnDelete(false);
HasOptional(a => a.Customer1).WithMany(b => b.MapCustomerMatches1).HasForeignKey(c => c.CustomerId).WillCascadeOnDelete(false);
HasOptional(a => a.MatchingCustomer).WithMany(b => b.MatchingCustomer).HasForeignKey(c => c.MatchingCustomerId).WillCascadeOnDelete(false);
我不知道为什么会这样…EF关系应该在两个模型中定义,但只有一个 错误:列名“Category\u CategoryID”无效 这是EF生成的SQL:
SELECT
[Extent1].[SubtypeID] AS [SubtypeID],
[Extent1].[SupplierID] AS [SupplierID],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[Category_CategoryID] AS [Category_CategoryID] -- <- here 1
FROM [dbo].[Subtypes] AS [Extent1]
错误原因:从类别实体取消注释ICollection引用Sybtype以复制问题:
public sealed class Category
{
public Category()
{
Products = new List<Product>();
}
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public byte[] Picture { get; set; }
public ICollection<Product> Products { get; set; }
// uncomment this line to reproduce the issue
// public ICollection<Subtype> Subtypes { get; set; } //<- here 2
}
但子类型不是指类别,而是指供应商:
public class Subtype : BaseModel
{
public override int Id { get; set; }
public int? SupplierID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
// Here is what is expected from EF (two side relation, but you've got only one)
// public virtual Category Category { get; set; }
public virtual Supplier Supplier { get; set; } //<- here 3
}
public sealed class Supplier
{
public int SupplierID { get; set; }
public ICollection<Subtype> Subtypes { get; set; }
}
流畅的API信息在这里也可能有帮助:
public class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
HasKey(t => t.CategoryID);
ToTable("Categories");
Property(t => t.CategoryID).HasColumnName("CategoryID");
}
}
public class SubtypeMap : EntityTypeConfiguration<Subtype>
{
public SubtypeMap()
{
HasKey(t => t.Id);
Property(t => t.SupplierID).HasColumnName("SupplierID");
HasOptional(t => t.Supplier)
.WithMany(t => t.Subtypes)
.HasForeignKey(d => d.SupplierID);
}
}
EF关系应该在两个模型中定义,但只有一个 错误:列名“Category\u CategoryID”无效 这是EF生成的SQL:
SELECT
[Extent1].[SubtypeID] AS [SubtypeID],
[Extent1].[SupplierID] AS [SupplierID],
[Extent1].[Name] AS [Name],
[Extent1].[Description] AS [Description],
[Extent1].[Category_CategoryID] AS [Category_CategoryID] -- <- here 1
FROM [dbo].[Subtypes] AS [Extent1]
错误原因:从类别实体取消注释ICollection引用Sybtype以复制问题:
public sealed class Category
{
public Category()
{
Products = new List<Product>();
}
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public string Description { get; set; }
public byte[] Picture { get; set; }
public ICollection<Product> Products { get; set; }
// uncomment this line to reproduce the issue
// public ICollection<Subtype> Subtypes { get; set; } //<- here 2
}
但子类型不是指类别,而是指供应商:
public class Subtype : BaseModel
{
public override int Id { get; set; }
public int? SupplierID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
// Here is what is expected from EF (two side relation, but you've got only one)
// public virtual Category Category { get; set; }
public virtual Supplier Supplier { get; set; } //<- here 3
}
public sealed class Supplier
{
public int SupplierID { get; set; }
public ICollection<Subtype> Subtypes { get; set; }
}
流畅的API信息在这里也可能有帮助:
public class CategoryMap : EntityTypeConfiguration<Category>
{
public CategoryMap()
{
HasKey(t => t.CategoryID);
ToTable("Categories");
Property(t => t.CategoryID).HasColumnName("CategoryID");
}
}
public class SubtypeMap : EntityTypeConfiguration<Subtype>
{
public SubtypeMap()
{
HasKey(t => t.Id);
Property(t => t.SupplierID).HasColumnName("SupplierID");
HasOptional(t => t.Supplier)
.WithMany(t => t.Subtypes)
.HasForeignKey(d => d.SupplierID);
}
}
Customer\u CustomerId不是FK-它是一个导航属性,您可以将其包含在查询中。您的查询是什么样子的?codecustomer.Matches=wait ctx.mapsCustomerMatches.Wherem=>m.MatchingCustomerId==CustomerId.ToListSync;code ctx是我的上下文Customer\u CustomerId不是FK-它是一个导航属性,您可以将其包含在您的查询中ry.您的查询是什么样子的?codecustomer.Matches=wait ctx.MapCustomerMatches.Wherem=>m.MatchingCustomerId==CustomerID.toListSync;代码ctx是我的上下文