ef-migrations

Entity Framework, Automatic apply Migrations

一个人想着一个人 提交于 2019-11-28 17:59:44
I use Entity Framework code first and set AutomaticMigrationsEnabled true by this code : Database.SetInitializer(new MigrateDatabaseToLatestVersion<DbContext, MigrateDBConfiguration>()); ////////////////////////////////// public class MigrateDBConfiguration : System.Data.Entity.Migrations.DbMigrationsConfiguration<DbContext> { public MigrateDBConfiguration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; } } In first run the project it work fine and create database and tables. after I change model and drop some field or add new fields and run Add-Migration,

Entity Framework - Migrations - Code First - Seeding per Migration

[亡魂溺海] 提交于 2019-11-28 17:24:46
问题 I am looking into Migrations in an effort to clean up our deployment processes. The less manual intervention required when pushing a change to production the better. I have run into 3 major snags with the migrations system. They are show stoppers if I can not figure out a clean way around them. 1. How do I add Seed data per migration: I execute the command "add-migration" which scaffolds a new migration file with Up and Down functions. Now, I want to automatically make changes to the data

Entity Framework code-first: migration fails with update-database, forces unneccessary(?) add-migration

给你一囗甜甜゛ 提交于 2019-11-28 17:19:18
问题 I have a funny effect using migration (EF 5.0) and code-first: I created some models with GUID primary keys. (BTW: It is important for me, that SQL Server uses NEWSEQUENTIALID() , which seems to be the default value in the current version) At some point I activated migrations. I added some code to the initial migration, this is mostly .Index() as needed. When I delete the database and call update-database, I get the following error: Unable to update database to match the current model because

Adding CreatedDate to an entity using Entity Framework 5 Code First

放肆的年华 提交于 2019-11-28 17:14:50
I am trying to add a CreatedDate property to entities in my Model and am using EF5 Code First. I want this date to not be changed once set, I want it to be a UTC date. I do NOT want to use a constructor, as I have many entities in my model that I want to inherit from an abstract class containing the CreatedDate property, and I can't enforce a constructor with an interface. I have tried different data annotations and I have attempted to write a database initializer that would pick up a specific entity type and write an alter constraint with a getdate() default value for the correct table_name

The term 'Update-Database' is not recognized as the name of a cmdlet

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-28 16:46:34
I am using EF5 beta1 and while I was able to run the "Update-Database" before. Now that I shut down Visual Studio, I cannot get it to run. I get the following error: The term 'Update-Database' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:16 + Update-Database <<<< -verbose + CategoryInfo : ObjectNotFound: (Update-Database:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException I have tried to re-install

How to explicitly name the database when using Entity Framework Migrations 4.3

落爺英雄遲暮 提交于 2019-11-28 16:29:15
I've recently started using Entity Framework migrations and noticed that the database name is not pulling through for me when I run the Update-Database command. My connectionstring is: <connectionStrings> <add name="DataContext" connectionString="Server=.\SQLEXPRESS;Initial Catalog=TestDB;Trusted_Connection=Yes;" providerName="System.Data.SqlClient" /> </connectionStrings> The very first time I run Update-Database my database is created with the correct name TestDB . However, as soon as I make a change to one of my entities it will not update any longer for me unless I add a Start Up Project

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

丶灬走出姿态 提交于 2019-11-28 16:17:19
问题 We are using TFS and have different branches for our Dev. in the branch A we made a migration to change a column size in the branch B we made a migration to add a new table. This branch doesn not know about the branch A modification !! both modification are merged to the main branch. When I do an update database, it does the 2 migration but at the end tells me there is pending changes. If I do an Add-Migration, it creates the same as the 1st migration (in branch A). Unable to update database

Entity Framework Migrations renaming tables and columns

早过忘川 提交于 2019-11-28 16:17:13
I renamed a a couple entities and their navigation properties and generated a new Migration in EF 5. As is usual with renames in EF migrations, by default it was going to drop objects and recreate them. That isn't what I wanted so I pretty much had to build the migration file from scratch. public override void Up() { DropForeignKey("dbo.ReportSectionGroups", "Report_Id", "dbo.Reports"); DropForeignKey("dbo.ReportSections", "Group_Id", "dbo.ReportSectionGroups"); DropForeignKey("dbo.Editables", "Section_Id", "dbo.ReportSections"); DropIndex("dbo.ReportSectionGroups", new[] { "Report_Id" });

How to seed data with AddOrUpdate with a complex key in EF 4.3

随声附和 提交于 2019-11-28 15:50:54
I am trying to seed a development database with some test data. I have used context.People.AddOrUpdate(p => p.Id, people)); with much success. I have another table that I need to seed, in which I would not know the primary key. For example, I would want to AddOrUpdate based on the First and Last names matching. I am unsure how to write the Expression correctly. context.People.AddOrUpdate(p => p.FirstName && p.LastName, people); is obviously incorrect, but I hope it conveys the solution I am looking for. Try this: context.People.AddOrUpdate(p => new { p.FirstName, p.LastName }, people); If you

How do I undo the last Add-Migration command?

梦想与她 提交于 2019-11-28 15:33:54
I have created a migration using the Add-Migration command, but I'd like to change the name of that migration. How can I undo the migration command, so that I can regenerate it using the new desired name? Is it just a matter of deleting the generated files, or this could be a bad idea? If you haven't used Update-Database you can just delete the migration file. If you've run the update you should roll it back using Update-Database -TargetMigration "NameOfPreviousMigration" then delete the migration file. Reference: http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/ If you haven