如何添加实体框架,控制台应用程序(包括图片) [英] How to add entity-framework to console application (images are included)
问题描述
我尝试添加实体框架,控制台应用程序:
我按下添加新项和
I try to add entity-framework to console application: I press "add new item" and
然后
then
然后
然后我添加的代码:
class Program
{
static void Main(string[] args)
{
try
{
Database1Entities db = new Database1Entities();
db.AddToTableTest(new TableTest { name = "name" });
db.SaveChanges();
int count = db.TableTest.Count();
int ui = 9 + 0;
}
catch (Exception e)
{
}
}
}
它给没有错误,但我没有看到任何改变。
我所描述的问题,更好地 href=\"http://stackoverflow.com/questions/13381738/i-cant-save-data-using-entity-framework\">
It gives no error, but I don't see any changes in database. I described the issue better here
推荐答案
我做你做了设置一个EF模型相同的步骤。你的 database.mdf
文件的复制到输出目录
设置为复制总是
,这意味着每次你打一次F5(构建或调试您的应用程序),该文件得到通过在项目中的空单所取代。
I did the same steps you did to setup a EF model. your database.mdf
file has the Copy to Output Directory
set to Copy always
, that means that every time you hit F5 (build or debug your app) the file is getting replaced by the empty one on your project.
更改复制到输出目录
mdf文件应该可以解决你的问题的属性窗口。
Changing the Copy to Output Directory
on the Properties window of the mdf file should solve your problem.
如果您使用复制如果新
你将要坚持上的数据库,直到您编辑数据库(MDF)本身的内容进行任何修改。
If you use Copy if newer
you are going to be persisting any modifications on the contents of the database until you edit the database (mdf) itself.
使用不要复制
任何变化mdf文件是不会让你的应用程序反射,可能会产生与EF问题。
With Do not copy
any change to the mdf file is not going to get reflected on your application and will probably generate problems with EF.
我建议这种情况下,你使用复制如果新
和MDF文件填写您的基本数据,因此你将它始终可用。
I recommend for this scenario that you use Copy if newer
and fill your basic data in the mdf file so you will have it always available.
这篇关于如何添加实体框架,控制台应用程序(包括图片)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!