Place to put Database.SetInitializer

To avoid the coupling, I would prefer not to set the initializer outside the Assembly that contains the DataContext. So, I added a static constructor for the DataContext. This way every project referencing this Assembly will enjoy the initializer without explicitly setting it, and the initializer is set only once per process. static MyDataContext() { … Read more

DbSet.Find method ridiculously slow compared to .SingleOrDefault on ID

Find calls DetectChanges internally, SingleOrDefault (or generally any query) doesn’t. DetectChanges is an expensive operation, so that’s the reason why Find is slower (but it might become faster if the entity is already loaded into the context because Find would not run a query but just return the loaded entity). If you want to use … Read more

Composite Key with EF 4.1 Code First

You can mark both ActivityID and ActivityName properties with Key annotation or you can use fluent API as described by @taylonr. Edit: This should work – composite key defined with annotations requires explicit column order: public class ActivityType { [Key, Column(Order = 0)] public int ActivityID { get; set; } [Key, Column(Order = 1)] [Required(ErrorMessage … Read more

How to create index in Entity Framework 6.2 with fluent configuration

Well 26.10.2017 Entity Framework 6.2 was officially released. It includes a possibility to define indexes with ease via Fluent API. Ho it is to use was already announced in the beta of 6.2. Now you can use the HasIndex() method, followed by IsUnique() if it should be an unique index. Just a small comparison (before/after) … Read more

Unable to Retrieve Metadata

It seems that MVC4 Controller scaffolding is not properly recognizing MySql Connection String. Change the connection string as shown below when generating EF CRUD code for Controllers: <connectionStrings> <add name=”BTDContext” connectionString=”Data Source=host_name;Database=database_name;uid=user_id;pwd=password;” providerName=”System.Data.SqlClient” /> </connectionStrings> Change it back to standard when running the application: <connectionStrings> <add name=”BTDContext” connectionString=”Data Source=host_name;Database=database_name;uid=user_id;pwd=password;” providerName=”MySql.Data.MySqlClient” /> </connectionStrings> Note the change, … Read more

Using mvc-mini-profiler database profiling with Entity Framework Code First

This is now fully supported, check out the latest source or grab the package from nuget. You will need the MiniProfiler.EF package if you are using nuget. (1.9.1 and up) Supporting this involved a large set of modifications to the underlying proxy object to support acting as EF code first proxies. To add this support: … Read more

How to set default value for POCO’s in EF CF?

With the release of Entity Framework 4.3 you can do this through Migrations. EF 4.3 Code First Migrations Walkthrough So using your example it would be something like: public partial class AddPersonClass : DbMigration { public override void Up() { CreateTable( “People”, c => new { Id = c.Int(nullable: false, identity: true), Name = c.String(maxLength: … Read more

What is the syntax for self referencing foreign keys in EF Code First?

Something like this will work: public class Contact { public int Id {get;set;} public string Name {get;set;} public int? SpouseId {get;set;} [ForeignKey(“SpouseId”)] public Contact Spouse {get;set;} } ForeignKeyAttribute has been added to System.ComponentModel.DataAnnotations by CTP5 assembly. Update I: CTP5 Bug: Due to a bug in CTP5, creating an Independent Self Referencing Associations throws an exception. … Read more