I have one solution with 2 project:
I add Entity Framework 6 and Sql server compact for both of them via nuget.
<packages>
<package id="EntityFramework" version="6.1.1" targetFramework="net45" />
<package id="EntityFramework.SqlServerCompact" version="6.1.1" targetFramework="net45" />
<package id="Microsoft.SqlServer.Compact" version="4.0.8876.1" targetFramework="net45" />
</packages>
I add this connection string to Web config of DAL:
<connectionStrings>
<remove name="connectionString"/>
<add name="connectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Database.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
Entity Framework config (I add contexts
to BreezeDemo.DAL only ):
<entityFramework>
<contexts>
<context type="BreezeDemo.DAL.MyDbContext, BreezeDemo.DAL">
<databaseInitializer type="BreezeDemo.DAL.SchoolInitializer, BreezeDemo.DAL" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>
Here my code in the context:
public class MyDbContext : DbContext
{
public MyDbContext()
: base("connectionString")
{
Database.SetInitializer<MyDbContext>(new SchoolInitializer());
}
public DbSet<Student> Students { get; set; }
public DbSet<Enrollment> Enrollments { get; set; }
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
When I try list of students:
public class StudentRepository
{
MyDbContext dal = new MyDbContext();
public List<Student> GetAll()
{
return dal.Students.ToList();
}
}
EF Code first running, but it does not use my connection string in web config. It create db file name "connectionString" in BreezeDemo\App_Data
. My connection in web config want to create the db filename: "Database" in BreezeDemo.DAL\App_Data
.
Am I missing something? Please help me make it work right.