开发者

SaveChanges doesn't work with ADO.NET Entity Framework

I am using ADO.NET Entity Framework data model for my ASP.NET MVC project. I am trying to insert into multiple tables and called .SaveChanges() method. It doesn't throw any exception and doesn't insert into these tables.

What could be wrong h开发者_高级运维ere?

Object1 obj1 = new  Object1();
obj1.TID = 300; //Auto generated but I am assigning the value. 
obj1.TypeID = 1;

Object2 obj2 = new Object2();

obj2.TID = 300;
obj2.StartDT = DateTime.Now;

db.AddToObject1(obj1);
db.AddToObject2(obj2);

db.SaveChanges();
db.AcceptAllChanges();


Have you try save changes for each object like:

db.AddToObject1(obj1);
db.SaveChanges();

db.AddToObject2(obj2);
db.SaveChanges();


Try this. You first have to add the object to the context and then modify their properties:

Object1 obj1 = new Object1();
Object2 obj2 = new Object2();
db.AddToObject1(obj1);
db.AddToObject2(obj2);


obj1.TID = 300; //Auto generated but I am assigning the value. 
obj1.TypeID = 1;


obj2.TID = 300;
obj2.StartDT = DateTime.Now;


db.SaveChanges();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜