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

Code First Migration with Connection Strings

On the package manager console type: Get-Help Update-Database Relevant part: Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [<Com monParameters>] So you can do a Update-Database -ConnectionStringName “MyConnectionString” and it should work like a charm. You also have a MigrateDatabaseToLatestVersion database initializer, if you set it (via Database.SetInitializer()), … Read more

Entity Framework Code-first default data in database

You create custom initializer, which inherits from DropCreateDatabaseIfModelChanges or DropCreateDatabaseAlways interface. Like: public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<-YourDbContext-> And then you overwrite Seed method like: protected override void Seed(YourDbContext context) Whole example might look like: public class EntitiesContextInitializer : DropCreateDatabaseIfModelChanges<EntitiesContext> { protected override void Seed(EntitiesContext context) { List<Role> roles = new List<Role> { new Role {Id=1, … Read more

Problems creating a Foreign-Key relationship on Entity Framework

There is a conflict between your foreign key property in class Dettaglio… public virtual int IDTEST { get; set; } …which has a non-nullable type (int) and therefore cannot be optional and your mapping… this.HasOptional(t => t.TEST_TABLE) //… …where you want the relationship to be optional. If you indeed want an optional relationship use a … 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

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