如何使用Entity Framework在2个或更多表的连接视图上保存更改? [英] How do I SaveChanges on a joined view of 2 or more tables using Entity Framework?

查看:139
本文介绍了如何使用Entity Framework在2个或更多表的连接视图上保存更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In order to get practice with Entity Framework, I am creating a C# WinForms project, and I used the answer to the question in this link to join and display tables in a datagridview:


一个dataGridView中的两个实体

我已经搜索了一个干净的方式来保存(对SQL Server数据库)datagridview中所做的更改。有没有一个好,快,短,干净的方法?我看到一些丑陋的尝试,即使他们工作,我被关闭了他们的丑陋。

I have searched for a clean way to save (to the SQL Server database) the changes made in the datagridview. Is there a good, fast, short, clean way? I have seen some ugly attempts, and even if they work, I am turned off by their ugliness.

只有一个表(不加入),调用.SaveChanges工作正常

With one table only (no joins), calling .SaveChanges works fine.

以下是设置为保存加入字段的类:

Here is the class set up to hold the joined fields:

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调用不起作用,但是

Here is the code to display the joined info in a datagridview. Obviously, the SaveChanges call does not work...yet

  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");
        }
    }


推荐答案

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

EF is not seeing your changes because your data grid view is not bound to the EF entities, but rather an object populated from it's entities:

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
    };

我可以想到的一个解决方案是组合实体本身的CustAndOrders:

One solution I can think of is to compose CustAndOrders of the entities themselves:

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

然后绑定到这些字段,即

And then bind to those fields i.e.

{Binding Customer.CustId}

或者如果您不想更改绑定,则将EF实体传递到CustAndOrders对象,并且从属性中获取和设置属性:

Or if you don't want to change your bindings then pass the EF entities into the CustAndOrders object and in your properties just get and set from the entities:

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

然后查询看起来像这样:

And then query looks like this:

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 };

这篇关于如何使用Entity Framework在2个或更多表的连接视图上保存更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆