context.SaveChanges()不保存数据库中的数据 [英] context.SaveChanges() not persisting data in database

查看:106
本文介绍了context.SaveChanges()不保存数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在开发一个MVC应用程序。



当我调用context.SaveChanges来更新特定记录时。更新未在数据库中注册。我也没有得到任何运行时错误。所有通知都是我的记录没有更新。我仍然看到相同的价值观。插入功能完美无缺。



似乎有什么问题?



这里是我的代码



公开招生更新(int stuid)

{

VDData.VidyaDaanEntities context = new VDData.VidyaDaanEntities();

VDData.Student_Master studentmaster = new VDData.Student_Master();



studentmaster.Student_ID = stuid; //这里我会得到身份证。

studentmaster.Student_First_Name = this.FirstName;

studentmaster.Student_Middle_Name = this.MiddleName;

studentmaster .Student_Last_Name = this.LastName;

studentmaster.Student_Address_1 = this.Address;

studentmaster.Student_Address_2 = this.Address2;

studentmaster.Student_City = this.City;

studentmaster.Student_State = this.State;

studentmaster.Student_Pin_Code = this.Pincode;



context.SaveChanges(); //这里它不会给出任何错误。它运行得非常好。

}

解决方案

您好,



请阅读这篇文章:

http://msdn.microsoft.com/en-us /library/bb896271.aspx [ ^ ]

http://stackoverflow.com/questions/8056761/ context-savechanges-not-working [ ^ ]



您的对象可能与上下文对象分离。



再见。


请注意,您可以在模型中创建类型的对象,而无需将其添加到数据存储中。你实际上就是这样做的。

在模型中你有一个 VDData.Student_Master 的集合。它应该是 context.Student_Master s - 但这取决于您使用的命名约定。此集合代表该表。如果要保留对象,则必须将该对象添加到集合中。如果你想更新一个现有的,你必须从Linq到EF的集合中获取它。只需设置对象的id就不会在数据存储中更改它,如果它在集合之外则更少。



请参阅:http://www.dotnetcurry.com/ShowArticle.aspx?ID=619 [ ^ ]

这与MVC无关。


您好b $ b

我认为在更新的情况下,您应首先阅读学生大师而不是创建新对象。



因此您不应该执行以下操作:

 VDData.Student_Master studentmaster = new VDData.Student_Master(); 





相反,你应该首先从上下文中读取它:

 VDData.Student_Master studentmaster =使用Student_ID从上下文中读取; 





其余代码可能保持不变。



希望这个会帮助你。

Hello, Im working on a MVC app.

When I call context.SaveChanges to update a specific records. The update is not registered in the database. I do not get any runtime error either. All in notice is that my Records is not updated. I still see the same values. Insert Functionality work Perfectly.

What seems to be the problem?

here is my code

public Admission Update(int stuid)
{
VDData.VidyaDaanEntities context = new VDData.VidyaDaanEntities();
VDData.Student_Master studentmaster = new VDData.Student_Master();

studentmaster.Student_ID = stuid; //here i will get the ID.
studentmaster.Student_First_Name = this.FirstName;
studentmaster.Student_Middle_Name = this.MiddleName;
studentmaster.Student_Last_Name = this.LastName;
studentmaster.Student_Address_1 = this.Address;
studentmaster.Student_Address_2 = this.Address2;
studentmaster.Student_City = this.City;
studentmaster.Student_State = this.State;
studentmaster.Student_Pin_Code = this.Pincode;

context.SaveChanges(); // here it wont give any kind of error. it runs sucessfuly.
}

解决方案

Hello,

Please read this articles:
http://msdn.microsoft.com/en-us/library/bb896271.aspx[^]
http://stackoverflow.com/questions/8056761/context-savechanges-not-working[^]

Your object is probably detached from the context object.

Bye.


Be aware, that you can create an object of a type in your model, without adding it to the data store. And you are actually doing this.
In the model you have a collection of VDData.Student_Master. It should be context.Student_Masters - but it depends on the naming convention you have used. This collection represents the table. You have to add the object to the collection if you want to persist it. And if you want to update an existing one, you have to get it from the collection with Linq to EF's. Just by setting the id of the object won't change it in the data store, even less if it is outside the collection.

See: http://www.dotnetcurry.com/ShowArticle.aspx?ID=619[^]
And this has nothing to do with MVC.


Hi
I think in case of update you should read the student master first rather than creating a new object.

so you should not do the following:

VDData.Student_Master studentmaster = new VDData.Student_Master();



Rather you should read it first from context like:

VDData.Student_Master studentmaster = Read from context using Student_ID ;



rest of your code may remain same.

Hope this will help you.


这篇关于context.SaveChanges()不保存数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆