2014-12-18 2 views
0

저는 C# (MVC 5 ASP.net EF6)의 코드 첫 번째 기술인 웹 캐쉬 레지스터에서 작업 중이며 마이그레이션 데이터베이스를 만들려고합니다. 데이터베이스 컨텍스트 파일에서 데이터베이스에 추가 할 테이블을 선택하지만 마이그레이션을 빌드 할 때 DB 컨텍스트에서 선택한 모델 대신 데이터베이스에 대해 만든 모든 excisting 모델을 사용합니다 파일. 왜 이런 일이 생길까요? 왜 내가 선택한 것뿐만 아니라 모든 것을 즉시? 여기 내 코드는 다음과 같습니다.C# 엔티티 프레임 워크 마이그레이션

using WebCashRegister.Models.BLModels; 
using System.Data.Entity; 
using System.Data.Entity.ModelConfiguration.Conventions; 

namespace WebCashRegister.DAL 
{ 
    public class WebCashRegisterContext : DbContext 
    { 
     public WebCashRegisterContext() : base("WebCashRegister") 
     { 

     } 

     public DbSet<WebCashRegister.Models.BLModels.Price> Price { get; set; } 
     public DbSet<WebCashRegister.Models.BLModels.Product> Product { get; set; } 
     public DbSet<WebCashRegister.Models.BLModels.Category> Category { get; set; } 
     public DbSet<WebCashRegister.Models.BLModels.DeliveryLine> DeliveryLine { get; set; } 
     public DbSet<WebCashRegister.Models.BLModels.Delivery> Delivery { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 
     } 
    } 
} 

다음은 Entity Framework에서 생성 된 마이그레이션입니다. 위에 표시된 DBContext에있는 모델 대신 모든 모델을 사용합니다.

namespace WebCashRegister.Migrations 
{ 
    using System; 
    using System.Data.Entity.Migrations; 

    public partial class eerste : DbMigration 
    { 
     public override void Up() 
     { 
      CreateTable(
       "dbo.Category", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         FoodNonfoodCategory = c.Int(nullable: false), 
        }) 
       .PrimaryKey(t => t.ID); 

      CreateTable(
       "dbo.Product", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         Name = c.String(), 
         Price_ID = c.Int(), 
         Category_ID = c.Int(), 
        }) 
       .PrimaryKey(t => t.ID) 
       .ForeignKey("dbo.Price", t => t.Price_ID) 
       .ForeignKey("dbo.Category", t => t.Category_ID) 
       .Index(t => t.Price_ID) 
       .Index(t => t.Category_ID); 

      CreateTable(
       "dbo.Price", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         ProductPrice = c.Int(nullable: false), 
         Sale = c.String(), 
        }) 
       .PrimaryKey(t => t.ID); 

      CreateTable(
       "dbo.Delivery", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         DateTimeSent = c.DateTime(nullable: false), 
         DateTimeEstimatedDelivery = c.DateTime(nullable: false), 
         DateTimeDelivered = c.DateTime(nullable: false), 
         Status_ID = c.Int(), 
         Store_ID = c.Int(), 
        }) 
       .PrimaryKey(t => t.ID) 
       .ForeignKey("dbo.Status", t => t.Status_ID) 
       .ForeignKey("dbo.Store", t => t.Store_ID) 
       .Index(t => t.Status_ID) 
       .Index(t => t.Store_ID); 

      CreateTable(
       "dbo.DeliveryLine", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         Delivery_ID = c.Int(), 
         Product_ID = c.Int(), 
        }) 
       .PrimaryKey(t => t.ID) 
       .ForeignKey("dbo.Delivery", t => t.Delivery_ID) 
       .ForeignKey("dbo.Product", t => t.Product_ID) 
       .Index(t => t.Delivery_ID) 
       .Index(t => t.Product_ID); 

      CreateTable(
       "dbo.Status", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         Delivered = c.Boolean(nullable: false), 
        }) 
       .PrimaryKey(t => t.ID); 

      CreateTable(
       "dbo.Store", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         StreetName = c.String(), 
         AdressNumber = c.Int(nullable: false), 
         ZIP = c.String(), 
         State = c.String(), 
         Country = c.String(), 
         Retailer_ID = c.Int(), 
        }) 
       .PrimaryKey(t => t.ID) 
       .ForeignKey("dbo.Retailer", t => t.Retailer_ID) 
       .Index(t => t.Retailer_ID); 

      CreateTable(
       "dbo.Retailer", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         Name = c.String(), 
         StreetName = c.String(), 
         AdressNumber = c.Int(nullable: false), 
         ZIP = c.String(), 
         State = c.String(), 
         Country = c.String(), 
         CEOName = c.String(), 
         Stores = c.Int(nullable: false), 
        }) 
       .PrimaryKey(t => t.ID); 

     } 

     public override void Down() 
     { 
      DropForeignKey("dbo.Store", "Retailer_ID", "dbo.Retailer"); 
      DropForeignKey("dbo.Delivery", "Store_ID", "dbo.Store"); 
      DropForeignKey("dbo.Delivery", "Status_ID", "dbo.Status"); 
      DropForeignKey("dbo.DeliveryLine", "Product_ID", "dbo.Product"); 
      DropForeignKey("dbo.DeliveryLine", "Delivery_ID", "dbo.Delivery"); 
      DropForeignKey("dbo.Product", "Category_ID", "dbo.Category"); 
      DropForeignKey("dbo.Product", "Price_ID", "dbo.Price"); 
      DropIndex("dbo.Store", new[] { "Retailer_ID" }); 
      DropIndex("dbo.DeliveryLine", new[] { "Product_ID" }); 
      DropIndex("dbo.DeliveryLine", new[] { "Delivery_ID" }); 
      DropIndex("dbo.Delivery", new[] { "Store_ID" }); 
      DropIndex("dbo.Delivery", new[] { "Status_ID" }); 
      DropIndex("dbo.Product", new[] { "Category_ID" }); 
      DropIndex("dbo.Product", new[] { "Price_ID" }); 
      DropTable("dbo.Retailer"); 
      DropTable("dbo.Store"); 
      DropTable("dbo.Status"); 
      DropTable("dbo.DeliveryLine"); 
      DropTable("dbo.Delivery"); 
      DropTable("dbo.Price"); 
      DropTable("dbo.Product"); 
      DropTable("dbo.Category"); 
     } 
    } 
} 

도움 주셔서 감사합니다. 마르코

답변

0

귀하의 Delivery 클래스가 너무 엔티티 프레임 워크가 너무 모델에 해당 클래스를 가져옵니다 StoreStatus에 탐색 속성이 다음 Store 클래스는 Retailer 탐색 속성이 있습니다. 이러한 테이블은 모두 Entity Framework에서 올바른 마이그레이션 코드를 생성 할 수 있도록 코드에서 정의한 모델을 지원해야합니다.

+0

답변 해 주셔서 감사합니다. 지금은 많은 의미가 있습니다. 문맥에 표를 포함시키지 않았으므로 처음에는 혼란 스러웠습니다.하지만 지금은 이해가됩니다. 나는 그것을 읽는 것이 더 분명하기 때문에 컨텍스트에서 추가해야 할 필요가 있습니까? – Markinson

+0

@Helix 그건 너에게 달렸어. 네비게이션 속성을 통해서만 데이터에 액세스하려는 경우 깔끔함을 위해 컨텍스트에 추가 할 이유가 없습니다. 그러나 Store's와'Status'es와'Retailer's에서 CRUD를 쉽게 할 수 있도록'DbSet's가 필요하지 않습니까? – Colin

관련 문제