问题
I have been working on this for a week now and have posted a number of questions in the ASP.Net forums but no one is getting back to me. Here is the breakdown. I have an MVC4 project that is layered with two class libraries, one for modeling data and the other for accessing data. In the DAL I have the following:
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);
}
}
I have the following 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>
I have tried adding : base("name=GinkysDb") to force it to look for this connection string in the App.config but I just get the following error:
No connection string named 'GinkysDb' could be found in the application config file.
It's clearly in the App.Config! I am so aggravated! I have set these types of projects up in the past using VS2010, MVC3 and EF4.2 (OR whatever version introduced code first). This is the first time I am using VS2012, EF5 and MVC4...
If I remove the name= part it works fine but uses the default connector to my local SQLEXPRESS DB. As a test to make sure I knew it was not using the App.Config, I replaced the connection string with a verified working connection string to my Web Hosting service and deleted all of the instances of the DB on my local SQLEXPRESS server. When i run the app, it creates the DB on the local SQL server! PLEASE HELP! I am begging you!
Thanks! Tony
回答1:
By default all configuration is gotten from config file of starting project. So your App.config connection string was not searching and not found. You should place your connection string to web.config file of your starting project and it will be found.
来源:https://stackoverflow.com/questions/13333273/is-there-a-bug-with-the-new-entity-framework-5-in-an-mvc4-project-with-a-data-ac