【问题标题】:Inserting a new record updates LocalDB but not SQL server DB插入新记录更新 LocalDB 但不更新 SQL Server DB
【发布时间】:2014-04-26 06:30:15
【问题描述】:

我正在关注 MVC-Movies 教程 here,我正在尝试更新 SQL Server 上的记录 NOT 在 LocalDB 上。

当我在我的程序中添加一条新记录时,例如:

SQL Server 中的 DB 没有更新,只有 LocalDB 更新了。

这里是 Web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<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="DefaultConnection" 
         connectionString="Data Source=localhost;
                           AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20140418101450.mdf;
                           Initial Catalog=aspnet-MvcMovie-20140418101450;Integrated Security=True"
         providerName="System.Data.SqlClient" />


    <add name="MovieDBContext"
         connectionString="Data Source=(LocalDB)\v11.0;
         AttachDbFilename=|DataDirectory|\Movies.mdf;
         Integrated Security=True"
         providerName="System.Data.SqlClient" />
  </connectionStrings>


  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthenticationModule" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

我哪里做错了?

我的数据库在 sql server 中的名称是:MoviesDB

谢谢

编辑

MovieMovieDBContext 类:

using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations;


namespace MvcMovie.Models
{
    public class Movie
    {
        public int ID { get; set; }
        public string Title { get; set; }

        [Display(Name = "Release Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime ReleaseDate { get; set; }
        public string Genre { get; set; }
        public decimal Price { get; set; }
    }

    public class MovieDBContext : DbContext
    {
        public DbSet<Movie> Movies { get; set; }
    }
}

编辑 2:

我把连接字符串改成了

<add name="MovieDBContext"
     connectionString="Data Source=localhost;
                       AttachDbFilename=|DataDirectory|\Movies.mdf;
                       Initial Catalog=MoviesDB;
                       Integrated Security=True"
     providerName="System.Data.SqlClient" />

但后来我明白了:

“/”应用程序中的服务器错误。

键“attachdbfilename”的值无效。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentException:键“attachdbfilename”的值无效。

来源错误:

第 18 行:公共 ActionResult Index()
第 19 行:{
第 20 行:返回 View(db.Movies.ToList());
第 21 行:}
第 22 行:

源文件:c:\Users\X3\Documents\Visual Studio 2013\Projects\MvcMovie\MvcMovie\Controllers\MoviesController.cs 行:20

【问题讨论】:

  • 我可以看看Context的课程吗?
  • @lnanikian: 你是说上课MovieDBContext 吗?我更新了帖子,谢谢。
  • 但在编辑你的问题之前,要知道 LocalDb\V.11 是 SQLserver。

标签: c# sql-server asp.net-mvc visual-studio-2012 visual-studio-2013


【解决方案1】:

您的任何一个连接字符串都没有引用MoviesDB

一个是指aspnet-MvcMovie-20140418101450.mdf,另一个是Movies.mdf

除非您覆盖该设置,否则您的上下文将使用与其名称匹配的连接字符串。所以你应该修改MovieDBContext连接字符串指向你的SQL实例而不是LocalDB

编辑

我假设这和你真正想要的连接字符串类似,但其他例子你应该参考官方Microsoft Reference

<add name="MovieDBContext"  providerName="System.Data.SqlClient"
    connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MoviesDB;
    Integrated Security=True"/>

【讨论】:

  • 我按照你的建议做了,但后来我遇到了一个例外,我将它添加到我的原始帖子中,在 EDIT 2 下。谢谢
  • @ron 我添加了一个我认为是您所追求的连接字符串,以及指向 Microsoft 以获取其他可能的连接字符串的链接。你不应该使用AttachDbFilename,除非你有一个存储数据库的特定文件;在这种情况下,AttachDbFilename 区分大小写。
【解决方案2】:

改为:

    <add name="MovieDBContext"
      connectionString="Data Source=localhost;
                   AttachDbFilename=|DataDirectory|\MoviesDB.mdf;
                   Initial Catalog=MoviesDB;
                   Integrated Security=True"
      providerName="System.Data.SqlClient" />

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2020-11-21
    • 2013-12-06
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    相关资源
    最近更新 更多