我已经创建了一个基于服务的数据库 folderName->Add New Item->Data->Service-based Database
文件到 WPF 应用程序中。然后我使用了 Database First
方法并创建了 PersonsModel.edmx
文件。这些操作完美执行。
数据读取正常:
using (PersonDBEntities db = new PersonDBEntities())
{
string dep = (db.Departament.FirstOrDefault()).DepName;//data can be read perfectly
string bureau = (db.Bureau.FirstOrDefault()).BureauName;//data can be read perfectly
}
但是无法插入数据(此代码在其他项目中运行良好):
using (PersonDBEntities db = new PersonDBEntities())
{
try
{
Departament dep = new Departament() { DepName = "NewDep" };
db.Departament.Add(dep);
db.SaveChanges();
}
catch(Exception ex)
{
string message = ex.Message;
}
}
有人知道为什么没有插入数据吗?
没有错误、异常,只是 EF 没有写入数据。
我已经 upload a project to github ,也许看看会很有趣:)。
创建表的 SQL 查询:
CREATE TABLE [dbo].[Departament] (
[IdDep] INT IDENTITY (1, 1) NOT NULL,
[DepName] NVARCHAR (100) NULL,
PRIMARY KEY CLUSTERED ([IdDep] ASC)
);
和 EF 模型类:
public partial class Departament
{
public Departament()
{
this.Person = new HashSet<Person>();
}
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int IdDep { get; set; }
public string DepName { get; set; }
public virtual ICollection<Person> Person { get; set; }
}
Visual Studio 2013 控制台输出的内容:
Started transaction at 18.09.2016 1:34:15 +03:00
INSERT [dbo].Departament VALUES (@0) SELECT [IdDep] FROM [dbo].[Departament] WHERE @@ROWCOUNT > 0 AND [IdDep] = scope_identity()
-- @0: '311' (Type = String, Size = 100)
-- Executing at 18.09.2016 1:34:15 +03:00
-- Completed in 79 ms with result: SqlDataReader
Committed transaction at 18.09.2016 1:34:15 +03:00
Closed connection at 18.09.2016 1:34:15 +03:00
如果我在 Visual Studio 2013 中将上述查询作为 SQL 数据库查询运行,那么它会完美地插入数据。
我按顺序尝试了以下方法:
//db.Departament.Add(dep);//not working
//db.Entry(dep).State = EntityState.Added;//not working
//db.Departament.Attach(dep);//not working
//db.Entry(dep).State = dep.IdDep == 0 ? EntityState.Added : EntityState.Modified;//not working
//db.Departament.Attach(dep);//not working
db.Entry(dep).State = EntityState.Modified;//not working
最佳答案
Special thanks to Magnus Montin as he has shown the mistake.我只是复制他的回答:
But since the INSERT statement is executed and you don't get any exception, how do you confirm that that the data is not inserted? Make sure that you are looking in the right database. That is the database that is located in the output folder of your executable (.exe), typically 'c:\yourprojectfolder\bin\Debug\' or 'c:\yourprojectfolder\bin\Release\'. This is where the data gets written by default.
我为避免这种行为所做的是我只写了我的数据库的绝对路径。现在看起来像这样:
<connectionStrings>
<add name="PersonDBEntities" connectionString="metadata=res://*/Model.PersonModel.csdl|res://*/Model.PersonModel.ssdl|res://*/Model.PersonModel.msl;provider=System.Data.SqlClient;provider connection string="
data source=(LocalDB)\v11.0;attachdbfilename=E:\Projects\WPF\EntityFrameworkCRUDDataGridWPF\EntityFrameworkCRUDDataGridWPF\AppData\EmployeeDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
一些附加信息:
当您运行您的应用程序(按 F5 或 CTRL+F5)时,Visual Studio 会将所有文件(包括数据库 MDF 文件)复制到 bin 文件夹。 所有更改都已完成复制到bin文件夹的数据库。
关于c# - EntityFramework 6.0.0.0 读取数据,但不插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39552207/