EF6(非 EF 核心)使用 Navigation 属性 而不是 Linq 连接
EF6(not EF core) using Navigation property instead of Linq joins
我有两个表需要联接和筛选。订单和客户。我使用 DB 中的 EF Code First 生成了这些 类。
为表格生成 类 -
订单
[Table("Orders")]
public partial class Orders
{
[Key]
[StringLength(17)]
public string OrderID { get; set; }
public int ShipToCustomerID { get; set; }
//Navigation Property
public Customer Customer { get; set; }
}
客户
[Table("Customer")]
public partial class Customer
{
public int CustomerID { get; set; }
public string AccountNumber { get; set; }
//Navigation prop
public int ShipToCustomerID { get; set; } (not a part of the table, just attempting to get the navigation work)
public Orders Order { get; set; }
}
方法一:
LINQ 连接
using (var context = new OrderDetailsGeneral1())
{
var data = (from p in context.Orders
join q in context.Customers
on p.ShipToCustomerID equals q.CustomerID
where p.OrderID == "7150615"
select new
{
OrderID = p.OrderID,
CustomerID = q.AccountNumber
}
);
var orders = data.ToList();
return Json(orders);
}
这很好用,我得到了以下输出 -
[
{
"OrderID": "7150615",
"CustomerID": "23320347 "
}
]
方法二:
我读到使用导航属性比使用连接更好,这就是我尝试这样做的原因,因为我将导航属性添加到上面的 类。
我尝试了很多方法将它们 link 放在一起。其中一个是这里提到的方式,我遇到了一堆错误。
它会尝试将 Customers.CustomerID 映射到 Orders.OrderID 而不是 Orders.ShipToCustomerID。
实现此目标的最佳方法是什么?我很难弄清楚 link 将这个外键 (Customers.CustomerID) 转换为非 primary/alternate 键 (Orders.ShipToCustomerID)
你必须修复你的 类
[Table("Orders")]
public partial class Order
{
[Key]
[StringLength(17)]
public string OrderID { get; set; }
public int ShipToCustomerID { get; set; }
[ForeignKey(nameof(ShipToCustomerID))]
[InverseProperty("Orders")]
public virtual Customer Customer { get; set; }
}
[Table("Customer")]
public partial class Customer
{
[Key]
public int CustomerID { get; set; }
public string AccountNumber { get; set; }
[InverseProperty(nameof(Order.Customer))]
public virtual ICollection<Order> Orders { get; set; }
}
我有两个表需要联接和筛选。订单和客户。我使用 DB 中的 EF Code First 生成了这些 类。
为表格生成 类 -
订单
[Table("Orders")]
public partial class Orders
{
[Key]
[StringLength(17)]
public string OrderID { get; set; }
public int ShipToCustomerID { get; set; }
//Navigation Property
public Customer Customer { get; set; }
}
客户
[Table("Customer")]
public partial class Customer
{
public int CustomerID { get; set; }
public string AccountNumber { get; set; }
//Navigation prop
public int ShipToCustomerID { get; set; } (not a part of the table, just attempting to get the navigation work)
public Orders Order { get; set; }
}
方法一:
LINQ 连接
using (var context = new OrderDetailsGeneral1())
{
var data = (from p in context.Orders
join q in context.Customers
on p.ShipToCustomerID equals q.CustomerID
where p.OrderID == "7150615"
select new
{
OrderID = p.OrderID,
CustomerID = q.AccountNumber
}
);
var orders = data.ToList();
return Json(orders);
}
这很好用,我得到了以下输出 -
[
{
"OrderID": "7150615",
"CustomerID": "23320347 "
}
]
方法二: 我读到使用导航属性比使用连接更好,这就是我尝试这样做的原因,因为我将导航属性添加到上面的 类。
我尝试了很多方法将它们 link 放在一起。其中一个是这里提到的方式,我遇到了一堆错误。
它会尝试将 Customers.CustomerID 映射到 Orders.OrderID 而不是 Orders.ShipToCustomerID。
实现此目标的最佳方法是什么?我很难弄清楚 link 将这个外键 (Customers.CustomerID) 转换为非 primary/alternate 键 (Orders.ShipToCustomerID)
你必须修复你的 类
[Table("Orders")]
public partial class Order
{
[Key]
[StringLength(17)]
public string OrderID { get; set; }
public int ShipToCustomerID { get; set; }
[ForeignKey(nameof(ShipToCustomerID))]
[InverseProperty("Orders")]
public virtual Customer Customer { get; set; }
}
[Table("Customer")]
public partial class Customer
{
[Key]
public int CustomerID { get; set; }
public string AccountNumber { get; set; }
[InverseProperty(nameof(Order.Customer))]
public virtual ICollection<Order> Orders { get; set; }
}