web applications - ASP.NET EF5 code-first not creating SQL Server CE database -
i searched far , wide, went through tutorials , tips, wont work!
ef 5 code-first not generating sql server ce 4 database file.
i have asp.net web application , want generate database using code-first approach.
here model classes:
public class class { [key] public int id { get; set; } public bool islecture { get; set; } public int courseid { get; set; } public virtual course course { get; set; } public int teacherid { get; set; } public virtual teacher teacher { get; set; } } public class course { [key] public int id { get; set; } public string name { get; set; } public int semester { get; set; } } public class teacher { public int id { get; set; } public string name { get; set; } public string lastname { get; set; } }
here context:
public class mycontext : dbcontext { public dbset<class> classes { get; set; } public dbset<teacher> teachers { get; set; } public dbset<course> courses { get; set; } }
here connection string in web.config
:
<add name="mycontext" connectionstring="data source=|datadirectory|\mycontext.sdf" providername="system.data.sqlserverce.4.0" />
this in global.asax.cs
:
protected void application_start() { database.setinitializer<mycontext> (new dropcreatedatabaseifmodelchanges<mycontext>()); ... }
i've tried w/ , w/o these lines in global.asax.cs
, w/ , w/o connection string.
the application loads , works, database file isn't created. tried creating .sdf
file myself, populated ef code-first, remains empty.
the project asp.net mvc 4 made hottowel template, if means anything, , i'm using visual studio 2012.
solved:
needed code in global.asax.cs
:
mycontext context = new mycontext(); context.database.initialize(true);
this works me - need connection factory...
<configsections>... </configsections> <entityframework> <defaultconnectionfactory type="system.data.entity.infrastructure.sqlceconnectionfactory, entityframework"> <parameters> <parameter value="system.data.sqlserverce.4.0" /> </parameters> </defaultconnectionfactory> </entityframework>