开发者

How can I add a new item to a EntitySet Using EF4?

开发者 https://www.devze.com 2023-03-13 05:37 出处:网络
I tried to add some new items to entityset by creating new instance and add it using AddTo method then I called SaveChanges() method to update my database but no new recode had been added.

I tried to add some new items to entityset by creating new instance and add it using AddTo method then I called SaveChanges() method to update my database but no new recode had been added.

foreach (var newitem in newItemsList)
{
      if (newitem.SomeValue > 0)
      {
          dbcontext.AddToMyEntityType(newitem);
      }
}
dbcontext.SaveChanges();

but when I used MyEntityType.CreateMyEntityType() method all new items are saved into my database

foreach (var newitem in newItemsList)
{
      if (newitem.SomeValue > 0)
      {
          MyEntityType _newEntity = MyEntityType.CreateMyEntityType();//I passed the required parameters
          dbcontext.AddToMyEntityType(_newEntity);
      }
}
dbcontext.SaveChanges();

why????

should I use Create method or there is another ways to do that?

Edit

I forget to mention that newitem have a navigation property(foreign key for a parent tab开发者_JS百科le) and I set this property with an a correct parent ID


There must be a difference between newItem and _newEntity on property level. CreateMyEntityType(...) doesn't do more then new MyEntityType and setting the properties you pass in. CreateMyEntityType(...) encourages to set properties for all required fields. Perhaps you set a required property (like a string) which isn't set in newItem. SaveChanges in your first example throws actually an exception because of the missing required field but you hid this exception (by try { ... } catch {} or something). Check in the debugger if SaveChanges passes without exception.

A hypothesis.


foreach (var newitem in newItemsList)
{
      if (newitem.SomeValue > 0)
      {
          dbcontext.AddObject("entity set name", newitem);
      }
}
dbcontext.SaveChanges();

or

var entitySet = dbcontext.CreateObjectSet<item type>();
foreach (var newitem in newItemsList)
{
      if (newitem.SomeValue > 0)
      {
          entitySet.AddObject(newitem);
      }
}
dbcontext.SaveChanges();


In first part you are not initializing the object of entity set This will work if you initialize the object of entityset by doing some thing like this

 foreach (var newitem in newItemsList)
 {
  if (newitem.SomeValue > 0)
  {  
      MyEntityType obj = new MyEntityType
      {
       value1 = newitem.somevalue,
       value2 = newitem.someothervalue
      };

      myContext.AddToMyEntityType(obj);
  }
 }
  dbcontext.SaveChanges();

In your seceod part you have deifned the enititySet in which u wany add new object and also initilazes the object of the entiity by this

 MyEntityType _newEntity = MyEntityType.CreateMyEntityType();

  myContext.AddToMyEntityType(_newEntity);

Here you are telling that add _newEntity to my MyEntityType AddToMyEntityType is a depreceated method for adding a new object to MyEntityType Entity Set..

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号