本文介绍了使用Entity Framework将多个行插入表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与EF乐趣,都来未粘住的。

I’m having fun with EF and have come unstuck.

本来我使用LINQ的标准,基本上进入一些数据表中使用下面的代码位。

Originally I used the following bit of code using standard linq that essentially enters some data into a table.

ManagePreferencesDataContext manpref = new ManagePreferencesDataContext();

                tblManagePreference prefMemberID = new tblManagePreference();
                {
                    prefMemberID.Username = CreateUserWizard1.UserName;
                    prefMemberID.MemberID = tbxMemberID.Text.ToString();
                    prefMemberID.LocationID = tbxLocationID.Text.ToString();
                    prefMemberID.Preference = "MemberID";
                }

                tblManagePreference prefLocationID = new tblManagePreference();
                {
                    prefLocationID.Username = CreateUserWizard1.UserName;
                    prefLocationID.MemberID = tbxMemberID.Text.ToString();
                    prefLocationID.LocationID = tbxLocationID.Text.ToString();
                    prefLocationID.Preference = "LocationID";
                }


                List<tblManagePreference> ie = new List<tblManagePreference>();
                ie.Add(prefMemberID);
                ie.Add(prefLocationID);

                manpref.tblManagePreferences.InsertAllOnSubmit(ie);
                manpref.SubmitChanges();

现在,我已经尝试使用EF复制相同或类似的代码,并已完全到来未粘住的。

Now, I’ve tried to replicate the same, or similar code using the EF and have totally come unstuck.

我试图用列表和.AddTotblManagePreferences但收到的方法已过时添加一个新的对象到tblManagePreferences EntitySet的。考虑使用相关的对象集属性,而不是的。新增方法。

I tried to use the list and .AddTotblManagePreferences but receive a "Deprecated Method for adding a new object to the tblManagePreferences EntitySet. Consider using the .Add method of the associated ObjectSet property instead.

我看了一下简单对象集,但我真的不知道如何修改代码。

I’ve had a look at ObjectSet briefly but I’m not really sure how to amend the code.

    VDSORDAL.PDC_VDSOREntities manpref = new PDC_VDSOREntities();
        tblUserPreference prefMemberID = new tblUserPreference();

        {
            prefMemberID.Username = CreateUserWizard1.UserName;
            prefMemberID.MemberID = tbxMemberID.Text.ToString();
            prefMemberID.LocationID = tbxLocationID.Text.ToString();
            prefMemberID.ColumnName = "MemberID";


        }

        tblUserPreference prefLocationID = new tblUserPreference();
        {
            prefLocationID.Username = CreateUserWizard1.UserName;
            prefLocationID.MemberID = tbxMemberID.Text.ToString();
            prefLocationID.LocationID = tbxLocationID.Text.ToString();
            prefLocationID.ColumnName = "LocationID";
        }

    List<tblUserPreference> ie = new List<tblUserPreference>();
        ie.Add(prefMemberID);
        ie.Add(prefLocationID);


        manpref.AddObject(PDC_VDSOREntities,ie);
        manpref.SaveChanges();

如果任何人使用的东西沿着这些路线之前或可以点我在正确的方向,我会将感激不尽。

If anyone has used something along these lines before or could point me in the right direction, I’d be most grateful.

虽然热心,我不禁感慨厚作为目前猪大便。

Although enthusiastic, I can't help but feeling thick as pig poo at the moment.

推荐答案

我想您已经重新创建你的模型中的实体框架设计师?这应该已经创建了ObjectContext中从一个特定类派生到模型。按照约定,该结束的实体

I presume you have recreated your model in the Entity Framework designer? This should have created a class deriving from ObjectContext specific to your model. By convention, this ends in "Entities".

有一个在你的 ManagePreferencesEntities 实例 - 无论你是调用。它应该具有在对应于表的实体它, tblManagePreferences 也许一个属性。这是针对特定的表对象集实例,它有您可以使用实体添加到 ADDOBJECT 方法。该表

Have a look at your ManagePreferencesEntities instance - or whatever yours is called. It should have a property on it corresponding to the table for your entities, tblManagePreferences perhaps. This is the ObjectSet instance for that specific table, and it has an AddObject method that you can use to add entities into that table.

尝试,而不是过去的5行左右的代码如下:

Try this instead of the last 5 or so lines of code:

manpref.tblUserPreferences.AddObject(prefMemberId);
manpref.tblUserPreferences.AddObject(prefLocationId);
manpref.SaveChanges();

默认情况下对象集不支持添加的东西名单,但可以很容易地创建自己的扩展方法

By default ObjectSet doesn't support adding lists of things, but it's easy to create your own extension method:

public static class ObjectSetExtensions
{
     public static void AddObjects<T>(this ObjectSet<T> objectSet, IEnumerable<T> objects)
     {
         foreach (var item in objects)
         {
            objectSet.AddObject(item);
         }
     }
}

这篇关于使用Entity Framework将多个行插入表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 04:41