ef-migrations

How to alter column to identity using entity framework dbmigration?

喜你入骨 提交于 2019-12-08 08:24:31
问题 I reverse engineered a small database using Entity Framework 5.0 and the power tools. Then I started developing a website. I hit a problem because I had forgotten to make the ID column on one of my tables an IDENTITY column. So I added a code first migration and ran it using the update-database command in the package manager console. Here is the "Up" method I wrote: public override void Up() { Sql("DELETE FROM dbo.Sections"); Sql("DELETE FROM dbo.QuestionInstances"); DropForeignKey("dbo

Implementing Cascade Delete in a self referencing table in EF Core 2

柔情痞子 提交于 2019-12-08 05:39:45
问题 How can I implement Cascade Delete in a self referencing table in EF Core 2 (code first)? (For example there is a Comment Table and man can reply to a comment and this reply can reply by another.) public class Comment { public virtual int Id { get; set; } public virtual int? ParentId { get; set; } public Comment Parent { get; set; } public virtual IList<Comment> Replies { get; set; } public virtual string Description { get; set; } public virtual Article Article { get; set; } } 回答1: The

Move property to new Entity in Entity Framework Code First Migration

岁酱吖の 提交于 2019-12-08 04:12:54
问题 I'm using Entity Framework Code First approach in my project. I have a problem with database migration. Now I have the entity public class Event { public int Id { get; set; } public string Title { get; set; } public string City { get; set; } } I have already have data in my existing DataBase. Now I want to extend City property, like this public class Event { public int Id { get; set; } public string Title { get; set; } public virtual Location Location { get; set; } } so, City become to

Set identity to the previous created column during migration

巧了我就是萌 提交于 2019-12-08 02:01:15
问题 I have a project with the CodeFirst database (Entity Framework 6) and two migration steps. Database is updated automatically by using this code in Application_Start in Global.asax: Database.SetInitializer( new MigrateDatabaseToLatestVersion<MyDBEntities, MyNamespace.Configuration>()); First migration step is creating the tables: CreateTable( "dbo.GalleryAlbum", c => new { Id = c.Int(nullable: false), //other columns..... }) .PrimaryKey(t => t.Id); CreateTable( "dbo.GalleryPics", c => new { Id

Entity Framework migrator's connection

梦想的初衷 提交于 2019-12-07 20:06:31
问题 Using the Entity Framework 5.0, I am attempting to manually migrate my code-first database during my MVC site's start up routine. To do this, I first create a DbContext instance and then run the following code: var migrator = new MigrateDatabaseToLatestVersion<DataContext, Configuration>(); migrator.InitializeDatabase(this.dataContext); I assumed the database associated with the dataContext's connection would be the one migrated. It appears though that this is not the case. Instead, it always

How to view the EF Code First Migrations changes before calling 'Update-Database'

被刻印的时光 ゝ 提交于 2019-12-07 19:21:19
问题 I am using Code First Migrations Beta 1. I would like to see the Migrations and SQL that will run before I actually call 'Update-Database' on the Package Manager Console. Is there a command to get this information on the Package Manager Console? 回答1: Take a look at the link below and search for the section headed Generating an SQL Script. ADO.Net Team Blog Run the Update-Database –TargetDatabase:"database" –Script command in Package Manager Console 来源: https://stackoverflow.com/questions

EF Migrations error: could not load type 'System.Data.Entity.Infrastructure.DbContextInfo'

人走茶凉 提交于 2019-12-07 14:54:28
问题 I am using ContosoUniversity example. I have just used Nuget to download and install code first migrations pakage. Whe I excecute update-database command it throws an error . Is there anything to do more than installing nuget package? Update-Database : Could not load type 'System.Data.Entity.Infrastructure.DbContextInfo' from assembly 'EntityFramework, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. At line:1 char:16 + update-database <<<< + CategoryInfo : NotSpecified: (:

How to specify a column Name for EF5 navigation property

旧巷老猫 提交于 2019-12-07 11:02:24
问题 I'm using EF5 code first to generate my database schema, but my new navigation property is being named in an undesirable way in the table. here is the model I'm working with. public class User { [Key] public long UserId { get; set; } ... **public virtual ICollection<ImagePermission> KeepThisNavigationName { get; set; }** } However, After I've updated my database and examine the table columns, the column is named: dbo.ImagePermission.User_UserId And I would like it to be named dbo

Is it acceptable to put seed data in the OnModelCreating method in EF code-first?

社会主义新天地 提交于 2019-12-07 07:57:12
问题 I know how to do seed data with the migrations API in EF 4.3. I've been playing with that all night. However, my ultimate goal is to get my project to the point where a user can pull it from source control and press F5 and they are good to go, database, seed data, and all. Currently code-first does an excellent job of building the DB on a fresh build but seed data isn't inserted until I do Update-Database in the package manager console. At that point it runs the seed method and inserts my

How to create CreatedOn and UpdatedOn using EF Core 2.1 and Pomelo

蹲街弑〆低调 提交于 2019-12-07 07:28:41
问题 In Code First approach, how to define my entity so that: CreatedOn NOT NULL - value is generated on insert by the db with the current timestamp Updated NULL - value is generated on update by the db with the current timestamp Sample Entity: public class MyEntity { public int Id { get; set; } public string Name { get; set; } [DatabaseGenerated(DatabaseGeneratedOption.Identity)] [Column(TypeName = "TIMESTAMP")] public DateTime CreatedOn { get; set; } [Column(TypeName = "TIMESTAMP")] public