[英]Inserting a new record updates LocalDB but not SQL server DB
我在這里關注MVC-Movies教程,我正在嘗試更新SQL Server上的記錄而不是 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>
我哪里做錯了 ?
我的DB在sql server中的名稱是:MoviesDB
謝謝
編輯 :
類Movie
和MovieDBContext
:
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索引()
第19行:{
第20行:返回視圖(db.Movies.ToList());
第21行:}
第22行:源文件:c:\\ Users \\ X3 \\ Documents \\ Visual Studio 2013 \\ Projects \\ MvcMovie \\ MvcMovie \\ Controllers \\ MoviesController.cs行:20
你的連接字符串都沒有引用MoviesDB
。
一個是指aspnet-MvcMovie-20140418101450.mdf
和其他Movies.mdf
。
除非您覆蓋該設置,否則您的上下文將使用與其名稱匹配的連接字符串。 因此,您應該修改MovieDBContext
連接字符串以指向您的SQL實例而不是LocalDB
編輯
我假設這類似於您實際需要的連接字符串,但您應該參考其他示例的官方Microsoft參考 。
<add name="MovieDBContext" providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MoviesDB;
Integrated Security=True"/>
改成 :
<add name="MovieDBContext"
connectionString="Data Source=localhost;
AttachDbFilename=|DataDirectory|\MoviesDB.mdf;
Initial Catalog=MoviesDB;
Integrated Security=True"
providerName="System.Data.SqlClient" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.