Best way to incrementally seed data in Entity Framework 4.3

If you want to use entities to seed data you should use Seed method in your migrations configuration. If you generate fresh project Enable-Migrations you will get this configuration class: internal sealed class Configuration : DbMigrationsConfiguration<YourContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(CFMigrationsWithNoMagic.BlogContext context) { // This method will be … Read more

Confusion over EF Auto Migrations and seeding – seeding every program start

The fact that the Seed method ran only when the database changed was quite limiting for the database initializers that shipped in EF 4.1. It was limiting because sometimes you needed to update seed data without changing the database, but to make that happen you had to artificially make it seem like the database had … Read more

Code first DbMigrator causes error when building from different machines

We changed our code from: dbMgConfig.AutomaticMigrationDataLossAllowed = false; var mg = new DbMigrator(dbMgConfig); mg.Update(null); to dbMgConfig.AutomaticMigrationDataLossAllowed = true; var mg = new DbMigrator(dbMgConfig); var scriptor = new MigratorScriptingDecorator(mg); string script = scriptor.ScriptUpdate(sourceMigration: null, targetMigration: null); throw new Exception(script); so that we could observe what changes DbMigrator is attempting on the remote server. In the case … Read more

Mapping a foreign key with a custom column name

If you don’t want to use fluent syntax, there are three other ways of implementing the reference using data annotations (Personally I prefer data annotations as they seem easier to read and are written just above the property they are affecting): 1.1) Use ForeignKey (with an associated property) – version 1 [Table(“WIDGETENTITIES”)] public class WidgetEntity … Read more

EntityFramework Code First – Check if Entity is attached

You can find the answer here. public bool Exists<T>(T entity) where T : class { return this.Set<T>().Local.Any(e => e == entity); } Place that code into your context or you can turn it into an extension like so. public static bool Exists<TContext, TEntity>(this TContext context, TEntity entity) where TContext : DbContext where TEntity : class … Read more

How to manage Migrations in a project with multiple branches?

There is a much better solution for handling entity framework migration merge conflicts on a similar question. All you need to do after a merge is to re-scaffold the meta data of the migration in the target branch. That is you do not rescaffold the up/down code, just the state in the resx-file. add-migration [the_migration_to_rescaffold_metadata_for] … Read more

Entity Framework – Is there a way to automatically eager-load child entities without Include()?

No you cannot do that in mapping. Typical workaround is simple extension method: public static IQueryable<Car> BuildCar(this IQueryable<Car> query) { return query.Include(x => x.Wheels) .Include(x => x.Doors) .Include(x => x.Engine) .Include(x => x.Bumper) .Include(x => x.Windows); } Now every time you want to query Car with all relations you will just do: var query = … Read more