[英]How do I overwrite a foreign key column in Entity Framework Core 6?
我正在使用 Entity Framework Core 6。我在 SQL 服务器中有 3 个表, order
, orderitem
和product
。
order
表的主键是id
, orderitem
的外键是orderid
和product_id
。 一切都设置正确,但是,EF Core 6 生成的查询没有使用product_id
,而是继续使用productId
作为 map 到product
表的外键。 覆盖它的最佳位置在哪里?
这是错误部分:
FROM [ShipEngineOrderItems] AS [s0]
LEFT JOIN [Products] AS [p] ON [s0].[ProductId] = [p].[Id]
这是完整的错误消息,上面的 select 语句使用正确的列名
Microsoft.EntityFrameworkCore.Database.Command[20102]
执行 DbCommand (90ms) 失败 [Parameters=[@__orderId_0='?' (DbType = Int32)],CommandType='Text',CommandTimeout='30']
SELECT [s].[id], [s].[order_date], [t].[PId], [t].[ProductName], [t].[Price], [t].[id], [ t].[Id0]
FROM [ShipEngineOrders] AS [s]
左连接(
SELECT [s0].[product_id] AS [PId], [p].[WarehouseSku] AS [ProductName], [s0].[price] AS [Price], [s0].[id], [p].[ Id] AS [Id0], [s0].[orderId]
从 [ShipEngineOrderItems] 作为 [s0]
左连接 [Products] AS [p] ON [s0].[ProductId] = [p].[Id]
) AS [t] ON [s].[id] = [t].[orderId]
WHERE [s].[id] = @__orderId_0
按 [s].[id]、[t].[id] 排序失败:Microsoft.EntityFrameworkCore.Query[10100]
迭代上下文类型“Core.Data.ApplicationDbContext”的查询结果时发生异常。
Microsoft.Data.SqlClient.SqlException (0x80131904):列名“ProductId”无效。
这是我的代码:
public partial class Order
{
public int id { get; set; }
public DateTime order_date { get; set; }
public virtual ICollection<OrderItem> OrderItems { get; set; }
}
public partial class OrderItem
{
public int id { get; set; }
[Column(TypeName = "decimal(18,2")]
public decimal price { get; set; }
public int orderId { get; set; } // navigation property
public virtual Order Order { get; set; }
[Column("product_id")]
public int product_id { get; set; } // navigation property
public virtual Product Product { get; set; }
}
public partial class Product
{
public int Id { get; set; }
public string? WarehouseSku { get; set; }
}
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext() { }
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) :
base(options) { }
public virtual DbSet<Order> Order { get; set; }
public virtual DbSet<OrderItem> OrderItem { get; set; }
public virtual DbSet<Product> Product { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>()
.ToTable("ShipEngineOrders", t => t.ExcludeFromMigrations());
//modelBuilder.Entity<OrderItem>()
// .Property(i => i.product_id).HasColumnName("product_id");
modelBuilder.Entity<OrderItem>()
.ToTable("ShipEngineOrderItems", t => t.ExcludeFromMigrations());
modelBuilder.Entity<Product>()
.ToTable("Products", t => t.ExcludeFromMigrations());
}
}
非常感谢您的建议
使用 Fluent API 作为参考。 添加这两个参考
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
public partial class OrderItem
{
public int id { get; set; }
[Column(TypeName = "decimal(18,2")]
public decimal price { get; set; }
[ForeignKey("Order")] // add foreign key reference here
public int orderId { get; set; } // navigation property
public virtual Order Order { get; set; }
[Column("product_id")]
[ForeignKey("Product")] //This is the foreign key reference
public int product_id { get; set; } // navigation property
public virtual Product Product { get; set; }
public virtual Order Order{ get; set; }
}
此外,尝试实践约定优于配置。 如果您的数据库设计使用 ProductId 而不是 product_id。 外键配置是自动设置的。这同样适用于 OrderId 而不是 orderId
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.