2014-04-14 3 views
0

alredy 기존 데이터베이스에서 내 seed 메소드를 실행할 수 없습니다. 이 데이터베이스는 다중 마이그레이션의 결과로, 우리가 만든 데이터베이스 "Whitelabel"을 ASP.NET MVC5 Identity 데이터베이스 ("IdentityDb"라고 함)와 함께 마이그레이션했습니다. 여기 update-database fires : "이미 데이터베이스에 'Adresses'라는 개체가 있습니다."

데이터베이스 테이블의 개요입니다 :

PM> update-database -ConfigurationTypeName DAL.DataContexts.WhitelabelMigrations.Configuration 

:

enter image description here

이 코드 첫째되어 있기 때문에,이 명령을 사용하여 데이터베이스를 업데이트하기 위해 패키지 관리자 콘솔을 사용 그리고 그것은 "데이터베이스에 이미 'Adresses'라는 개체가 있습니다."라는 오류가 발생합니다.

여기 WhitelabelDb 컨텍스트가 있습니다.

using ClassLibrary.Entities; 
using Microsoft.AspNet.Identity.EntityFramework; 

namespace DAL.DataContexts 
{ 
public class IdentityDb : IdentityDbContext<ApplicationUser> 
{ 
    public IdentityDb() 
     : base("WhitelabelDb") 
    { 
    } 

} 
} 

여기 WhitelabelMigrations에 대한 configuration.cs 파일입니다 :

using System.Data.Entity.Migrations; 
using System; 
using System.Collections.ObjectModel; 
using System.Data.Entity.Migrations; 
using System.Linq; 
using ClassLibrary.Entities; 


namespace DAL.DataContexts.WhitelabelMigrations 
{ 
internal sealed class Configuration : DbMigrationsConfiguration<WhitelabelDb> 
{ 

    public Configuration() 
    { 
     AutomaticMigrationsEnabled = true; 
     MigrationsDirectory = @"DataContexts\WhitelabelMigrations"; 
    } 

    protected override void Seed(WhitelabelDb context) 
    { 
     **//This is the seed method I want to run when I update the database:** 
     if(!context.CategorySet.Any(c => c.Name == "Root")) 
     { 
      Category mainCategory = new Category { Id = Guid.NewGuid(), Name = "Root", Parent = null }; 
      context.CategorySet.Add(mainCategory); 
      context.SaveChanges(); 
     } 

    } 
    } 
    } 

가 그리고 InitialCreate.cs 시스템을 사용하여 WhitelabelMigrations 신청을

using System.Data.Entity; 
    using ClassLibrary.Entities; 

    namespace DAL.DataContexts 
    { 
    public class WhitelabelDb : DbContext 
    { 
    public WhitelabelDb() 
     : base("WhitelabelDb") 
    { 
    } 

    public virtual DbSet<Category> CategorySet { get; set; } 
    public virtual DbSet<Product> ProductSet { get; set; } 
    public virtual DbSet<Stock> StockSet { get; set; } 
    public virtual DbSet<Rating> RatingSet { get; set; } 
    public virtual DbSet<Discount> DiscountSet { get; set; } 
    public virtual DbSet<LineItem> LineItemSet { get; set; } 
    public virtual DbSet<ShoppingCart> ShoppingCartSet { get; set; } 
    public virtual DbSet<Invoice> InvoiceSet { get; set; } 
    public virtual DbSet<Customer> CustomerSet { get; set; } 
    public virtual DbSet<Adress> AdressSet { get; set; } 


    } 
    } 


} 

그리고 여기에는 IdentityDb 컨텍스트입니다. Data.Entity.Migrations;

namespace DAL.DataContexts.WhitelabelMigrations 
{ 
    public partial class InitialCreate : DbMigration 
    { 
    public override void Up() 
    { 
     CreateTable(
      "dbo.Adresses", 
      c => new 
      { 
       Id = c.Guid(false), 
       Street = c.String(), 
       ZipCode = c.Int(false), 
       City = c.String(), 
       Country = c.String(), 
       IsShippingAdress = c.Boolean(false), 
       Customer_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Customers", t => t.Customer_Id) 
      .Index(t => t.Customer_Id); 

     CreateTable(
      "dbo.Customers", 
      c => new 
      { 
       Id = c.Guid(false), 
       FirstName = c.String(), 
       LastName = c.String(), 
       Email = c.String(), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.Invoices", 
      c => new 
      { 
       Id = c.Guid(false), 
       DueDate = c.DateTime(false), 
       InvoiceDate = c.DateTime(false), 
       Customer_Id = c.Guid(), 
       ShoppingCart_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Customers", t => t.Customer_Id) 
      .ForeignKey("dbo.ShoppingCarts", t => t.ShoppingCart_Id) 
      .Index(t => t.Customer_Id) 
      .Index(t => t.ShoppingCart_Id); 

     CreateTable(
      "dbo.ShoppingCarts", 
      c => new 
      { 
       Id = c.Guid(false), 
       Quantity = c.String(), 
       Status = c.String(), 
       IsPaid = c.Boolean(false), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.LineItems", 
      c => new 
      { 
       Id = c.Guid(false), 
       Quantity = c.Int(false), 
       ProductId = c.Guid(), 
       ShoppingCart_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Products", t => t.ProductId) 
      .ForeignKey("dbo.ShoppingCarts", t => t.ShoppingCart_Id) 
      .Index(t => t.ProductId) 
      .Index(t => t.ShoppingCart_Id); 

     CreateTable(
      "dbo.Products", 
      c => new 
      { 
       Id = c.Guid(false), 
       Name = c.String(), 
       Description = c.String(), 
       Price = c.Decimal(false, 18, 2), 
       Status = c.String(), 
       RekeaseDate = c.DateTime(), 
       Weight = c.Double(), 
       Category_Id = c.Guid(), 
       Discount_Id = c.Guid(), 
       Stock_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Categories", t => t.Category_Id) 
      .ForeignKey("dbo.Discounts", t => t.Discount_Id) 
      .ForeignKey("dbo.Stocks", t => t.Stock_Id) 
      .Index(t => t.Category_Id) 
      .Index(t => t.Discount_Id) 
      .Index(t => t.Stock_Id); 

     CreateTable(
      "dbo.Categories", 
      c => new 
      { 
       Id = c.Guid(false), 
       Name = c.String(), 
       Parent = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.Attributes", 
      c => new 
      { 
       Id = c.Guid(false), 
       Name = c.String(), 
       Category_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Categories", t => t.Category_Id) 
      .Index(t => t.Category_Id); 

     CreateTable(
      "dbo.Discounts", 
      c => new 
      { 
       Id = c.Guid(false), 
       Price = c.Decimal(precision: 18, scale: 2), 
       StartDate = c.DateTime(), 
       EndDate = c.DateTime(), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.Ratings", 
      c => new 
      { 
       Id = c.Guid(false), 
       Rate = c.Int(), 
       Comment = c.String(), 
       Product_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Products", t => t.Product_Id) 
      .Index(t => t.Product_Id); 

     CreateTable(
      "dbo.Stocks", 
      c => new 
      { 
       Id = c.Guid(false), 
       Quantity = c.Int(), 
       DeliveryDate = c.DateTime(), 
      }) 
      .PrimaryKey(t => t.Id); 
    } 

    public override void Down() 
    { 
     DropForeignKey("dbo.Invoices", "ShoppingCart_Id", "dbo.ShoppingCarts"); 
     DropForeignKey("dbo.LineItems", "ShoppingCart_Id", "dbo.ShoppingCarts"); 
     DropForeignKey("dbo.LineItems", "ProductId", "dbo.Products"); 
     DropForeignKey("dbo.Products", "Stock_Id", "dbo.Stocks"); 
     DropForeignKey("dbo.Ratings", "Product_Id", "dbo.Products"); 
     DropForeignKey("dbo.Products", "Discount_Id", "dbo.Discounts"); 
     DropForeignKey("dbo.Products", "Category_Id", "dbo.Categories"); 
     DropForeignKey("dbo.Attributes", "Category_Id", "dbo.Categories"); 
     DropForeignKey("dbo.Invoices", "Customer_Id", "dbo.Customers"); 
     DropForeignKey("dbo.Adresses", "Customer_Id", "dbo.Customers"); 
     DropIndex("dbo.Invoices", new[] {"ShoppingCart_Id"}); 
     DropIndex("dbo.LineItems", new[] {"ShoppingCart_Id"}); 
     DropIndex("dbo.LineItems", new[] {"ProductId"}); 
     DropIndex("dbo.Products", new[] {"Stock_Id"}); 
     DropIndex("dbo.Ratings", new[] {"Product_Id"}); 
     DropIndex("dbo.Products", new[] {"Discount_Id"}); 
     DropIndex("dbo.Products", new[] {"Category_Id"}); 
     DropIndex("dbo.Attributes", new[] {"Category_Id"}); 
     DropIndex("dbo.Invoices", new[] {"Customer_Id"}); 
     DropIndex("dbo.Adresses", new[] {"Customer_Id"}); 
     DropTable("dbo.Stocks"); 
     DropTable("dbo.Ratings"); 
     DropTable("dbo.Discounts"); 
     DropTable("dbo.Attributes"); 
     DropTable("dbo.Categories"); 
     DropTable("dbo.Products"); 
     DropTable("dbo.LineItems"); 
     DropTable("dbo.ShoppingCarts"); 
     DropTable("dbo.Invoices"); 
     DropTable("dbo.Customers"); 
     DropTable("dbo.Adresses"); 
    } 
    } 
    } 

그리고 물론

은 Identity-마이그레이션 InitialCreate.cs 그래서

using System.Data.Entity.Migrations; 

    namespace DAL.DataContexts.IdentityMigrations 
    { 
    public partial class InitialCreate : DbMigration 
    { 
    public override void Up() 
    { 
     CreateTable(
      "dbo.AspNetRoles", 
      c => new 
      { 
       Id = c.String(false, 128), 
       Name = c.String(false), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.AspNetUsers", 
      c => new 
      { 
       Id = c.String(false, 128), 
       UserName = c.String(), 
       PasswordHash = c.String(), 
       SecurityStamp = c.String(), 
       Discriminator = c.String(false, 128), 
       Customer_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Customers", t => t.Customer_Id) 
      .Index(t => t.Customer_Id); 

     CreateTable(
      "dbo.AspNetUserClaims", 
      c => new 
      { 
       Id = c.Int(false, true), 
       ClaimType = c.String(), 
       ClaimValue = c.String(), 
       User_Id = c.String(false, 128), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.AspNetUsers", t => t.User_Id, true) 
      .Index(t => t.User_Id); 

     CreateTable(
      "dbo.AspNetUserLogins", 
      c => new 
      { 
       UserId = c.String(false, 128), 
       LoginProvider = c.String(false, 128), 
       ProviderKey = c.String(false, 128), 
      }) 
      .PrimaryKey(t => new {t.UserId, t.LoginProvider, t.ProviderKey}) 
      .ForeignKey("dbo.AspNetUsers", t => t.UserId, true) 
      .Index(t => t.UserId); 

     CreateTable(
      "dbo.AspNetUserRoles", 
      c => new 
      { 
       UserId = c.String(false, 128), 
       RoleId = c.String(false, 128), 
      }) 
      .PrimaryKey(t => new {t.UserId, t.RoleId}) 
      .ForeignKey("dbo.AspNetRoles", t => t.RoleId, true) 
      .ForeignKey("dbo.AspNetUsers", t => t.UserId, true) 
      .Index(t => t.RoleId) 
      .Index(t => t.UserId); 

     CreateTable(
      "dbo.Customers", 
      c => new 
      { 
       Id = c.Guid(false), 
       FirstName = c.String(), 
       LastName = c.String(), 
       Email = c.String(), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.Adresses", 
      c => new 
      { 
       Id = c.Guid(false), 
       Street = c.String(), 
       ZipCode = c.Int(false), 
       City = c.String(), 
       Country = c.String(), 
       IsShippingAdress = c.Boolean(false), 
       Customer_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Customers", t => t.Customer_Id) 
      .Index(t => t.Customer_Id); 

     CreateTable(
      "dbo.Invoices", 
      c => new 
      { 
       Id = c.Guid(false), 
       DueDate = c.DateTime(false), 
       InvoiceDate = c.DateTime(false), 
       Customer_Id = c.Guid(), 
       ShoppingCart_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Customers", t => t.Customer_Id) 
      .ForeignKey("dbo.ShoppingCarts", t => t.ShoppingCart_Id) 
      .Index(t => t.Customer_Id) 
      .Index(t => t.ShoppingCart_Id); 

     CreateTable(
      "dbo.ShoppingCarts", 
      c => new 
      { 
       Id = c.Guid(false), 
       Quantity = c.String(), 
       Status = c.String(), 
       IsPaid = c.Boolean(false), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.LineItems", 
      c => new 
      { 
       Id = c.Guid(false), 
       Quantity = c.Int(false), 
       ProductId = c.Guid(), 
       ShoppingCart_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Products", t => t.ProductId) 
      .ForeignKey("dbo.ShoppingCarts", t => t.ShoppingCart_Id) 
      .Index(t => t.ProductId) 
      .Index(t => t.ShoppingCart_Id); 

     CreateTable(
      "dbo.Products", 
      c => new 
      { 
       Id = c.Guid(false), 
       Name = c.String(), 
       Description = c.String(), 
       Price = c.Decimal(false, 18, 2), 
       Status = c.String(), 
       RekeaseDate = c.DateTime(), 
       Weight = c.Double(), 
       Category_Id = c.Guid(), 
       Discount_Id = c.Guid(), 
       Stock_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Categories", t => t.Category_Id) 
      .ForeignKey("dbo.Discounts", t => t.Discount_Id) 
      .ForeignKey("dbo.Stocks", t => t.Stock_Id) 
      .Index(t => t.Category_Id) 
      .Index(t => t.Discount_Id) 
      .Index(t => t.Stock_Id); 

     CreateTable(
      "dbo.Categories", 
      c => new 
      { 
       Id = c.Guid(false), 
       Name = c.String(), 
       Parent = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.Attributes", 
      c => new 
      { 
       Id = c.Guid(false), 
       Name = c.String(), 
       Category_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Categories", t => t.Category_Id) 
      .Index(t => t.Category_Id); 

     CreateTable(
      "dbo.Discounts", 
      c => new 
      { 
       Id = c.Guid(false), 
       Price = c.Decimal(precision: 18, scale: 2), 
       StartDate = c.DateTime(), 
       EndDate = c.DateTime(), 
      }) 
      .PrimaryKey(t => t.Id); 

     CreateTable(
      "dbo.Ratings", 
      c => new 
      { 
       Id = c.Guid(false), 
       Rate = c.Int(), 
       Comment = c.String(), 
       Product_Id = c.Guid(), 
      }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.Products", t => t.Product_Id) 
      .Index(t => t.Product_Id); 

     CreateTable(
      "dbo.Stocks", 
      c => new 
      { 
       Id = c.Guid(false), 
       Quantity = c.Int(), 
       DeliveryDate = c.DateTime(), 
      }) 
      .PrimaryKey(t => t.Id); 
    } 

    public override void Down() 
    { 
     DropForeignKey("dbo.AspNetUsers", "Customer_Id", "dbo.Customers"); 
     DropForeignKey("dbo.Invoices", "ShoppingCart_Id", "dbo.ShoppingCarts"); 
     DropForeignKey("dbo.LineItems", "ShoppingCart_Id", "dbo.ShoppingCarts"); 
     DropForeignKey("dbo.LineItems", "ProductId", "dbo.Products"); 
     DropForeignKey("dbo.Products", "Stock_Id", "dbo.Stocks"); 
     DropForeignKey("dbo.Ratings", "Product_Id", "dbo.Products"); 
     DropForeignKey("dbo.Products", "Discount_Id", "dbo.Discounts"); 
     DropForeignKey("dbo.Products", "Category_Id", "dbo.Categories"); 
     DropForeignKey("dbo.Attributes", "Category_Id", "dbo.Categories"); 
     DropForeignKey("dbo.Invoices", "Customer_Id", "dbo.Customers"); 
     DropForeignKey("dbo.Adresses", "Customer_Id", "dbo.Customers"); 
     DropForeignKey("dbo.AspNetUserClaims", "User_Id", "dbo.AspNetUsers"); 
     DropForeignKey("dbo.AspNetUserRoles", "UserId", "dbo.AspNetUsers"); 
     DropForeignKey("dbo.AspNetUserRoles", "RoleId", "dbo.AspNetRoles"); 
     DropForeignKey("dbo.AspNetUserLogins", "UserId", "dbo.AspNetUsers"); 
     DropIndex("dbo.AspNetUsers", new[] {"Customer_Id"}); 
     DropIndex("dbo.Invoices", new[] {"ShoppingCart_Id"}); 
     DropIndex("dbo.LineItems", new[] {"ShoppingCart_Id"}); 
     DropIndex("dbo.LineItems", new[] {"ProductId"}); 
     DropIndex("dbo.Products", new[] {"Stock_Id"}); 
     DropIndex("dbo.Ratings", new[] {"Product_Id"}); 
     DropIndex("dbo.Products", new[] {"Discount_Id"}); 
     DropIndex("dbo.Products", new[] {"Category_Id"}); 
     DropIndex("dbo.Attributes", new[] {"Category_Id"}); 
     DropIndex("dbo.Invoices", new[] {"Customer_Id"}); 
     DropIndex("dbo.Adresses", new[] {"Customer_Id"}); 
     DropIndex("dbo.AspNetUserClaims", new[] {"User_Id"}); 
     DropIndex("dbo.AspNetUserRoles", new[] {"UserId"}); 
     DropIndex("dbo.AspNetUserRoles", new[] {"RoleId"}); 
     DropIndex("dbo.AspNetUserLogins", new[] {"UserId"}); 
     DropTable("dbo.Stocks"); 
     DropTable("dbo.Ratings"); 
     DropTable("dbo.Discounts"); 
     DropTable("dbo.Attributes"); 
     DropTable("dbo.Categories"); 
     DropTable("dbo.Products"); 
     DropTable("dbo.LineItems"); 
     DropTable("dbo.ShoppingCarts"); 
     DropTable("dbo.Invoices"); 
     DropTable("dbo.Adresses"); 
     DropTable("dbo.Customers"); 
     DropTable("dbo.AspNetUserRoles"); 
     DropTable("dbo.AspNetUserLogins"); 
     DropTable("dbo.AspNetUserClaims"); 
     DropTable("dbo.AspNetUsers"); 
     DropTable("dbo.AspNetRoles"); 
    } 
    } 
} 

, 나는에만 허용 목록 라벨 configuration.cs 파일에서 Category 클래스에 액세스 할 수 있기 때문에 실행 씨앗 방법을 얻을 수있는 방법 ?

설명, 생각 및 건설적인 도움을 주시면 감사하겠습니다.

public virtual DbSet<Category> CategorySet { get; set; } 

의 I는 IdentityMigrations의 configuration.cs의 범주 클래스에 액세스 할 수 있습니다 그리고 그 길을 나는 작업 내 종자 방법을 만듭니다

답변

0

그런데 실제로 내가했던 일은,이 IdentityDb이 줄을 추가했다 그것을 원했다.

protected override void Seed(IdentityDb context) 
{ 
    **//This is the seed method I want to run when I update the database:** 
    if(!context.CategorySet.Any(c => c.Name == "Root")) 
    { 
     Category mainCategory = new Category { Id = Guid.NewGuid(), Name = "Root", Parent = null }; 
     context.CategorySet.Add(mainCategory); 
     context.SaveChanges(); 
    } 

}