ef-migrations

Best way to incrementally seed data in Entity Framework 4.3

大憨熊 提交于 2019-11-26 15:37:42
问题 I have been using Entity Framework 4.3 on an existing database and I have a couple of scenarios that I am trying to cater for. Firstly, if I delete my database I would like to EF to recreate if from scratch - I have successfully used a CreateDatabaseIfNotExists database initialiser for this. Secondly, if I update my model and the database already exists I would like the database to be updated automatically - I have successfully used Entity Framework 4.3 Migrations for this. So here's my

Generate full SQL script from EF 5 Code First Migrations

半城伤御伤魂 提交于 2019-11-26 14:59:18
问题 How do I use Entity Framework 5 Code First Migrations to create a full database script from the initial (empty) state to the latest migration? The blog post at MSDN Blog suggests to do this, but it seems to create an empty script: Update-Database -Script -SourceMigration: $InitialDatabase 回答1: The API appears to have changed (or at least, it doesn't work for me). Running the following in the Package Manager Console works as expected: Update-Database -Script -SourceMigration:0 回答2: To add to

Using Entity Framework (code first) migrations in production

一曲冷凌霜 提交于 2019-11-26 14:53:33
I'm just looking into using EF migrations for our project, and in particular for performing schema changes in production between releases. I have seen mentioned that there is an API to perform these migrations at run-time using the DbMigration class, but I can't find any specific examples. Ideally, I would want one DbMigration file for every database change, and for those changes to be applied automatically on application start up from the current version up to the latest version. WDRust There is a Database Initializer you can use to achieve the migration to latest version on startup (or

Adding 'GO' statements to Entity Framework migrations

笑着哭i 提交于 2019-11-26 14:23:10
问题 So I have an application with a ton of migrations made by Entity framework. We want to get a script for all the migrations at once and using the -Script tag does work fine. However...it does not add GO statements in the SQL giving us problems like Alter view should be the first statement in a batch file... I have been searching around and manually adding Sql("GO"); help with this problem but only for the entire script. When I use the package console manager again it returns an exception.

How to disable migration in Entity Framework 4.3.1?

谁都会走 提交于 2019-11-26 13:56:14
问题 Is there any way to disable migration in Entity Framework 4.3.1? I removed the migrations folder from the project and the generated tables in my database, but it doesn't work! How can you remove the migration? 回答1: If you don't want to use migrations but in the same time you want EF to create database for you, you just need to set correct database initializer: Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists<YourContentType>()); 回答2: Deleting the Migrations folder has

Entity framework 4.3 run migrations at application start

帅比萌擦擦* 提交于 2019-11-26 13:48:55
问题 What is the best way to execute all required db migrations at application start with EF 4.3? 回答1: The best way should be using new MigrateDatabaseToLatestVersion initializer. Database.SetInitializer<YourContext>( new MigrateDatabaseToLatestVersion<YourContext, YourMigrationsConfig>()); Database.Initialize(false); 回答2: A great description of the EF 4.3 configuration options can be found at EF 4.3 Configuration File Settings on the ADO.NET team blog. The very last section describes Database

EF 4.3 Auto-Migrations with multiple DbContexts in one database

99封情书 提交于 2019-11-26 12:09:21
问题 I\'m trying to use EF 4.3 migrations with multiple code-first DbContexts. My application is separated into several plugins, which possibly have their own DbContext regarding their domain. The application should use one single sql-database. When I try to auto migrate the contexts in an empty database, this is only successful for the first context. Every other context needs the AutomaticMigrationDataLossAllowed-Property set to true but then tries to drop the tables of the previous one. So my

Multiple DB Contexts in the Same DB and Application in EF 6 and Code First Migrations

核能气质少年 提交于 2019-11-26 11:59:53
I'm new to Entity Framework. I am trying to setup an MVC Application what uses EF 6. I am using Code First Migrations. I am using Areas in the app and would like to have different DbContexts in each area to break it up. I know EF 6 has ContextKey, but I can't find complete information on how to use it. Currently I can only use migrations one context at a time. Can someone give an example with enough detail for a new person to EF like me to understand and use. Entity Framework 6 added support for multiple DbContext s by adding the -ContextTypeName and -MigrationsDirectory flags. I just ran the

Enable Migrations with Context in Separate Assembly?

本秂侑毒 提交于 2019-11-26 10:19:39
问题 I have one project that I want to run my update-database against but I have my Models and Context in a separate project. If I run enable-migrations I get this error: No context type was found in the assembly \'MyProject\'. This is presumably because my Context is in MyProject.MVC. If I run enable-migrations against MyProject.MVC I have to add an app config file. I don\'t want to do that as I want to use the code across many projects. So can I run enable-migrations against MyProject and

EF migration for changing data type of columns

老子叫甜甜 提交于 2019-11-26 09:28:13
问题 I have a Model in my project as below: public class Model { public int Id { get; set; } public long FromNo { get; set; } public long ToNo { get; set; } public string Content { get; set; } public long TicketNo { get; set; } } The migration is as below public override void Down() { AlterColumn(\"dbo.Received\", \"FromNo\", c => c.Long(nullable: false)); AlterColumn(\"dbo.Received\", \"ToNo\", c => c.Long(nullable: false)); AlterColumn(\"dbo.Received\", \"TicketNo\", c => c.Long(nullable: false)