如何添加实体框架到控制台应用程序(包括图像) [英] How to add entity-framework to console application (images are included)
问题描述
我尝试添加实体框架到控制台应用程序:
我按添加新项目和
然后
然后
然后我添加代码:
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)
{
}
}
}
它没有错误,但我在数据库中没有看到任何更改。
我更好地描述了问题这里
我做了与设置EF模型相同的步骤。您的 database.mdf
文件具有复制到输出目录
设置为始终复制
,这意味着每次你打F5(构建或调试你的应用程序)文件被替换为你的项目中的空文件。
更改复制到输出目录
在mdf文件的属性窗口应该可以解决你的问题。
如果使用复制如果较新的
您将要持续对数据库内容的任何修改,直到您编辑数据库(mdf)本身。
使用不要复制
对mdf文件的任何更改都不会反映在您的应用程序上,并且可能会导致EF的问题。
我建议在这种情况下,使用复制如果新的
并填写您的基本数据在mdf文件,所以你将永远可用。 / p>
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. 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.
Changing the Copy to Output Directory
on the Properties window of the mdf file should solve your problem.
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.
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.
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屋!