How do I use Entity Framework in Code First Drop-Create mode?

Use DropCreateDatabaseAlways initializer for your database. It will always recreate database during first usage of context in app domain: Database.SetInitializer(new DropCreateDatabaseAlways<YourContextName>()); Actually if you want to seed your database, then create your own initializer, which will be inherited from DropCreateDatabaseAlways: public class MyInitializer : DropCreateDatabaseAlways<YourContextName> { protected override void Seed(MagnateContext context) { // seed database … Read more

Entity Framework CTP 4 – Code First Custom Database Initializer

I ran into the same problem. I didn’t really solve it, but I managed to get a little nasty workaround running, so i can deploy my solution to AppHarbor 😉 Its a IDatabaseInitializer implementation, that doesn’t delete the db, but just nukes all the constraints and tables, and then uses the ObjectContext.CreateDatabaseScript() method to generate … Read more

Using MySql with Entity Framework 4 and the Code-First Development CTP

Right, finally got it working with a few points of interest. Cannot create a DB, must exist already You have to create a connection string for each DB contest using the DBContext name (in the above example a connectionstring must exist with the name “NerdDinners”), not just a default one (else it will use SQL) … Read more

How to configure ProviderManifestToken for EF Code First

If you’re using EF 6 (just released) you have an alternative. Dependency Resolution You can use the new dependency resolution feature to register an implementation of IManifestTokenResolver (described in this preview documentation as IManifestTokenService). This article gives a bit more information on how to use DbConfiguration. The easiest way to use it is like this: … Read more

Understanding ForeignKey attribute in entity framework code first

The required side of the 1..0 relationship MemberDataSet should not have a FK to DeferredData. Instead, DeferredData‘s PK should also be a FK to MemberDataSet (known as shared primary key) public class MemberDataSet { [Key] [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual DeferredData DeferredData { get; set; } } public class DeferredData … Read more

Entity Framework loading child collection with sort order

You cannot achieve it directly because neither eager or lazy loading in EF supports ordering or filtering. Your options are: Sort data in your application after you load them from database Execute separate query to load child records. Once you use separate query you can use OrderBy The second option can be used with explicit … Read more

Can you create sql views / stored procedure using Entity Framework 4.1 Code first approach

We support stored procedures in our Entity Framework Code First Migrations. Our approach is to create some folder to hold the .sql files (~/Sql/ for example). Create .sql files in the folder for both creating and dropping the stored procedure. E.g. Create_sp_DoSomething.sql and Drop_sp_DoSomething. Because the SQL runs in a batch and CREATE PROCEDURE.. must … Read more