ef-migrations

How to apply migrations from code (EF Core)

微笑、不失礼 提交于 2019-12-18 02:27:06
问题 How to apply migrations from code for EF6 work code Database.SetInitializer<CmContext>(null); var settings = new MigrationsConfiguration(); var migrator = new DbMigrator(settings); migrator.Update(); how to make similar in EF Core? 回答1: In beta 7 and on, use: using Microsoft.Data.Entity; ... context.Database.Migrate(); 回答2: For Entity Framework Core 1.0.0, ensure you have the Microsoft.EntityFrameworkCore.Relational NuGet package. Then import this namespace: using Microsoft

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

点点圈 提交于 2019-12-17 21:42:21
问题 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

migration synch developmental and production databases

只愿长相守 提交于 2019-12-17 21:23:49
问题 I am using MVC 5 with NET Framework 4.5.1. with Code-first. I am also using Migrations with the SQL 2012 server and (localdb)\v11.0. I am in the middle of developing a project using C# and MVC5. During development, I created a lot of new tables in my developmental computer and changed a the "Name" field which I believe the system makes an index for. I added it and deleted it several times. After that, I added a lot iof new unrelated tables, but for some reason, my migrations started giving me

Database.SetInitializer to null not working Entity Framework 4.3.1 Code First

孤街醉人 提交于 2019-12-17 19:52:36
问题 I have a context class that inherits from an abstract base AuditableDbContext : DbContext . The AuditableDbContext takes two parameters, one for the auditor and one for the context to audit into. In the inherited class, I have a default parameterless constructor that calls the other constructors with null parameters and then in the final constructor I call Database.SetInitializer<MyDbContext>(null) after calling the base constructor. The problem is that even when I do this, I still get the db

Code first custom SQL migration timeout exception

老子叫甜甜 提交于 2019-12-17 19:12:19
问题 I am trying to create FULL TEXT index using Entity Framework Migration by executing custom Sql. My migration class looks like this: public partial class DocumentContentFullTextIndex : DbMigration { public override void Up() { AlterColumn("dbo.Attachments", "ContentType", c => c.String(maxLength: 260)); Sql("CREATE FULLTEXT CATALOG FullTextIndexes AS DEFAULT;", true); Sql(@"CREATE FULLTEXT INDEX ON [Attachments]( Content TYPE COLUMN ContentType Language 'ENGLISH' ) KEY INDEX [PK_dbo

EF Data migrations won't detect changes when adding new migration

你。 提交于 2019-12-17 18:57:10
问题 I am using Entity Framework 5.0 Data migrations along with code first. When i add a new field to my model and execute the following command in the package manager console. "Add-migration AddedField" All I get is an empty migration called "n_AddedField", the up and down methods contain no logic. I tried a bunch of things, reinstalling the EF nuget package, cleaning my solution, rebuilding, manually removing all generated files and directories. Then i decided that i would scrap all my

Unable to generate an explicit migration in entity framework

无人久伴 提交于 2019-12-17 17:33:50
问题 I am adding a new migration but this message shows: Unable to generate an explicit migration because the following explicit migrations are pending: [201203170856167_left]. Apply the pending explicit migrations before attempting to generate a new explicit migration. Can any one help me? 回答1: It tells you that there is some unprocessed migration in your application and it requires running Update-Database before you can add another migration. 回答2: I had the same problem. Apparently entity

Entity Framework Code First Migrations: Set Primary Key Value

前提是你 提交于 2019-12-17 16:30:13
问题 I have a table that stores some extra data for some rows of a table like: public class QuoteExtra { [Key] public int QuoteId { get; set; } // More fields here } I'd like to be able to add rows to this table where I explicitly set the PK. If I simply leave it as above, setting a value and submitting the row causes the value to be discarded and replaced with the auto-generated value from the database (and the column is defined as an Identity column in the actual schema). This appears to be the

Migrations in Entity Framework in a collaborative environment

空扰寡人 提交于 2019-12-17 08:23:37
问题 We have multiple developers working on a project that uses Entity Framework 5.0. Every developer uses his own local SQL 2012 database so he can develop and test without impeding others. At first, we used a hybrid of automatic migrations and code-based migrations. That didn't work well at all so we decided to disable automatic migrations and to only allow code-based. I should add that we started again with a clean database without a 'corrupted' _MigrationsHistory from all the automatic

Entity Framework Code First Using Guid as Identity with another Identity Column

两盒软妹~` 提交于 2019-12-17 08:16:30
问题 a.k.a How can we create multiple identity columns in Code First? Because of clustering performance, a common recommendation is to use an autoincremented integer column instead of a GUID created with newid() . In order to declare a column as autoincrement, you have to specify it with the Annotation [DatabaseGenerated(DatabaseGeneratedOption.Identity)] . But, you can only have one identity in a table. So starting with a base model like: public abstract class ModelBase { // the primary key