简体   繁体   English

在Entity Framework中使用context.SaveChanges()

[英]Using context.SaveChanges() in Entity Framework

Hi I'm using the Code first method within Entity Framework using Visual Studio 2012 Could someone tell me if I should add context.SaveChanges(); 嗨我正在使用Visual Studio 2012在Entity Framework中使用Code第一种方法有人可以告诉我是否应该添加context.SaveChanges(); at the end of my Seed method within the configuration.cs file in order to successfully update my tables? 在configuration.cs文件中的Seed方法结束时,为了成功更新我的表? I can make structural changes using update-database -verbose in the console, but this doesn't update my data. 我可以在控制台中使用update-database -verbose进行结构更改,但这不会更新我的数据。

edit: you do in fact not need to call SaveChanges . 编辑:你实际上不需要调用SaveChanges Thus, this must mean that after the Seed method has been called, a call to SaveChanges is automatically made. 因此,这必须意味着在调用Seed方法之后,会自动调用SaveChanges However, it is perhaps useful to note that you can call SaveChanges() within the Seed method if you want, which can be useful when dealing with foreign key relations. 但是,如果需要,可以注意可以Seed方法中调用SaveChanges() ,这在处理外键关系时很有用。

Initializers that provide Seed usually look like this inside... 提供Seed初始化程序通常看起来像这样......

// ...do something - prepare Db, Create etc.
Seed(context); // you override that
context.SaveChanges();

...just to clarify further. ......只是为了进一步澄清。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM