C# 正在执行未知的连接字符串

C# 正在执行未知的连接字符串,c#,sql,asp.net-mvc,C#,Sql,Asp.net Mvc,我已经清楚地说明了我需要MVC应用程序在Web配置文件中使用的连接字符串: <?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>

我已经清楚地说明了我需要MVC应用程序在Web配置文件中使用的连接字符串:

<?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="OnlineStoreDB" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=OnlineStoreDB; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|OnlineStoreDB.mdf" 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.2" />
    <httpRuntime targetFramework="4.5.2" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <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.2.3.0" newVersion="5.2.3.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>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
</configuration>
上下文
文件代码:

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using Microsoft.AspNet.Identity.EntityFramework;
using OnlineStore.Models.MvcMusicStore.Models;

namespace OnlineStore.Models
{
    public class OnlineStoreDB : IdentityDbContext
    {
        public OnlineStoreDB()
            : base("OnlineStoreDB")
        {
            Configuration.LazyLoadingEnabled = false;
            Configuration.ProxyCreationEnabled = false;
        }
        public DbSet<Item> Items { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Brand> Brands { get; set; }
        public DbSet<Cart> Carts { get; set; }
        public DbSet<Order> Orders { get; set; }
        public DbSet<OrderDetail> OrderDetails { get; set; }
        public System.Data.Entity.DbSet<OnlineStore.ViewModels.ShoppingCartViewModel> ShoppingCartViewModels { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
        }
    }
}
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Conventions;
使用Microsoft.AspNet.Identity.EntityFramework;
使用OnlineStore.Models.MvcMusicStore.Models;
名称空间OnlineStore.Models
{
公共类OnlineStoreDB:IdentityDbContext
{
公共在线存储数据库()
:base(“OnlineStoreDB”)
{
Configuration.LazyLoadingEnabled=false;
Configuration.ProxyCreationEnabled=false;
}
公共数据库集项{get;set;}
公共数据库集类别{get;set;}
公共DbSet品牌{get;set;}
公共DbSet Carts{get;set;}
公共数据库集命令{get;set;}
公共DbSet OrderDetails{get;set;}
public System.Data.Entity.DbSet ShoppingCartViewModels{get;set;}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Conventions.Remove();
modelBuilder.Conventions.Remove();
}
}
}

请注意,我是从
IdentityDBContext
而不是
DBContext
派生
Context
类(
OnlineStoreDB
),因为我需要在我的应用程序中使用
IdentityRoles

您需要将
ApplicationDbContext
中的
DefaultConnection
构造函数更改为您自己的连接密钥(即
OnlineStoreDB
),如下所示:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("OnlineStoreDB", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<OnlineStore.Models.Item> Items { get; set; }
}

是否已检查MDF文件是否可用?或者是否有任何特殊原因提到连接字符串中的MDF文件nane?这是猜测:您使用的是什么用户/登录管理代码?如果它是作为项目模板的一部分生成的,则可能需要您指定要用作用户数据库的内容。只需查看代码即可。@PrashantPimpale。我故意删除了“DefaultConnection.mdf”,只是因为我不想让我的应用程序使用它。我想知道为什么会这样。在父web.config文件中还有其他连接字符串吗?找出根据该名称搜索整个项目时使用的连接字符串的位置(即
DefaultConnection
)。应用建议的更改后,带有
OnlineStoreDB
的数据库是在localDB上创建的,但问题是其中只有一个名为
dbo的表。\uu MigrationHistory
,并且没有ASP.NET实体被转换为SQL数据库表。我将在我的问题中添加“上下文”文件代码,供您审阅。谢谢,因为您有EF Code First上下文定义,所以需要启用迁移以添加现有数据库架构(使用启用迁移添加迁移,然后更新数据库)。您可以阅读这些步骤(注意:迁移过程似乎与此问题不同)。我注意到建议的连接字符串中的
\OnlineStoreDB.mdf
与我的连接字符串中使用的
OnlineStoreDB.mdf
不同。我应用了您的代码,但很抱歉,这没有任何区别。
[\]
反斜杠标记了数据库文件的路径(它遵循操作系统路径约定)。在您可以首先将现有数据库与代码一起使用之前(请注意,OnModelCreatingusage),必须像前面提到的那样启用迁移。我应用了迁移,现在它的所有功能都可以正常工作了。非常感谢兄弟一直以来的帮助。谢谢。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("OnlineStoreDB", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }

    public System.Data.Entity.DbSet<OnlineStore.Models.Item> Items { get; set; }
}
<add name="OnlineStoreDB" connectionString="Data Source=(localdb)\MSSQLLocalDB; 
     Initial Catalog=OnlineStoreDB;Integrated Security=True; 
     MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|\OnlineStoreDB.mdf" 
     providerName="System.Data.SqlClient" />