如何使用实体框架在2个或更多表的连接视图上保存更改?

本文关键字:连接 视图 保存更改 实体 何使用 框架 2个 | 更新日期: 2023-09-27 18:06:04

为了得到实践与实体框架,我正在创建一个c# WinForms项目,我用这个链接中的问题的答案来连接和显示表在一个datagridview:

一个dataGridView中的两个实体

我已经搜索了一个干净的方式来保存(SQL Server数据库)在datagridview所做的更改。有没有好的、快的、短的、干净的方法?我见过一些丑陋的尝试,即使他们成功了,我也被他们的丑陋所吸引。

只有一个表(没有连接),调用. savechanges工作得很好。

下面是用来保存连接字段的类:

public class CustAndOrders
{
    // Customer table
    public Int64 Cust_Id { get; set; }
    public string Cust_Name { get; set; }
    public DateTime Cust_BDay { get; set; }
    //Order table
    public Int64 Order_Id { get; set; }
    public decimal Order_Amt { get; set; }
    public DateTime Order_Date { get; set; }
    // OrderDetail table
    public Int64 Item_Number { get; set; }
    public decimal Item_Amt { get; set; }
}
下面是在datagridview中显示合并信息的代码。显然,SaveChanges调用还不能工作…
  AWModel.TestJLEntities dc;
    private void button1_Click(object sender, EventArgs e)
    {
        dc = new AWModel.TestJLEntities();
        var query = from c in dc.Customers
                    join o in dc.Orders on c.CustId equals o.CustId
                    join od in dc.OrderDetails on o.OrderId equals od.OrderId
                    orderby o.OrderDate ascending, c.CustId, o.OrderAmount
                    select new CustAndOrders
                    {
                        Cust_Id = c.CustId,
                        Cust_Name = c.CustName,
                        Cust_BDay = c.CustBday,
                        Order_Id = o.OrderId,
                        Order_Amt = o.OrderAmount,
                        Order_Date = o.OrderDate,
                        Item_Number = od.ItemNumber,
                        Item_Amt = od.ItemAmount
                    };
        var users = query.ToList();
        dataGridView1.DataSource = users;
    }
    private void button2_Click(object sender, EventArgs e)
    {
        try
        {
            dc.SaveChanges();
        }
        catch
        {
            MessageBox.Show("Error saving changes");
        }
    }

如何使用实体框架在2个或更多表的连接视图上保存更改?

EF看不到您的更改,因为您的数据网格视图没有绑定到EF实体,而是从它的实体填充的对象:

select new CustAndOrders
    {
        Cust_Id = c.CustId,
        Cust_Name = c.CustName,
        Cust_BDay = c.CustBday,
        Order_Id = o.OrderId,
        Order_Amt = o.OrderAmount,
        Order_Date = o.OrderDate,
        Item_Number = od.ItemNumber,
        Item_Amt = od.ItemAmount
    };

我能想到的一个解决方案是组合实体本身的cusstandorders:

public class CustAndOrders
{
    public Customer Customer { get; set; }
    public Order Order { get; set; }
    public OrderDetail OrderDetail { get; set; }
}

然后绑定到这些字段,即

{Binding Customer.CustId}

或者,如果你不想改变你的绑定,那么传递EF实体到CustAndOrders对象,并在你的属性中从实体中获取和设置:

public class CustAndOrders
{
    public Customer Customer { get; set; }
    public Order Order { get; set; }
    public OrderDetail OrderDetail { get; set; }
    // Customer table
    public Int64 Cust_Id
    {
        get
        { return Customer.CustId;}
        set
        { Customer.CustId = value; }
    }
... Do this for the rest of your properties

然后查询看起来像这样:

var query = from c in dc.Customers
                join o in dc.Orders on c.CustId equals o.CustId
                join od in dc.OrderDetails on o.OrderId equals od.OrderId
                orderby o.OrderDate ascending, c.CustId, o.OrderAmount
                select new CustAndOrders { Customer = c, Order = o, OrderDetail = od };