ef-migrations

Where should EF Migrations go, my Class Library project or my ASP.NET project?

你说的曾经没有我的故事 提交于 2019-12-03 14:19:48
My solution contains: FooBarAsp , an asp project (provides a UI for the application) FooBar , a class library (the application) FooBar.Tests , a test project (tests the application) FooBar uses EF 6 Code First, and contains a number of models, and a DataContext . FooBarAsp uses Microsoft's Identity framework for user authentication, and has an ApplicationDbContext . Both contexts play nice and work as expected. Global.asax.cs should execute MigrateDatabaseToLatestVersion (right?). FooBar.Tests should execute DropCreateDatabaseAlways and won't care about migrations (right?). Should I

Entity Framework Core: Is it safe to delete Migration.Designer.cs if we will never Revert a migration?

心不动则不痛 提交于 2019-12-03 13:36:46
We have a database schema with ~200 tables. Model snapshot (Migration.Designer.cs) which is created for each migration is ~20K lines. So, having quite a number of migrations really slows down our build on CI (with ~30 migrations building a solution takes 6 minutes with migrations or 4 minutes without them). So, to the question: is it safe to delete model snapshots for old migrations (that we know we will never Revert)? Are model snapshots used for anything else except Revert-Migration? Are model snapshots used for anything else except Revert-Migration? Yes. There are a few edge cases where it

Drop or Recreate database with Entity Framework Migrations (Code-First)

雨燕双飞 提交于 2019-12-03 13:14:36
Which is the command to recreate or drop the database when using Entity Framework Migrations, not Initializers ? What should I write on the package manager console ? COMMENT : I'm looking for some command that gives me the same functionality as Database.SetInitializer<>(new DropCreateDatabaseIfModelChanges<>()); but with the migrations approach. You can get the same behavior with Migrations by using automatic migrations. PM> enable-migrations -EnableAutomaticMigrations In Configuration.cs in the constructor, make sure automatic migrations and allow data loss are set to true... public

Entity Framework Code First: One-to-Many and Many-to-Many relationships to same table

喜你入骨 提交于 2019-12-03 12:12:26
I have a User model and a Event model in my project. The Event has a creator(User) and has participant(Users) so Event has a one-to-many relationship with User and also a many-to-many relationship to the same table. I had first the one-to-many relationship like this: Public class Event { ... public int CreatedById { get; set; } public virtual User CreatedBy { get; set; } ... } Then when I added the many-to-many relationship the migration doesn't generate the many to many relationship: Public class User { ... public virtual ICollection<Event> Events { get; set; } ... } Public class Event { ...

EF - Moving from AutomaticMigrations to Manual Migrations

痴心易碎 提交于 2019-12-03 12:08:20
问题 End of long day of testing various scenarios where I don't have to recreate a production database... We started off with EF, and didn't get wise enough during development to move from automatic migrations to named migrations. Now I'm trying to rewind the clock, and create an initial migration that aligns with the production database. Is this possible to align a model has with an automatic migration has in the migration table? Should I just create an empty migration to get started with named

Naming conventions for Code First migrations

有些话、适合烂在心里 提交于 2019-12-03 12:02:48
We are using code first migrations to keep our database and model in sync. At the moment we have the version number as name for the migration which clearly doesn't work out. The problem is that multiple migrations with the same name where created by different developers independent of each other for their local database. This led to some weird behavior as the IMigrationMetadata.Id was different because of the time stamp but the classes are partial with the same name. What is the way to go to call these migrations? The examples are always ridiculously oversimplified: e.g. adding a property

Cannot create more than one clustered index on table

不羁的心 提交于 2019-12-03 11:58:45
I am having the following error after typing update-database: Cannot create more than one clustered index on table 'dbo.AppUsers'. Drop the existing clustered index 'PK_dbo.AppUsers' before creating another. I am working on an Azure mobile service. I have three data models: public class AppUser : EntityData { public string Username { get; set; } public virtual ICollection<RatingItem> userRatings { get; set; } } public class PlaceItem : EntityData { public string PlaceName { get; set; } public int Group { get; set; } public string XCoordinate { get; set; } public string YCoordinate { get; set;

EF Code First MigrateDatabaseToLatestVersion accepts connection string Name from config

人盡茶涼 提交于 2019-12-03 11:34:03
问题 While trying to implement EF Migrations in my project I am stuck at one place. EF Code First MigrateDatabaseToLatestVersion accepts connection string Name from config. In my case database name get known at Runtime (User selects it from dropdown). Just the way DbContext either accepts, ConnectionString or connectionString Name in it's constructor, "MigrateDatabaseToLatestVersion" does not accept the same System.Data.Entity.Database.SetInitializer (new MigrateDatabaseToLatestVersion

Net Core 2 - Entity Framework: Update-Database of different environment

≯℡__Kan透↙ 提交于 2019-12-03 09:41:27
FACTS: net core 2.0 project entity framework (code first) different appsettings.json file for different environments I utilize Package Manager Console to generate my DB scripts (Add-Migration, Update-Database) If I run PM>"Get-DbContext" it brings back info pulled from my appsettings.Development.json file GREAT, that's what I want most of the time! But how do I tell it to pull db variables from appsettings.Staging.json instead of development for PM commands? I tried creating new launchSettings.json profiles and setting "ASPNETCORE_ENVIRONMENT": "Staging" but everything seems to respect that

Getting Initial Entity Framework Migrations Script

白昼怎懂夜的黑 提交于 2019-12-03 09:25:01
问题 I just installed Entity Framework Migrations, added a property to a class, and gave EF Migrations a whirl. My development database was promptly updated. So far, so good. Now, I want to create a change script for this initial use of Migrations for the production database. Note there was a pre-existing database because I applied this to an existing project. The migrations I have are: PM> Get-Migrations Retrieving migrations that have been applied to the target database. 201204102238194