C#实体框架迁移
我正在使用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>();
}
}
}
这是由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");
}
}
}
谢谢你的帮助! 马尔科
您的Delivery
类具有导航属性以Store
和Status
因此实体框架也将这些类拉入模型中,然后Store
类具有Retailer
导航属性。 这些表格都需要支持您在代码中定义的模型,以便Entity Framework生成正确的迁移代码。