ef-migrations

Confusion over EF Auto Migrations and seeding - seeding every program start

China☆狼群 提交于 2019-11-26 19:02:56
问题 I recently changed an application from using the following for dev: DropCreateDatabaseIfModelChanges<Context> To using: public class MyDbMigrationsConfiguration: DbMigrationsConfiguration<GrsEntities> { public MyDbMigrationsConfiguration() { AutomaticMigrationsEnabled = true; AutomaticMigrationDataLossAllowed = true; } } In my db context I have: protected override void OnModelCreating(DbModelBuilder modelBuilder) { // Tell Code First to ignore PluralizingTableName convention // If you keep

why remove-migration run my app?

谁说胖子不能爱 提交于 2019-11-26 19:02:34
I did some version upgrade in last months and now I noticed that when I use "remove-migration" to delete migration that I reverted, it's first run my app. (I noticed that because we do update-database inside startup, so I got to situation where I can't remove migrations, since every time I tried remove migration - it automatically ran the startup which apply the migration to db, then it failed delete it, since it see it in db.) any idea? Update for ASP.NET Core 2.1 In ASP.NET Core 2.1 the methods changed slightly. The general method is similar to the 2.0, just the methods name and return types

MVC3 and Code First Migrations - “model backing the 'blah' context has changed since the database was created”

不羁的心 提交于 2019-11-26 18:50:25
I started off my project by using Entity Framework Code First. When I was ready I uploaded my database and code to my host provider. Everything worked. I need to add a new field to one of my classes and I don't want to loose the data in the database. Thus, I tried following some blog posts about using Code First Migrations. I did the following: I backed up my remote (production) database. I attached this database locally I added the property to my class PM> Enable-Migrations PM> Add-Migration AddSortOrderToCar PM> Update-Database At this point I created a .bak file of the local database and

EF Migrations: Rollback last applied migration?

大兔子大兔子 提交于 2019-11-26 18:42:15
问题 This looks like a really common task, but I can't find an easy way to do it. I want to undo the last applied migration. I would have expected a simple command, like PM> Update-Database -TargetMigration:"-1" Instead, all I can come up with is: PM> Get-Migrations Retrieving migrations that have been applied to the target database. 201208012131302_Add-SystemCategory 201207311827468_CategoryIdIsLong 201207232247409_AutomaticMigration 201207211340509_AutomaticMigration 201207200025294

How to delete and recreate from scratch an existing EF Code First database

隐身守侯 提交于 2019-11-26 18:30:20
I am using EF Code First with EF 5 in VS 2012. I use PM update-database command and I have a simple seed method to fill some tables with sample data. I would like to delete and recreate my x.mdb. The update history seems to be out of sync. If I comment out all my DBSets in my context, update-database runs with no error but leaves some tables in the DB. As I have no valuable data in the DB it seems to the simplest to reset the all thing. How can I accomplish this? NSGaga If I'm understanding it right... If you want to start clean : 1) Manually delete your DB - wherever it is (I'm assuming you

How can I disable migration in Entity Framework 6.0

送分小仙女□ 提交于 2019-11-26 17:35:56
问题 I'm trying to ignore the "Automatic" migration using Entity Framework 6.0 rc1. My problem is that I don't want this feature right now and every time that my application runs I can see all entity logs trying to create all tables. Anticipate thanks. 回答1: Try this: internal sealed class Configuration : DbMigrationsConfiguration<YourContext> { public Configuration() { AutomaticMigrationsEnabled = false; } } UPDATE: You can also try this: Database.SetInitializer<YourContextType>(new

Value cannot be null. Parameter name: entitySet

和自甴很熟 提交于 2019-11-26 16:58:13
问题 I have a fairly standard setup with simply POCO classes public class Project { public int ProjectId { get; set; } public string Name { get; set; } public int? ClientId { get; set; } public virtual Client Clients { get; set; } } They use an interface public interface IProjectRepository { IEnumerable<Project> Projects { get; } } and are constructed as a repository for ninject to bind to public class EFProjectRepository : IProjectRepository { private EFDbContext context = new EFDbContext();

Seed Entities AND Users, Roles?

℡╲_俬逩灬. 提交于 2019-11-26 16:33:05
问题 How do you seed users, roles and app specific entities? It appears as though the IdentityModel targets its own Context? internal sealed class Configuration : DbMigrationsConfiguration<Project.Models.SchoolContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(Project.Models.SchoolContext context) { // Seed the Entities // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" } // ); // } } vs. protected

LINQ to Entities does not recognize the method 'System.String ToString()' method in MVC 4

末鹿安然 提交于 2019-11-26 16:12:28
问题 I'm working with MVC 4 and I have to update my database using Code First Migrations. What I'm trying to do is to select records from a database table, and insert them into a dropdown list where the user can select one. I have an error that I don't understand: LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression. Controller: public ActionResult Addnew() { var dba = new DefaultConnection(); var query = dba

AutomaticMigrationsEnabled false or true?

坚强是说给别人听的谎言 提交于 2019-11-26 16:05:28
问题 In EF projects, Is there any best practice for setting AutomaticMigrationsEnabled ? More declaration: In our team after modifying a model we usually run "add-migration" and "update-databse" commands in Package Manager Console. This error raises when other developers run the project: "Can not drop database because it is in use" Every time this happen the first modifier should Check In whole project and others have to GET modified objects. In many cases we don't want to check in the already