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

Entity Framework code first unique column

In Entity Framework 6.1+ you can use this attribute on your model: [Index(IsUnique=true)] You can find it in this namespace: using System.ComponentModel.DataAnnotations.Schema; If your model field is a string, make sure it is not set to nvarchar(MAX) in SQL Server or you will see this error with Entity Framework Code First: Column ‘x’ in table … Read more

Entity Framework – Start Over – Undo/Rollback All Migrations

You can rollback to any migration by using: Update-Database -TargetMigration:”MigrationName” If you want to rollback all migrations you can use: Update-Database -TargetMigration:0 or equivalent: Update-Database -TargetMigration:$InitialDatabase In some cases you can also delete database and all migration classes.

Entity Framework: One Database, Multiple DbContexts. Is this a bad idea? [closed]

You can have multiple contexts for single database. It can be useful for example if your database contains multiple database schemas and you want to handle each of them as separate self contained area. The problem is when you want to use code first to create your database – only single context in your application … Read more

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