Connection string EF 4.1 code first SQL compact in windows form application
You need to put the app.config in the application (.exe) project. The app.config file should look like in this blog post (case sensitive): http://erikej.blogspot.com/2011/04/saving-images-to-sql-server-compact.html and the names should be MyDB, not MyDatabase...
Francesco
Updated on June 25, 2022Comments
-
Francesco almost 2 years
I have created a windows form application:
- A presentation library with several windows forms
- A class library with a data layer
- A class library to access a database
I'm using EntityFramework 4.1 with Code First Approach and SQL Compact 4.0 database.
I created a connection string in the app.config file in the class library project used to connect to the database. The problem is that the connection string has apparently no influence on the database creation. I mean that everything is working fine with the program but even if I specify a location for the database this does not have any effect!
Am I writing in the right app.config? Do I need to initialize my DbContext class in a specific way? (today I do not pass any connection string in the constructor)
DbContext class:
public class MyDB : DbContext { public DbSet<ContactPerson> ContactPersons { get; set; } public DbSet<Customer> Customers { get; set; } public DbSet<Project> Projects { get; set; } public DbSet<Quotation> Quotations { get; set; } public MyDB() : base("MyDatabase") { } }
App.config connection string:
<add name="MyDatabase" connectionString="Data Source=MyDB.sdf" providerName="System.Data.SqlServerCE.4.0">
-
Francesco almost 13 yearsI did it, but it is not creating any actual database file. I tried to search for the database in the folder without any results
-
Francesco almost 13 yearsThe database is actually creating in SQL server express!
-
Francesco almost 13 yearsI tried everything with no results! app.config is marked has "copy always" and it appears in the bin\debug folder. But no sdf file is created
-
ErikEJ almost 13 yearsBuild action should be None and Do not Copy. Could you show us your app.config contents, please...
-
Francesco almost 13 years<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="MyDB" providerName="System.Data.SqlServerCe.4.0" connectionString="Data Source=C:\Users\Francesco\Documents\Test.sdf"/> </connectionStrings> </configuration>
-
ErikEJ over 12 yearsActually the providerName should be: providerName="System.Data.SqlServerCe.4.0"