EF不能将SaveChanges保存到db [英] EF can't SaveChanges to db

查看:164
本文介绍了EF不能将SaveChanges保存到db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试创建一个方程实体,并将其添加到SQL Server数据库,我没有得到任何错误,我的代码,但我没有看到任何更改db。

I try to create an Equation entity and to add it to SQL Server db, i do not get any errors in my code but i do not see any changes in the db.

我在visual studio中检查服务器资源管理器中的db,并且没有添加值。当我在我的项目中为它创建ef数据模型时,数据库被添加到项目中,所以visual studio在我的项目中创建了db的副本。但我不知道为什么它在项目目录和/ bind / Debug目录中创建了两个副本。

I check the db in the server explorer in visual studio, and there are not added values. The db were added to the project when i was creating the ef data model for it in my project so the visual studio made a copy of the db in my project. But i don't know why it created two copies, in the project directory and /bind/Debug directory.

实体类:

public partial class Equation
    {
        public Equation()
        {
            this.Results = new HashSet<Result>();
        }

        public int id { get; set; }
        public double a { get; set; }
        public double b { get; set; }
        public double c { get; set; }

        public virtual ICollection<Result> Results { get; set; }
    }

id是主键并具有标识(1,1)

id is primary key and has identity(1,1)

我的代码:

private EquationDBEntities dbcontext = new EquationDBEntities();
Equation eq = new Equation()
{
    a = 1, b = 2, c = 3
};

dbcontext.Equations.Add(eq);
dbcontext.SaveChanges();

DbContext:

DbContext:

public partial class EquationDBEntities : DbContext
{
    public EquationDBEntities()
        : base("name=EquationDBEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public virtual DbSet<Equation> Equations { get; set; }
    public virtual DbSet<Result> Results { get; set; }

conectionString:

conectionString:

<connectionStrings>
<add name="EquationDBEntities" connectionString="metadata=res://*/EquationDBModel.csdl|res://*/EquationDBModel.ssdl|res://*/EquationDBModel.msl;
provider=System.Data.SqlClient;
provider connection string=&quot;
data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\EquationDB.mdf;
integrated security=True;connect timeout=30;
MultipleActiveResultSets=True;App=EntityFramework&quot;
" providerName="System.Data.EntityClient" />
</connectionStrings>

使用ef模型的项目的App.Config(我从当前项目引用它)

App.Config of project with ef model(i reference it from my current project)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="EquationDBEntities" connectionString="metadata=res://*/EquationDBModel.csdl|res://*/EquationDBModel.ssdl|res://*/EquationDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\EquationDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

任何想法?

推荐答案

发生这种情况是因为 | DataDirectory | 替换字符串表示应用程序使用的数据库位于子文件夹 BIN \DEBUG 中。但是当您向项目添加数据库时,它会创建两个副本,一个在项目文件夹中,另一个在 BIN \DEBUG 中,您希望更改将在项目文件夹中进行,这就是为什么您在服务器资源管理器中看不到任何内容,因为它可能有错误的连接。

It happens because |DataDirectory| substitution string means that the database used by your application is located in the subfolder BIN\DEBUG. But when you add a database to your project it creates two copies, one in project folder and another in BIN\DEBUG and you expect that the changes will be made in database from the project folder that's why you don't see anything in server explorer, because it probably has a wrong connection.

这篇关于EF不能将SaveChanges保存到db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆