web.config连接字符串未设置上下文?

时间:2015-06-14 19:00:58

标签: c# sql asp.net entity-framework

我正在使用asp.net mvc并在类库中使用实体框架。

我的实体背景:

public class AmarContext : DbContext
{
    public AmarContext()
        : base("AmarGiriEntities")
    {

    }

    public DbSet<AmarTable> Amars { get; set; }
    public DbSet<GuidDetailsTable> GuidDetails { get; set; }
    public DbSet<GuidIDTable> GuidIDs { get; set; }
    public DbSet<IPsTable> IPs { get; set; }
}

我的Web.config Connectionstring:

    <configuration>
  <connectionStrings>
    <add name="AmarGiriEntities" connectionString="Data Source=localhost;Integrated Security=False;Initial Catalog=FrameSoftAmar;User ID=username;Password=pass;Connect Timeout=15;Encrypt=False;Packet Size=4096" />
  </connectionStrings>
  <configSections>

    <sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
      <section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
   ...

但如果我检查连接字符串,请将此返回给我:

Data Source=(localdb)\mssqllocaldb;AttachDbFilename=|DataDirectory|AmarGiriEntities.mdf;Initial Catalog=AmarGiriEntities;Integrated Security=True;MultipleActiveResultSets=True

为什么呢? 如果我在控制台应用程序中测试我的类库并在app.config中设置连接字符串,那么它的工作原理。但是来自asp.net mvc无法正常工作。

1 个答案:

答案 0 :(得分:0)

您是否尝试过添加<clear/>标签,例如Stefan评论?它应该解决。

也请查看此部分:

<entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> <parameters> <parameter value="Data Source=(localdb)\v11.0...." MultipleActiveResultSets=True" /> </parameters> </defaultConnectionFactory> </entityFramework>

如果不解决,请检查Web.Config文件的位置。请记住,此文件存在优先约定。

请参阅:ASP.NET Configuration File Hierarchy and Inheritance