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

标签 c# asp.net-mvc entity-framework 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= 部分,它工作正常,但使用默认连接器连接到我的本地 SQLEXPRESS 数据库。作为确保我知道它没有使用 App.Config 的测试,我将连接字符串替换为经过验证的工作连接字符串到我的 Web 托管服务,并删除了我本地 SQLEXPRESS 服务器上的所有数据库实例。当我运行该应用程序时,它会在本地 SQL 服务器上创建数据库!请帮忙!我求求你了!

谢谢! 托尼

最佳答案

默认情况下,所有配置都是从启动项目的配置文件中获取的。所以你的 App.config 连接字符串没有搜索也没有找到。您应该将您的连接字符串放入您的起始项目的 web.config 文件中,它将被找到。

关于c# - 具有数据访问层和 App.Config 的 MVC4 项目中的新 Entity Framework 5 是否存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13333273/

相关文章:

c# - 为什么 Custom DataAnnotationsModelMetadataProvider 的 CreateMetadata 永远不会被调用?

javascript - 如果使用 jquery timepicker,则无法从模式弹出窗口发布表单

entity-framework - 在实体( Entity Framework )中创建非映射属性

c# - 角色管理器是否为自定义角色提供程序缓存数据,我可以清除此缓存吗?

c# - 无法先从模型代码中删除 FK

c# - 为什么对 ObjectSet<T>.AddObject() 的调用会获取所有 T 类型的实体?

c# - Entity Framework 6 Fluent 映射 - 一对多代码优先

c# - 继承类的泥潭,如何让这段可维护的代码

c# - 异步编程和 Azure 函数

c# - 清除 C#/VB.NET 中的字体使用