简体   繁体   English

使用实体框架保存更改后,本地数据库不更新

[英]Local database not update after SaveChanges with entity framework

I would like to create a winform application using a local database (.mdf) and entity framework. 我想使用本地数据库(.mdf)和实体框架创建Winform应用程序。

I'have created the database and the model. 我已经创建了数据库和模型。 When I execute this code: 当我执行此代码时:

using (var context = new testEntity())
{
    CLIENTS c = new CLIENTS();
    c.Name = "name";
    c.Age = 10;
    context.CLIENTS.Add(c);
    context.SaveChanges();
}

a client is created in the database and I can get it with this code: 在数据库中创建了一个客户端,我可以使用以下代码获取它:

using (var context = new testEntity())
{
    List<CLIENTS> clients = context.CLIENTS.ToList();
    //break point here
}

But the added client is not on the .mdf file after closing the program. 但是关闭程序后,添加的客户端不在.mdf文件上。 If I add some lines in the .mdf file, I can display them but not the opposite. 如果在.mdf文件中添加一些行,则可以显示它们,但不能显示相反的行。

If someone can help me, thank you 如果有人可以帮助我,谢谢

check the Copy to Output Directory of DB. 检查Copy to Output Directory数据库的Copy to Output Directory
change the Copy to Output Directory property of the database file to Copy if newer. 将数据库文件的“ Copy to Output Directory属性更改为“ Copy if newer. .

.mdf file lost changes every time when you rebuild your code: Solution: Keep your .mdf file other location rather than build folder location, eg C or D drive, make respective changes in connection string. 每次重建代码时, .mdf文件都会丢失更改:解决方案:将.mdf文件保留在其他位置而不是在生成文件夹位置,例如CD驱动器,请在连接字符串中进行相应更改。 You can also refer Pranav's answer. 您也可以参考Pranav的答案。

Take a look of your connection string in the config file (App.config) When you generate a .edmx from a database, it makes a copy of your database in the debug folder(if you run on debug) if you clicked on yes when it was asked. 查看配置文件(App.config)中的连接字符串。从数据库生成.edmx时,如果在以下情况下单击“是”,它将在debug文件夹(如果在debug中运行)复制数据库:有人问。 So as it was say, every time you rebuild, the content of the copy of your database is cleared. 据说,每次重建时,都会清除数据库副本的内容。

You have to change the connection string to point to your initial dabatase. 您必须更改连接字符串以指向您的初始dabatase。

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

相关问题 无法使用Entity Framework中的savechanges()更新数据库 - Unable to update database using savechanges() in Entity Framework 实体框架 SaveChanges() 不更新数据库 - Entity Framework SaveChanges() not updating the database 使用Entity Framework 1(EF1)更新-在saveChanges()上 - Update using Entity Framework 1 (EF1) - on saveChanges() SaveChanges实体框架后延迟加载不起作用 - Lazy Loading Not Working After SaveChanges Entity Framework WPF-如何在Entity Framework DbContext saveChanges()之后自动更新ComboBox项? - WPF - How to automatically update ComboBox items after Entity Framework DbContext saveChanges()? 在状态为EntityState的SaveChanges()之后,实体框架不会更新记录。 - Entity Framework won't update record after SaveChanges() with state=EntityState.Modified 保存更改后,如何首先使用实体​​框架代码从数据库中检索对象集合的ID - How to retrieve Ids of a collection of objects after savechanges from database using Entity Framework code first 模型更改后更新数据库 - 实体框架7 - Update Database after Model Changes - Entity Framework 7 更新数据库后出现实体框架错误 - entity-framework error after update database 实体框架6中“更新数据库”之后发生错误 - Error after 'update-database' in Entity Framework 6
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM