FAQ Database Discussion Community


ASP.NET migrations is creating duplicate bridge entities

c#,visual-studio-2013,asp.net-mvc-5,entity-framework-6,code-first-migrations
I am designing a web application with ASP.NET using MVC 5 and EF 6. Our team uses code first migrations to design our database. In our project we have two models - Location and Recreation. Recreation is essentially an enumerable tag that can be applied to any Location (many-to-many relationship),...

Merging migration entries in Entity Framework

c#,.net,entity-framework,entity-framework-6,code-first-migrations
I have an Entity Framework 6 CF project that has a few migrations already in place. The model is now stable and there is no need to keep the migration history that already exists. Is there a way to reset the model and merge all migration commands into the initial...

EF6 migrations merged in script

sql-server,entity-framework,entity-framework-6,code-first-migrations
I've got an Entity Framework 6 migration which adds a column to a table and runs some SQL to populate the new column with the correct data. The migration is as follows: AddColumn("SiteBranches", "SiteId", c => c.Int(nullable: false, defaultValue: 0)); Sql(@"WITH WithSiteId AS ( --get the relevant data ) UPDATE...

Enable Migrations in Multi-Tier MVC Application

asp.net-mvc,asp.net-web-api,entity-framework-6,code-first-migrations
I am creating a new application using MVC 5 (Razor Views), Entity Framework 6 (Code First), ASP.Net Identity 2.0 and Web API. Trying to create a decent de-coupled architecture, I wanted to enable migrations in my data layer, but I have a misunderstanding of how migrations work. Here's my basic...

Entitiy Framework code-first migrations in multiple development environments

sql-server,entity-framework-6,code-first-migrations
I'm working on a project that uses EF6 with code-first migrations. All the work till now was done on a Dev. environment, including the DB migrations. I need to deploy the code to another environment (QA), however I've stumbled into a problem: The DB exists, however there are no tables...

How to start using EF code first migrations on an existing set of databases whilst also using LocalDB for testing

entity-framework,code-first-migrations,localdb
I am working on a system that currently has a number of environments (test, stage, live, etc) each with their own database. So far these databases have been kept in sync by running the same update scripts on each of them. We are now moving to using EF6 code first...

Use Entity Framework to update column length and data in single migration

entity-framework,code-first-migrations
I'm using Entity Framework with code first migrations. I need to increase the length of a VARCHAR(50) column to VARCHAR(100) and update all the records in that column by doubling the string. So "abc" turns into "abcabc" (except the values will be longer than three characters). It would be nice...

Can one set a breakpoint in EF code first migrations seed method?

c#,asp.net,entity-framework,ef-code-first,code-first-migrations
I am having trouble with something in the Seed method in the Configure.cs for my entity framework 6 code-first migration process. I am running the Update-Database -verbose command in the Package Manager Console, and tried to set breakpoints (in VS studio web express 2013) in the c# code of the...

Codefirst and migrations

entity-framework,ef-code-first,code-first-migrations
When an application is Live an iterative approach to database changes is obviously required. In the db first world I would change the object (eg. column added to table) in the databaae project, then deploy (recreate) to my local instance, then replace the old table with the new in my...

How to create and add initial data row to new table using Entity Framework 6 Code First migrations

sql-server,entity-framework,code-first,code-first-migrations
Working with Entity Framework 6 Code First migrations, I need to add a new table to the DB and then add a foreign key to that new table in an existing table (with lots of data in it). Since the existing table already has lots of data in it, and...

EF Code first - many to many relation mapping table with extra columns

entity-framework,ef-code-first,entity-framework-6,code-first-migrations
I am having a User Model and and a Group Model. User and Group share a many to many relationship. In when I translate this to table, I want to have a mapping table. I am using the following to achieve this. modelBuilder.Entity<UserGroup>() .HasMany(a => a.Users) .WithMany(b => b.UserGroup) .Map(mc...

Seed method keeps generating duplicate data using AddOrUpdata and DropCreateAlways (Code First, EF)

c#,entity-framework,code-first-migrations
I have a problem when seeding my database using a code first approach in EF. I find this odd since I'm using the AddOrUpdate method. Here's the code: using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MovieModel { public class MovieContext : DbContext { public...

Using bcp.exe to successfully import data, even if the column order in the format file differs to that of the table

sql-server,entity-framework,code-first-migrations,bcp
I've been creating a system for my team which allows us to automatically generate large amounts of test data so that we can return our database to a consistent state when we need it. This needs to be fully automated, but I currently have one issue that's preventing that from...

Entity Migration. Could not drop constraint. See previous errors

c#,entity-framework,code-first-migrations
Here is the code that produce this error. I 've tried solution for renaming the AddForeignKey but the error is same. 'PK_dbo.Item' is not a constraint. Could not drop constraint. See previous errors. Can you suggest some solution? public override void Up() { DropForeignKey("dbo.AddGallery", "item_fk_id", "dbo.Item"); DropForeignKey("dbo.ExtraFieldValue", "item_fk_id", "dbo.Item"); DropPrimaryKey("dbo.Item");...

How can I connect the ASP.NET Identity database with my own database?

asp.net,entity-framework,asp.net-identity,code-first-migrations
I want to have all the tables in one place, something like the situation in this question. Which migrations should I run and how?

EF Migrations - should an initial migration be automatically generated?

entity-framework,code-first-migrations
I have a project that uses EF code-first. It has automatically generated a DB from the model. Now, I want to enable EF migrations. The 'Getting Started' docs on EF Migrations suggest that in this scenario (DB already exists), two things should happen when calling Enable-Migrations: the project gets updated...

Add more default data in existing database using ef6 code first migrations

entity-framework-6,code-first-migrations,production-environment
Is it possible to create a migration that includes only data changes? Or is it only a change to the data model that will trigger the creation of a new migration? I found myself in a situation where I realized that some default data weren't included in the last migration...

Adding a migrations gives filename already exists

.net,entity-framework,ef-code-first,code-first,code-first-migrations
I have enabled migrations as such: enable-Migrations -ProjectName ProjectOne -ContextTypeName MyIdentity.Config.MyIdentityContext -MigrationsDirectory Identity\\Migrations I specified my context as it is in a separated namespace, and i specified the directory because i want to have the migrations in a different directory. After enabling the migrations like this, i get the expected...

Entity Framework 6 and migration when I don't have a default Database

c#,database,entity-framework,code-first-migrations
I have a solution where I do not have one default database. I have a master Database that are returning a connection string for the customer that is requesting data, and each customer has their own database. I am using migration (and has AutomaticMigrationsEnabled set to false) and code first....

Deploying to Azure not running EF Code First Migrations

entity-framework,azure,ef-code-first,code-first-migrations,ef-migrations
I am running Entity Framework with Code First Migrations. My new release adds a table, modifies a few tables, and run some scripts. This works perfectly when developing locally using update-database. Upon deployment the new table was not created and I was receiving errors from my client. I attached the...

Code First Migration Entity Framework 6 Empty Up/Down

entity-framework-6,code-first-migrations
The problem I'm having is that when I "Add-Migration", it seems that no comparison against the target database is done and the Up/Down methods are empty. This starts from me targeting different environments. The migration changes work fine when I update-database and it targets one particular database, my dev database....

Multiple cascade paths Entity Framework

entity-framework,ef-code-first,code-first-migrations
My code-first Entity Framework model is as follows: In my database I have customers, purchases and items. A customer may have several purchases Each purchase may consist of several purchased_item. Each purchased_item makes reference to an item. So, when configuring my entities with the FluentAPI I have the following configurations:...

Mixing automatic and explicit migrations on the production environment

entity-framework,entity-framework-6,code-first-migrations
I've seen this question, and I'm wondering how automatic migrations mixed with explicit migrations would be applied on the production environment. Let us say I create migrations in the following order: Explicit Migration A Explicit Migration B Automatic Migration C Explicit Migration D Since no file is created for the...

Invalid Username or password on Seeded User Account MVC5

entity-framework-6,code-first-migrations,asp.net-mvc-5.2
I am working in a new MVC5 application that I built specifically to test this. I have two db contexts. The configuration.cs for the ApplicationDbContext is as follows: using FB.DOMAIN.Authentication; using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using System.Data.Entity.Migrations; namespace FB.DAL.Migrations.ApplicationDbContext { internal sealed class Configuration : DbMigrationsConfiguration<DataContexts.ApplicationDbContext> { public Configuration() { AutomaticMigrationsEnabled...

Merging multiple database contexts in ASP.Net MVC

c#,asp.net,asp.net-mvc,asp.net-mvc-4,code-first-migrations
Background: I have multiple classes called ApplicationDbContext. One is located in the root and the rest are divided up amongst their modularized area directories. Currently, I have the option of handling data migrations like so. Question: How would I rewrite each of my classes in the area folder's subdirectories to...

Schema name duplication in EF6 code first migration

c#,mysql,entity-framework,code-first-migrations
Update-Database is outputting this error. Table 'external_service.external_service.products' doesn't exist Here is some Update-Database -Verbose output. Target database is: 'external_service' (DataSource: something, Provider: MySql.Data.MySqlClient, Origin: Configuration). Applying explicit migrations: [201503091803430_1.0.1]. Applying explicit migration: 201503091803430_1.0.1. alter table `external_service.products` drop column `test` This is what the db migration looks like... public partial class...

How to completely reset all Entity Framework 6 migration history? (Zombie migrations!)

.net,entity-framework-6,code-first-migrations
Short version: After deleting the entire database (including __MigrationHistory) AND all the migrations in the solution... somehow, named migrations are being found somewhere and applied by DbMigrator! Where are they coming from??? Long version: We are using Entity Framework 6, against SQL Server 2012. It's an MVC webapp, and we...

How to delete EF migration that adds a new table completely

entity-framework,migration,entity-framework-6,code-first-migrations
I am trying to delete migration but that results into some weird scenario. To create this migrations I: Created class that represents table in db Added that to my DbContext Scaffolded that migration As a result of that new table created and everything worked as expected. Until I decided that...

Create table and insert data into it during EF code first migration

c#,entity-framework,code-first-migrations
I'm using Entity Framework Code First with Code First migrations. During a migration, I need to create a new table, and then insert some data into it. So I create the table with : CreateTable("MySchema.MyNewTable", c => new { MYCOLUMNID = c.Int(nullable: false, identity: true), MYCOLUMNNAME = c.String(), }) .PrimaryKey(t...

EF creating another database instead of applying migrations to a existing one

c#,entity-framework,code-first-migrations
I can't understand why another database is being created when I run update-database command instead of applying pending migrations to the existing database. The scenario appears to happen sporadically. These are the steeps I am used to when applying migrations: Set the package manager project to MySolution.MyProject; Run the update-database...

MVC C# foreach Loop passing button selection

c#,asp.net-mvc,code-first-migrations
I have a listing view which shows the jobs extracted from the database. Each Job has a button next to it. When I move to the next page, i need to carry the specified job, display it and later save it to my database. This is inside my Controller class....

How to define a table that its primary key is constructed from 2 foreign keys with EF code-first

c#,entity-framework,entity-framework-4,ef-code-first,code-first-migrations
I am using models to define my tables using EF code-first. I have and Item model and an Order model. Item: public class Item { public int ItemID { get; set; } [Required] public int Price { get; set; } [Required] public int AmountLeft { get; set; } [Required] public...

How to alter relationship keeping existing data in entity framework code first?

entity-framework,ef-code-first,code-first-migrations
I have two entities public class Account { [Key] public int Id { get; set; public int MemberInfoId { get; set; } public Member MemberInfo { get; set; } //Other Properties } public class Member { [Key] public int Id { get; set; } //Other Properties } With following relationship...

Entity Framework telling me the model backing the context has changed

entity-framework,code-first-migrations
I have a weird problem with Entity Framework code first migrations. I've been using EF and code first migrations on a project for months now and things are working fine. I recently created a new migration and when running Update-Database a restored backup of my database I get this error:...

Entity Framework 7 Reverse Engineering ASP.NET 5

c#,asp.net-mvc,code-first-migrations,entity-framework-7
I have been trying to migrate my database into web app model using EF7 _4. Couple things I realized is that the syntax/approach how to configure particular entities has been changed. Normally you generate POCO classes by using EF Power Tools (EF6), which generates entity types and entity map as...

.NET Code first migration for SQL: adding migration to an existing database

c#,sql,.net,entity-framework,code-first-migrations
This might seem like a trivial question, however I come from a java background and pretty new to .NET so bare with me. I was put on a C# project that already has an SQL database on azure and asked to implement an automatic code first migration system. We have...

DbMigration.SqlFile difference in base directory

c#,entity-framework-6,code-first-migrations
We are using the new DbMigration.SqlFile method in EF Migrations 6.1.2 to run a migration script in our migration. According to the documentation, the file has to be relative to the current AppDomain BaseDirectory. We have included these files in the project, and set them to copy to output directory....

Entity FrameWork 7(beta 4) migrations error

asp.net,entity-framework,database-migration,code-first-migrations,visual-studio-2015
Who else is getting this error from the Package Manager console when he/she runs the 'Add-Migration Blah' command add-migration : The term 'add-migration' 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...

Code First Migration tries to re-create enitre db instead of updating changes

asp.net-mvc,ef-code-first,code-first-migrations
Im trying nto enable Migration with code first but im having some issues. Im working on a mvc5 website and I have a dll with the datalayer, dll with the domain classes and then the website itself. I have already run the website and all the tables have been created....

Entity Framework 7 Migration Scaffolding in Class Library with Configuration

code-first-migrations,asp.net-5,visual-studio-2015,entity-framework-7,dnx
Trying to add migrations to an EF7 Model that lives in an ASP.NET 5 class library. When running dnx . ef migration add mymigration fails with different results depending on which project I run it on. If I run it in the folder of the main project, it cannot locate...

Entity Framework Migrations: Constructor of Configuration called always

c#,entity-framework,code-first-migrations
I created a simple console application, install EntityFramework from nuget and execute command Enable-Migrations. Here is the code of main: using(var ctx = new AppDbContext()) { var persons = ctx.Persons.ToList(); Console.ReadKey(); } Here is the code of the class Configuration internal sealed class Configuration : DbMigrationsConfiguration<AppDbContext> { public Configuration() {...

How to restore database EF migration tools

c#,entity-framework,code-first-migrations
I got a database, and accidentally I deleted the __MigrationHistory table. Now the program throws error, and I can't loss all the datas. Is there anyway to restore the table that I deleted?? Will I lost all my database? I added the __MigrationHistory manually, and now this is the error:...

Teamcity build fails because of EF code migrations

entity-framework-4,teamcity,code-first-migrations,teamcity-8.0
My TeamCity build fails because I have a project that has 2 EF Code Migration configurations in it. From the build log: [12:39:58]Checking for changes [12:39:58]Collecting changes in 1 VCS root (1s) [12:40:00]Clearing temporary directory: C:\TeamCity\buildAgent2\temp\buildTmp [12:40:00]Publishing internal artifacts [12:40:00]Checkout directory: C:\TeamCity\buildAgent2\work\1679b8b30e00ad0 [12:40:00]Updating sources: server side checkout (2s) [12:40:03]Step 1/8:...

Getting a migration exception when trying to create property of String in VB.Net

vb.net,entity-framework-6,code-first-migrations
I am trying to create a non-mapped String property in an entity; however, the migration fails to create the migration class. However, it works using Integer. The following shows what I have tested: Private _testProp As Integer <NotMapped> _ Public Property TestProp() As Integer Get Return _testProp End Get Set(ByVal...

No Code First Migrations When Deploying Via Visual Studio 2013

azure,visual-studio-2013,migration,code-first-migrations
I've written an API with no azure connection currently. I am now ready to publish this to Azure. In doing so, I assumed, as was the case with many other deployments, there would be an option for code first migrations. The website already exists, so when I log in I...

EntityFramework 6 CODE FIRST - Dont drop database when model changes

c#,entity-framework-6,code-first,code-first-migrations
I am changing my model at runtime. After that I need to recreate database according to new model. I cannot use DropCreateDatabaseIfModelChanges because I need to prevent data loss and I don't want to drop the database. What I would need is to check for tables and delete non existing...

Should I seed DB in DropCreateDatabaseIfModelChanges or in DbMigrationsConfiguration?

c#,entity-framework,ef-code-first,entity-framework-6,code-first-migrations
I have a working code first entity model with a model change policy in place: public class MyModelChangePolicy : DropCreateDatabaseIfModelChanges<MyDBContext> { protected override void Seed(MyDBContext context) { //Add the enumeration type values context.SyncEnums(false); base.Seed(context); } } SyncEnums is an extension method I created which loads data into several enumeration type...

EF Code First Migration insists on switching junction table names

c#,entity-framework,code-first-migrations,junction-table
I have an EF Code First model containing a table for Foo and a table for Bar. This is a many to many relationship so EF generated a junction table called FooBars: CreateTable( "dbo.FooBar", c => new { Foo_Id = c.Int(nullable: false), Bar_Id = c.Int(nullable: false), }) .PrimaryKey(t => new...

entity framework looking at wrong database

entity-framework,asp.net-web-api,sql-server-2012,code-first-migrations
VS2013 WebAPI 2.x with Repository has suddenly begin to look for data migrations to happen on a database instance other than the one it has been working with for the past 3 months - EF version 6 I have SQL Server 2012 (v11.0.2218.0) as ron-hp\localdb - this has accepted 12...

EF Code first migrations not running after deploy to Azure

entity-framework,azure,ef-code-first,code-first-migrations
I have two folders for my migrations (AuthContext and UserProfileContext), each has their own migration and some custom sql to run afterwards for data migrations and whatnot. This works fine when using package manager console. I Restore from production Run Update-Database -ConfigurationTypeName Migrations.Auth.Configuration Run Update-Database -ConfigurationTypeName Migrations.UserProfile.Configuration Then everything is...

EF multi-context with a plugin-style system. How to apply migrations at runtime?

c#,entity-framework,code-first-migrations
I have a web application which is supposed to be composed as a series of plugins into a core infrastructure. A plugin is a compiled CLR dll + some content files which will be put in a certain location. I'm using Autofac to scan and register types out of the...

How do you perform a rollback when using MigrateDatabaseToLatestVersion

entity-framework,ef-code-first,code-first-migrations,ef-migrations
With EF Migrations you can add migration scaffoldings and then keep running 'Update-Database' to apply changes. And in order to rollback the following works: Update-Database -TargetMigration: <xyzMigration> This is great for updating your dev. DB. However when I automate migrations using MigrateDatabaseToLatestVersion for deploying to other environments like test and...

How to run sql script on every EF migration

entity-framework,code-first-migrations
I want to run some custom Sql after every migration. I was hoping there was a global event or something I could tap into instead of putting my code in each individual migration. What am I doing? I'm going to traverse the dbcontext via reflection to find enums to dump...