我正在使用C#(MVC 5 ASP.net EF6),Code First技术进行Web Cash Register,我正在尝试使用迁移创建数据库。在我的数据库上下文文件中,我选择要在数据库中添加的表,但是当我构建迁移时,它只使用我为数据库创建的所有模型,而不是我在数据库上下文中选择的模型。文件。为什么会这样?为什么要立即采取一切,而不仅仅是我选择的那些?这是我的代码:
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 :(得分:0)
您的Delivery
类具有Store
和Status
的导航属性,因此实体框架也将这些类拉入模型,然后Store
类具有Retailer
导航属性。这些表都是支持您在代码中定义的模型所必需的,因此Entity Framework正在生成正确的迁移代码。