Answered by:
Unable to create database when using connection string

Question
-
I am new to entity framework. I was trying to use connection string along with DropCreateDatabaseIfModelChanges initializer. I was able to call SaveChanges method and could see primary keys getting generated incrementally. But I was not able to find the database created in the SQL management studio.
Database.SetInitializer<ProjectDataContext>(new DropCreateDatabaseIfModelChanges<ProjectDataContext>());
<connectionStrings> <clear/> <add name="ProjectDatabase" providerName="System.Data.SqlClient" connectionString="Data Source=.\EXPRESS2008;Initial Catalog=ProjectDatabase;Integrated Security=True;Connect Timeout=30;User Instance=True"/> </connectionStrings>
public class ProjectDataContext : DbContext { public ProjectDataContext() : base("name=ProjectDatabase") { } public DbSet<Property> Properties { get; set; } public DbSet<DrawerType> Drawers { get; set; } }
using (var context = new ProjectDataContext()) { var unicorns = from u in context.Drawers where u.Name.StartsWith("W") select u; context.Drawers.Add(new DrawerType { Name = "WOWWOWWOW" }); context.SaveChanges(); return unicorns.ToList(); }
If I don't use the connection string everything works fine.Now I know that this is against code first principle and we have to define our own configuration for mapping using fluent API.
The real issue i wanted to resolve was that I have both SQl2005 and 2008 on my machine and the EF creates the database in SQL2005. I want to force it to use SQL2008 using connection string.
Wednesday, July 20, 2011 11:06 AM
Answers
-
Hi Farahan,
Try creating a context class with the class name "ProjectDatabase" which is your connection string name or alternatively you can change your connectionstring name with "ProjectData" like this
<add name="ProjectData" providerName="System.Data.SqlClient" connectionString="Data Source=.\EXPRESS2008;Initial Catalog=ProjectDatabase;Integrated Security=True;Connect Timeout=30;User Instance=True"/> </connectionStrings>
Since, by default when we create a DBContext class using EF code-first it check for the connection string with the same name to instantiate.
Thanks.
- Proposed as answer by umeshv Thursday, July 21, 2011 7:37 AM
- Marked as answer by Larcolais Gong Thursday, August 4, 2011 6:54 AM
Wednesday, July 20, 2011 1:13 PM
All replies
-
Hi Farahan,
Try creating a context class with the class name "ProjectDatabase" which is your connection string name or alternatively you can change your connectionstring name with "ProjectData" like this
<add name="ProjectData" providerName="System.Data.SqlClient" connectionString="Data Source=.\EXPRESS2008;Initial Catalog=ProjectDatabase;Integrated Security=True;Connect Timeout=30;User Instance=True"/> </connectionStrings>
Since, by default when we create a DBContext class using EF code-first it check for the connection string with the same name to instantiate.
Thanks.
- Proposed as answer by umeshv Thursday, July 21, 2011 7:37 AM
- Marked as answer by Larcolais Gong Thursday, August 4, 2011 6:54 AM
Wednesday, July 20, 2011 1:13 PM -
Hi Umeshv
Cool that do works. Thanks for the reply.
the User Instance = True in the connection string was also one of the culprit. I overlooked my mistake.
farhanThursday, July 21, 2011 5:35 AM