0

I have one solution with 2 project:

enter image description here

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.

Stiger
  • 1,189
  • 2
  • 14
  • 29

1 Answers1

2

If I'm not mistaken, your start up project is BreezeDemo, but your connection string is located on BreezeDemo.DAL.

You need to copy <configSections>, <connectionStrings> and <entityFramework> element from Web.Config of BreezeDemo.DAL to Web.Config of BreezeDemo.

Yuliam Chandra
  • 14,494
  • 12
  • 52
  • 67
  • I want all data access has been done in BreezeDemo.DAL, BreezeDemo just need to add DAL reference and call some public function from DAL. The DB file should be created in DAL App_Data. Please help. – Stiger Jul 31 '14 at 02:23
  • One thing, after move the connection string to the BreezeDemo, I get the new error `Keyword not supported: 'attachdbfilename'.`. Maybe it happen because of any error in my connection string: `` – Stiger Jul 31 '14 at 02:35
  • you want to use sql server or sql compact ? you mentioned about [sql compact](http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection.connectionstring%28v=vs.100%29.aspx) earlier but your connection string is [sql server](http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.attachdbfilename%28v=vs.110%29.aspx), and the connection string path is relative to the start up project, you need to use relative path to find the location you expected, like `..\..\..\ProjectName\AppData` – Yuliam Chandra Jul 31 '14 at 04:59
  • I want to use sql compact, my connection maybe wrong at providerName. I understand the path, but how can I put this connection string in the DAL project. I mean when I copy this project to somewhere, I don't need to update the new start up project connection string, just add DAL reference and using it. – Stiger Aug 01 '14 at 01:30
  • @Stiger, if you use sqlcompact, the connection string you have is invalid, AttachDbFileName belongs to sqlserver, and the sqlcompact datasource location is relative to the bin folder of the start up project, you need to find the location to use relative path like `..\..\..\ProjectName\App_Data` or might the absolute path `C:\..\..`, you can also use `"Data source=|DataDirectory|Database.sdf"`, but it will relative to start up project's App_Data – Yuliam Chandra Aug 01 '14 at 05:53
  • It's SQL Server Compact, and It use sdf file for database. Database location can be config like my connection string. This connection string I copy from another working project :) – Stiger Aug 01 '14 at 06:59