实体框架4.1不会向SQL Server Express数据库添加任何行 [英] Entity Framework 4.1 is not adding any rows to SQL Server Express database
问题描述
使用带有SQL Server Express .mdf
数据库的Entity Framework 4.1。
Working with Entity Framework 4.1 with a SQL Server Express .mdf
database .
出于测试目的,我是试图在WPF应用程序中使用实体模型在SQL Server Express数据库上执行CRUD操作。
For test purpose I am trying to perform CRUD operations on SQL Server Express database using Entity Model in a WPF application .
我是这个概念的新手,我按照视频教程进行了编码,并根据
I am new to this concept, and I followed the video tutorial and done coding according that
我创建了一个非常简单的表的实体模型。我在cs文件中编写了简单的代码,使用以下代码执行向数据库添加一行的操作
I created Entity model of single very simple table. And I wrote simple code in cs file to perform adding one row to the database using following code
testEntities db = new testEntities();
TestTable tb = new TestTable();
tb.Name = txtName.Text;
tb.Email = txtMail.Text;
db.TestTables.AddObject(tb);
db.SaveChanges();
但是如果我返回数据库,则不会添加任何数据。请告诉我这里出了什么问题??
But if I go back check the database no data is added. Please tell me whats going wrong here ??
这是我的连接字符串
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="testEntities"
connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\test.mdf;integrated security=True;connect timeout=30;user instance=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
推荐答案
整个用户实例和AttachDbFileName = 方法有缺陷-充其量! Visual Studio将围绕 .mdf
文件(从 DataDirectory
复制到正在运行的应用程序的输出目录)和最有可能,您的 INSERT
可以正常工作-但是您只是在查看错误的 .mdf
文件!
The whole User Instance and AttachDbFileName= approach is flawed - at best! Visual Studio will be copying around the .mdf
file (from the DataDirectory
to the output directory of your running app) and most likely, your INSERT
works just fine - but you're just looking at the wrong .mdf
file in the end!
如果您要坚持使用这种方法,请尝试在 .SaveChanges()
调用上设置一个断点-然后使用SQL Server Mgmt Studio Express检查正在运行的应用程序目录中的 .mdf
文件-我几乎可以确定您的数据在那里。
If you want to stick with this approach, then try putting a breakpoint on the .SaveChanges()
call - and then inspect the .mdf
file in the running app's directory with SQL Server Mgmt Studio Express - I'm almost certain your data is there.
我认为真正的解决方案是
-
安装SQL Server Express(而且您已经完成了此操作)
install SQL Server Express (and you've already done that anyway)
安装SQL Server Management Studio Express
install SQL Server Management Studio Express
在 SSMS Express 中创建数据库,并为其指定一个逻辑名称(例如 TestDatabase
)
create your database in SSMS Express, give it a logical name (e.g. TestDatabase
)
使用其逻辑数据库名称(在服务器上创建时赋予该名称)连接到它-并且不要弄乱物理数据库文件和用户实例。在这种情况下,您的连接字符串将类似于:
connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:
Data Source=.\\SQLEXPRESS;Database=TestDatabase;Integrated Security=True
其他所有内容与以前完全相同。 ..
and everything else is exactly the same as before...
这篇关于实体框架4.1不会向SQL Server Express数据库添加任何行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!