我如何对SaveChanges的使用Entity Framework的2个或多个表的加盟看法? [英] How do I SaveChanges on a joined view of 2 or more tables using Entity Framework?

查看:124
本文介绍了我如何对SaveChanges的使用Entity Framework的2个或多个表的加盟看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了得到与实体框架的做法,我创建一个C#的WinForms项目,我用了这个问题的答案在这个环节中的一个DataGridView的加入,显示表:




于一体的dataGridView两个实体




我已经寻找一个干净的方式来保存(到SQL Server数据库)在DataGridView所做的更改。是否有一个好,速度快,周期短,干净的方式?我已经看到了一些丑陋的尝试,即使他们的工作,我为他们的丑陋关闭。



通过一个表只(不加入),呼吁.SaveChanges工作正常



下面是类设置保持连接字段:

 公共类CustAndOrders 
{
// Customer表
公众的Int64 CUST_ID {搞定;组; }
公共字符串CUST_NAME {搞定;组; }
公众的DateTime Cust_BDay {搞定;组; }

// Order表
公众的Int64 ORDER_ID {搞定;组; }
公共小数ORDER_AMT {搞定;组; }
公众的DateTime ORDER_DATE {搞定;组; }

//的OrderDetail表
公众的Int64 {ITEM_NUMBER获得;组; }
公共小数Item_Amt {搞定;组; }
}

下面是显示在DataGridView中加入了信息的代码。显然,调用SaveChanges不能正常工作...但

  AWModel.TestJLEntities直流电; 

私人无效的button1_Click(对象发件人,EventArgs五)
{
直流=新AWModel.TestJLEntities();

VAR的查询=从C在dc.Customers
加盟澳在c.CustId dc.Orders等于o.CustId
在dc.OrderDetails加入OD上o.OrderId等于od.OrderId
排序依据o.OrderDate上升,c.CustId,o.OrderAmount
选择新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
};
变种的用户= query.ToList();
dataGridView1.DataSource =用户;
}

私人无效button2_Click(对象发件人,EventArgs五)
{

{
dc.SaveChanges();
}

{
MessageBox.Show(错误保存更改);
}
}


解决方案

EF没有看到你的变化,因为您的数据网格视图未绑定到EF实体,而是来自它填充对象的实体:

 选择新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:

 公共类CustAndOrders 
{
公众客户客户{搞定;组; }
治安秩序得到{;组; }
公众的OrderDetail的OrderDetail {搞定;组; }
}



,然后绑定到这些领域,即

  {结合Customer.CustId} 

或者,如果你不想改变你的绑定,再通过EF实体到CustAndOrders对象,然后在属性先手,并从实体设置:

 公共类CustAndOrders 
{
公众客户客户{搞定;组; }
治安秩序得到{;组; }
公众的OrderDetail的OrderDetail {搞定;组; }

// Customer表
公众的Int64 CUST_ID
{
得到
{返回Customer.CustId;}

{Customer.CustId =价值; }
}
......这样做对你的属性其余​​



然后查询看起来是这样的:

  VAR的查询=从C在dc.Customers 
加盟澳中dc.Orders在c.CustId等于o.CustId
在dc.OrderDetails加入OD上o.OrderId等于od.OrderId
排序依据o.OrderDate上升,c.CustId,o.OrderAmount
选择新CustAndOrders {客户= C,令= O,=的OrderDetail OD};


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

解决方案

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

这篇关于我如何对SaveChanges的使用Entity Framework的2个或多个表的加盟看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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