[英]Inserting multiple rows into a table using Entity Framework
I'm having fun with EF and have come unstuck. 我和EF玩得很开心。
Originally I used the following bit of code using standard linq that essentially enters some data into a table. 最初我使用标准linq的以下代码,基本上将一些数据输入表中。
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();
Now, I've tried to replicate the same, or similar code using the EF and have totally come unstuck. 现在,我尝试使用EF复制相同或相似的代码并完全解开。
I tried to use the list and .AddTotblManagePreferences but receive a “Deprecated Method for adding a new object to the tblManagePreferences EntitySet. 我尝试使用列表和.AddTotblManagePreferences,但收到一个“不推荐使用的方法,用于向tblManagePreferences EntitySet添加新对象。 Consider using the .Add method of the associated ObjectSet property instead.
请考虑使用关联的ObjectSet属性的.Add方法。
I've had a look at ObjectSet briefly but I'm not really sure how to amend the code. 我已经简要介绍了ObjectSet,但我不确定如何修改代码。
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. 虽然热情,但我还是忍不住感觉像猪屎一样浓浓。
I presume you have recreated your model in the Entity Framework designer? 我假设您已在Entity Framework设计器中重新创建了模型? This should have created a class deriving from ObjectContext specific to your model.
这应该创建一个派生自特定于您的模型的ObjectContext的类。 By convention, this ends in "Entities".
按照惯例,这以“实体”结束。
Have a look at your ManagePreferencesEntities
instance - or whatever yours is called. 看看你的
ManagePreferencesEntities
实例 - 或者你调用的任何实例。 It should have a property on it corresponding to the table for your entities, tblManagePreferences
perhaps. 它应该有一个属性,对应于你的实体的表,也许是
tblManagePreferences
。 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. 这是该特定表的
ObjectSet
实例,它有一个AddObject
方法,可用于将实体添加到该表中。
Try this instead of the last 5 or so lines of code: 试试这个而不是最后5行左右的代码:
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: 默认情况下,ObjectSet不支持添加事物列表,但是创建自己的扩展方法很容易:
public static class ObjectSetExtensions
{
public static void AddObjects<T>(this ObjectSet<T> objectSet, IEnumerable<T> objects)
{
foreach (var item in objects)
{
objectSet.AddObject(item);
}
}
}
I'm using entity framework 5 now. 我现在正在使用实体框架5。
When you use: 当你使用:
objectcontext.Add(yourobject1);
objectcontext.Add(yourobject2);
objectcontext.SaveChanges();
then only last of your objects will be inserted into database. 然后只有最后一个对象将插入到数据库中。
But if you will use: 但是,如果你将使用:
objectcontext.Entry(yourobject1).State = System.Data.EntityState.Added;
objectcontext.Entry(yourobject2).State = System.Data.EntityState.Added;
objectcontext.SaveChanges();
Then all of your objects will be added. 然后将添加所有对象。
Try the following code: 请尝试以下代码:
manPref.tblManagePreference.Add(prefMemberID);
manPref.tblManagePreference.Add(prefLocationID);
manPref.SaveChanges();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.