[英]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文件保留在其他位置而不是在生成文件夹位置,例如C
或D
驱动器,请在连接字符串中进行相应更改。 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.