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

unexpected GetType() result for entity entry

You can get the original entity type of a proxy type by ObjectContext.GetObjectType(entity.GetType()) This is a static method of ObjectContext, so you can readily use in in a DbContext environment. If for some reason you need the actual entity as its original type you can use the pattern var entity = entry.Entity as MyEntity; if … Read more

How can I log all entities change, during .SaveChanges() using EF code first?

You can get the before and after values for all changed entities by going through DbContext.ChangeTracker. Unfortunately the API is a little verbose: var changeInfo = context.ChangeTracker.Entries() .Where (t => t.State == EntityState.Modified) .Select (t => new { Original = t.OriginalValues.PropertyNames.ToDictionary (pn => pn, pn => t.OriginalValues[pn]), Current = t.CurrentValues.PropertyNames.ToDictionary (pn => pn, pn => … Read more

Lose EF Code First Migration when working on different TFS branches?

An EF migration step contains a metadata file, that has a signature of the model that is the result of the migration step. The problem when merging is that the signature of the migration done in branch B doesn’t include the stuff done in the migration in branch A. As long as the migrations are … Read more

The type ‘Company.Model.User’ and the type ‘Company.Core.Model.User’ both have the same simple name of ‘User’ and so cannot be used in the same model

This is a limitation of EF that I reported in 2012 https://entityframework.codeplex.com/workitem/483 that is still not implemented in 6.0.2. EF uses a flat internal architecture and does not recognize namespaces. Might be coming in EF7 but not before. For now the only solutions is to rename the two classes to unique class names irrespective of … Read more

Why does Add-Migration sometimes create duplicate migrations?

A snapshot of the database model is saved along with every migration in a .resx file. When you add a new migration, EF compares the current database model (which is generated from your model classes and settings from your DbModelBuilder) with the last migration and determines a changes between them. The problem you are describing … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)