我在学英孚
我想先用代码来处理不同类型的继承。
我最初从几个类开始运行我的应用程序。我看到创建了数据库,所有的类都表示为表。
但是,当我添加新的类或字段并再次运行应用程序时,在数据库架构中看不到更改。
我使用了“dropcreatedatabasealways”,所以我不明白为什么在添加字段和类时没有用正确的模式更新我的数据库。有人能解释一下我做错了什么吗?
初始代码:
namespace Domain
{
public class Good
{
public int Id { get; set; }
public string Name { get; set; }
public double BaseValue { get; set; }
}
public class Manufacturer {
public int Id {get; set;}
public ICollection<ManufacturingCapability> ManufacturingCapabilities { get; set; }
}
public class ManufacturingCapability {
public int Id { get; set; }
public ICollection<ManufacturingInput> Inputs { get; set; }
public ICollection<ManufacturingOutput> Outputs { get; set; }
public TimeSpan CycleTime { get; set; }
}
public class ManufacturingInput {
public int Id { get; set; }
public Good Good { get; set; }
public uint Quantity { get; set; }
}
public class ManufacturingOutput {
public int Id { get; set; }
public Good Good { get; set; }
public uint Quantity { get; set; }
}
public class ManufacturingDbContext :DbContext {
public DbSet<Good> Goods { get; set; }
public DbSet<Manufacturer> Manufacturers { get; set; }
public ManufacturingDbContext() : base("name=EFLearnConnectionString") {
Database.SetInitializer<ManufacturingDbContext>(new DropCreateDatabaseAlways<ManufacturingDbContext>());
}
}
}
namespace EFLearning {
class Program {
static void Main(string[] args) {
using (var manufacturingDbContext = new ManufacturingDbContext()) {
var good = new Good() { Name = "Water" };
manufacturingDbContext.Goods.Add(good);
manufacturingDbContext.SaveChanges();
}
}
}
}
运行后Management Studio中的数据库表:
新增代码:
public class Station : Manufacturer {
public string Name { get; set; }
public Vector3 Location { get; set; }
}
我在上下文中添加了这个:
public DbSet<Station> Stations { get; set; }
运行后Management Studio中的数据库表: