[英]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 然后
then 然后
then I added code: 然后我添加了代码:
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)
{
}
}
}
It gives no error, but I don't see any changes in database. 它没有出错,但我没有看到数据库有任何变化。 I described the issue better here 我在这里更好地描述了这个问题
I did the same steps you did to setup a EF model. 我做了与设置EF模型相同的步骤。 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. 您的database.mdf
文件将“ Copy to Output Directory
设置为Copy always
,这意味着每次按F5(构建或调试应用程序)时,文件都会被项目中的空文件替换。
Changing the Copy to Output Directory
on the Properties window of the mdf file should solve your problem. 在mdf文件的“属性”窗口中更改“ Copy to Output Directory
”可以解决您的问题。
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. 如果您使用Copy if newer
,则在编辑数据库(mdf)本身之前,将继续对数据库的内容进行任何修改。
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. 使用Do not copy
任何更改到mdf文件不会反映在您的应用程序上,并可能会产生EF的问题。
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. 我建议在这种情况下使用Copy if newer
for Copy if newer
并在mdf文件中填充基本数据,这样您就可以随时使用它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.