C# Entity Framework Migration
I'm working on a Web Cash Register in C# (MVC 5 ASP.net EF6), Code First technology, and I'm trying to make a database with migrations. In my database context file I select the tables I want to add in the database, but when I build the migration, it just uses all the excisting Models I have made for the database, instead of the ones I've selected in the DB Context file. Why does this happen? Why Immediately everything, and not just the ones I selected? Here's my code:
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>();
}
}
}
Here is the migration, generated with Entity Framework. It just uses all the models I have instead of the ones in the DBContext shown above.
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");
}
}
}
Thanks for your help! Marco
Your Delivery
class has navigation properties to Store
and Status
so Entity Framework pulls those classes into the Model too, then the Store
class has a Retailer
navigation property. These tables are all required to support the model that you have defined in code so Entity Framework is generating the correct migration code.
上一篇: MySql数据库中的实体框架代码优先迁移
下一篇: C#实体框架迁移