简体   繁体   English

如何使用实体框架在WPF c#中的两个表之间插​​入一对多关系的数据?

[英]How to insert data for one to many relationship between two tables in WPF c# using Entity Framework?

try
{
    foreach (IEnumerable<string> row in itemgridshow.Items)
    {
        var valueslist = row.ToList();

        Order_Item oi = new Order_Item();
        oi.Item_Description = valueslist[0].ToString();
        oi.Item_Copy = int.Parse(valueslist[1].ToString());
        oi.Item_Price = int.Parse(valueslist[2].ToString());
        oi.Item_Amount = int.Parse(valueslist[3].ToString());
        oi.Item_Image = valueslist[4].ToString();

        neworder.Order_Item.Add(oi);

        db.Order_Item.Add(oi);
    }

    db.Order_Master.Add(neworder);
    db.SaveChanges();
}

Order_Master is the primary table and Order_Item is the secondary table. Order_Master是主表, Order_Item是辅助表。 I want to insert Order_Master record & many Order_item details in a single save button operation. 我想在一个保存按钮操作中插入Order_Master记录和许多Order_item详细信息。

db is the context name. db是上下文名称。

For order_item data is pass through datagrid (itemgrid.items) in string array. 对于order_item数据通过字符串数组中的datagrid(itemgrid.items)传递。

All looks good and above code doesn't cause any errors, but data is not inserted into the database. 一切看起来不错,上面的代码不会引起任何错误,但是数据不会插入数据库中。 Please help me to fix this issue. 请帮助我解决此问题。

I don't see any logic issues with the code snippet above. 我看不到上面的代码片段有任何逻辑问题。

Try restructuring the code, encapsulating the context usage might uncover why the save isn't happening. 尝试重组代码,封装上下文用法可能会揭示为什么未进行保存的原因。

    public void SaveOrder(IEnumerable<string> rows)
    {
        // Create a new order and add items
        var neworder = new Order_Master();
        foreach (var row in rows)
        {
            var valueslist = row.ToList();
            var oi = new Order_Item();

            oi.Item_Description = valueslist[0].ToString();
            // Set more properties...

            // Add the item to the order
            neworder.Order_Item.Add(oi);
        }

        // Instance a context, add the order and save
        try
        {
            using (var db = new OrderContext())
            {
                db.Order_Master.Add(neworder);
                db.SaveChanges();
            }
        }
        catch (Exception e)
        {
            throw new Exception($"Failed to save to database: {e.Message}");
        }
    }

Entity Model should look something like this: 实体模型应如下所示:

    public class OrderContext : DbContext
    {
        public DbSet<Order_Master> Order_Master { get; set; }
        public DbSet<Order_Item> Order_Items { get; set; }
        // More entities...
    }

    public class Order_Item
    {
        public int Id { get; set; }

        public string Item_Description { get; set; }
        // More properties...
    }

    public class Order_Master
    {
        public Order_Master()
        {
            Order_Item = new List<Order_Item>();
        }

        public ICollection<Order_Item> Order_Item { get; set; }

        public int Id { get; set; }

        public string Order_Description { get; set; }
        // More properties...
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Entity Framework从具有一对多关系的两个表中选择所有相关数据? - How to select all related data from two tables having one-to-many relationship using Entity Framework? C#实体框架插入关系表? - C# Entity Framework insert relationship tables? C#中的实体框架一对多关系 - Entity framework one to many relationship in C# 配置实体框架两个表之间的多对多关系? - Configuring Entity Framework Many to Many relationship between two tables? 实体框架代码优先:如何在两个表之间创建一对多和一对一的关系? - Entity Framework Code First: How can I create a One-to-Many AND a One-to-One relationship between two tables? 实体框架更新一对一关系的两个表 - Entity Framework updating two tables with one to many relationship 如何根据两个表之间的关系从下拉列表中插入数据库值? 我正在使用 ASP.NET c# 实体框架,代码优先 - How can I insert in database values from dropdown list based on a relationship between 2 tables? I am using ASP.NET c# Entity framework, Code first 如何使用实体框架一次将数据插入两个表中 - how to insert data into two tables at a time using entity framework 如何使用与实体框架的多对多关系插入 - How to insert using many to many relationship with entity framework 实体框架如何在两个表的两列之间建立关系 - Entity Framework How to have relationship between two columns of two tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM