我已经为此工作了一个星期,并在ASP.Net论坛中发布了许多问题,但是没有人回复我。这是细分。我有一个MVC4项目,该项目具有两个类库,一个用于建模数据,另一个用于访问数据。在DAL中,我有以下内容:

public class GinkysDb : DbContext
{
   public GinkysDb()
        : base("ginkys")
    {
        Debug.Write(Database.Connection.ConnectionString);
    }

    public DbSet<User> Users { get; set; }
    public DbSet<SearchLog> SearchLogs { get; set; }

    public class DbInitializer : DropCreateDatabaseIfModelChanges<GinkysDb>
    {
        protected override void Seed(GinkysDb context)
        {

        Guid g1 = Guid.NewGuid();
        Guid g2 = Guid.NewGuid();
        DateTime Date1 = DateTime.Now.AddDays(-160);
        DateTime Date2 = DateTime.Now.AddDays(-37);
        DateTime Date3 = DateTime.Now.AddDays(-16);
        DateTime Date4 = DateTime.Now.AddDays(-10);
        DateTime Date5 = DateTime.Now.AddDays(-60);
        DateTime Date6 = DateTime.Now.AddDays(-23);
            new List<User>
            {
                new User{Id=g1,UserName="jIgnatowski",FirstName="Jim",LastName="Ignatowski",EmailAddress="Jim@Ignatowski.com"},
                new User{Id=g2,UserName="esmtih",FirstName="Elliott",LastName="Smith",EmailAddress="elliott@smith.com"},
            }.ForEach(u => context.Users.Add(u));
            base.Seed(context);

            new List<SearchLog>
            {
                new SearchLog{Id=(Guid.NewGuid()),UserId=g1,ComapnyName="GT",Notes=""},
                new SearchLog{Id=(Guid.NewGuid()),UserId=g2,ComapnyName="MCA",Notes=""},
                new SearchLog{Id=(Guid.NewGuid()),UserId=g1,ComapnyName="RLL",Notes=""},
                new SearchLog{Id=(Guid.NewGuid()),UserId=g1,ComapnyName="PwC",Notes=""},
                new SearchLog{Id=(Guid.NewGuid()),UserId=g2,ComapnyName="PH",Notes=""},
                new SearchLog{Id=(Guid.NewGuid()),UserId=g1,ComapnyName="KPMG",Notes=""},
            }.ForEach(s => context.SearchLogs.Add(s));
            base.Seed(context);
        }
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        Database.SetInitializer(new DbInitializer());
        base.OnModelCreating(modelBuilder);
    }
}


我有以下App.Config:

<configuration>
  <connectionStrings>
    <add name="GinkysDb" connectionString="Data Source=.\SQLEXPRESS;initial catalog=ginkys;integrated security=true;App=EntityFramework;multipleactiveresultsets=True" providerName="System.Data.SqlClient" ></add>

  </connectionStrings>
  <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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
  </entityFramework>
</configuration>


我尝试添加:base(“ name = GinkysDb”)强制其在App.config中查找此连接字符串,但我得到以下错误:

在应用程序配置文件中找不到名为“ GinkysDb”的连接字符串。

显然在App.Config中!我好生气!我过去使用VS2010,MVC3和EF4.2(或任何先引入代码的版本)设置了这些类型的项目。这是我第一次使用VS2012,EF5和MVC4 ...

如果删除name =部分,则可以正常工作,但使用默认连接器连接到本地SQLEXPRESS DB。作为一项测试,以确保我知道它没有使用App.Config,我将连接字符串替换为Web Hosting服务的经过验证的有效连接字符串,并删除了本地SQLEXPRESS服务器上数据库的所有实例。当我运行该应用程序时,它将在本地SQL服务器上创建数据库!请帮忙!我求你了!

谢谢!
托尼

最佳答案

默认情况下,所有配置都从启动项目的配置文件中获取。因此,您的App.config连接字符串未搜索到,也未找到。您应该将连接字符串放置到启动项目的web.config文件中,然后将找到它。

09-10 00:43
查看更多