I have a layered MVC application using Entity Framework 5. I have the MVC project, and two library projects, one for modeling and the other for Data Access. The Data Access project has EF5 installed as does the MVC project. I have the below code configured:
namespace Conn.Repository
{
public class ConnDb : DbContext
{
public ConnDb()
: base("name=ConnDb")
{
}
public DbSet<User> Users { get; set; }
public class DbInitializer : DropCreateDatabaseIfModelChanges<ConnDb>
{
protected override void Seed(ConnDb context)
{
Guid g = Guid.NewGuid();
new List<User>
{
new User{Id=g,UserName="user",FirstName="Jim",LastName="Joe",EmailAddress="Jim@Joe.com"},
}.ForEach(u => context.Users.Add(u));
base.Seed(context);
}
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new DbInitializer());
base.OnModelCreating(modelBuilder);
}
}
}
Whaen I run the application and it gets to creating the Database, I get the following error:
No connection string named 'ConnDb' could be found in the application config file.
It's clearly there, am I missing something? I am using VS2012 and MVC4. I have done this in the past with MVC3 Applications without a problem and I did exactly what I did in those projects...
I can do that and it works, but it does not access the Connection String in the App.Config... AS a test, I replce the connection string with a working connection string that conects to my Web Hosting site but the DB still get's created on the local SQLEXPRESS
server... I don't get it... I've been trying to work through this for about a week now! It's crazy! LOL
tlitterio
Member
4 Points
20 Posts
Data Access Layer not recognizing Connection String in App.Config
Nov 10, 2012 03:32 PM|LINK
I have a layered MVC application using Entity Framework 5. I have the MVC project, and two library projects, one for modeling and the other for Data Access. The Data Access project has EF5 installed as does the MVC project. I have the below code configured:
namespace Conn.Repository { public class ConnDb : DbContext { public ConnDb() : base("name=ConnDb") { } public DbSet<User> Users { get; set; } public class DbInitializer : DropCreateDatabaseIfModelChanges<ConnDb> { protected override void Seed(ConnDb context) { Guid g = Guid.NewGuid(); new List<User> { new User{Id=g,UserName="user",FirstName="Jim",LastName="Joe",EmailAddress="Jim@Joe.com"}, }.ForEach(u => context.Users.Add(u)); base.Seed(context); } } protected override void OnModelCreating(DbModelBuilder modelBuilder) { Database.SetInitializer(new DbInitializer()); base.OnModelCreating(modelBuilder); } } }Here is what the connection string looks like:
Whaen I run the application and it gets to creating the Database, I get the following error:
No connection string named 'ConnDb' could be found in the application config file.
It's clearly there, am I missing something? I am using VS2012 and MVC4. I have done this in the past with MVC3 Applications without a problem and I did exactly what I did in those projects...
Thanks!
Tony
h.zahed@live...
Contributor
3171 Points
514 Posts
Re: Data Access Layer not recognizing Connection String in App.Config
Nov 10, 2012 06:40 PM|LINK
Hi,
Remove name= and try again.
More info: http://msdn.microsoft.com/pt-br/library/jj129537.aspx
MCP-MCTS-MCPD
Mark the post as Answer, if it helped you in solving your question.
tlitterio
Member
4 Points
20 Posts
Re: Data Access Layer not recognizing Connection String in App.Config
Nov 10, 2012 06:53 PM|LINK
I can do that and it works, but it does not access the Connection String in the App.Config... AS a test, I replce the connection string with a working connection string that conects to my Web Hosting site but the DB still get's created on the local SQLEXPRESS server... I don't get it... I've been trying to work through this for about a week now! It's crazy! LOL
tlitterio
Member
4 Points
20 Posts
Re: Data Access Layer not recognizing Connection String in App.Config
Nov 10, 2012 06:54 PM|LINK
Could there be a bug in the new Entity Framework???
tlitterio
Member
4 Points
20 Posts
Re: Data Access Layer not recognizing Connection String in App.Config
Nov 12, 2012 02:09 AM|LINK
Answer here:
http://stackoverflow.com/questions/13333273/is-there-a-bug-with-the-new-entity-framework-5-in-an-mvc4-project-with-a-data-ac