2

ive a similar problem as this guy: Generate SQL CE database from EF Code-First DbContext class

I know that i need to use

Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0");

it must be executed on the application start .. (i am MVC newbie) where can i find such an "Application_Start" in MVC?

EDIT: I solved my problem: this code must be inserted into the global.asax

1
  • 1
    ya.. but its not necessary at all this should be controlled via your connect string - see below. Commented Nov 5, 2011 at 23:41

1 Answer 1

4

You actually don't need to do that at all. Specify the name of your context class in your web.config:

 <connectionStrings>
    <add name="EntityDemoSite.DataAccess.EntityContext" connectionString="Data Source=|DataDirectory|EntityDemoSite.sdf" providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>

If your context class is in the same project (mine is not) just use name="WhateverYourContextClassIsNamed" instead.

Check out my sample projects here: http://completedevelopment.blogspot.com/2011/10/codecamp-nyc-entity-framework-mvc-code.html note in the web.config of the mvc app the connectionstrings section This is where it all happens. Then in my context class in the data access project you'll note how it tels it to create the database:

public class EntityContext : DbContext
{
     static EntityContext()
     {
       //for example to create a db:
       Database.SetInitializer(new CreateDatabaseIfNotExists());
      ...
     }
}      
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.