How do I SaveChanges on a joined view of 2 or more tables using Entity Framework?

拜拜、爱过 提交于 2019-12-07 19:34:24

问题


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:

Two entities in one 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.

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

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

回答1:


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

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}

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


来源:https://stackoverflow.com/questions/23961071/how-do-i-savechanges-on-a-joined-view-of-2-or-more-tables-using-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!