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

How to Specify Primary Key Name in EF-Code-First

If you want to specify the column name and override the property name, you can try the following: Using Annotations public class Job { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(“CustomIdName”)] public Guid uuid { get; set; } public int active { get; set; } } Using Code First protected override void OnModelCreating(DbModelBuilder mb) { base.OnModelCreating(mb); mb.Entity<Job>() .HasKey(i => … Read more

Why Entity Framework performs faster than Dapper in direct select statement [closed]

ORM (Object Relational Mapper) is a tool that creates layer between your application and data source and returns you the relational objects instead of (in terms of c# that you are using) ADO.NET objects. This is basic thing that every ORM does. To do this, ORMs generally execute the query and map the returned DataReader … Read more

How can set a default value constraint with Entity Framework 6 Code First?

Unfortunately the answer right now is ‘No’. But you can vote for Better support for default values EDIT 30 Mar 2015: It’s coming in EF7… Support database default values in Code First EDIT 30 Jan 2017: General support for default database values is part of EF Core (the new name for EF7)… Default values EDIT … Read more

How can I configure Entity Framework to automatically trim values retrieved for specific columns mapped to char(N) fields?

Rowan Miller (program manager for Entity Framework at Microsoft) recently posted a good solution to this which uses Interceptors. Admittedly this is only valid in EF 6.1+. His post is about trailing strings in joins, but basically, the solution as applied neatly removes trailing strings from all of the string properties in your models, automatically, … Read more