繁体   English   中英

在具有数据访问层和App.Config的MVC4项目中是否存在新的Entity Framework 5的错误

[英]Is there a bug with the new Entity Framework 5 in an MVC4 Project with a Data Access Layer and App.Config

我已经在这方面工作了一个星期,并在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 = part它工作正常,但使用我本地SQLEXPRESS DB的默认连接器。 作为确保我知道它没有使用App.Config的测试,我将带有经过验证的工作连接字符串的连接字符串替换为我的Web Hosting服务,并删除了本地SQLEXPRESS服务器上的所有DB实例。 当我运行应用程序时,它会在本地SQL服务器上创建数据库! 请帮忙! 我求求你了!

谢谢! 托尼

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM