我正在学习EF 我想先使用代码来处理不同类型的继承。
我最初从几节课开始,然后运行了我的应用程序。我看到数据库是用我所有的类表示为表创建的。
但是,当我添加新的类或字段并再次运行该应用程序时,我看不到数据库架构中的更改。
我使用了“ 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中