簡體   English   中英

C#實體框架遷移

[英]C# Entity Framework Migration

我正在使用C#(MVC 5 ASP.net EF6)和Code First技術開發一個Web收銀機,並且正在嘗試通過遷移創建數據庫。 在我的數據庫上下文文件中,選擇要添加到數據庫中的表,但是在構建遷移時,它僅使用為數據庫創建的所有現有模型,而不是在數據庫上下文中選擇的模型。文件。 為什么會這樣? 為什么要立即進行所有操作,而不僅僅是我選擇的? 這是我的代碼:

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>();
        }
    }
} 

這是使用實體框架生成的遷移。 它僅使用我擁有的所有模型,而不是上面顯示的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");
        }
    }
}

謝謝你的幫助! 馬可

您的Delivery類具有“ Store和“ Status導航屬性,因此Entity Framework也將這些類拉入模型,然后Store類具有“ Retailer導航屬性。 這些表都是支持您在代碼中定義的模型所必需的,以便Entity Framework生成正確的遷移代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM