我在具有代码优先模式的Web API项目中使用EF。
到目前为止,这么好,但我在表中有Time字段,我想要Precision 0。
所以我在OnModelCreating
类的DBContext
方法中添加了所需的代码。
之后,没有执行Configuration类中的Seed方法。
当我在OnModelCreating
中评论代码时,Seed方法正在执行。
如果有人能给我一个理由,我会很高兴,如何重组我的课程以运行这两种方法。
通常我使用标准方式。我有最简单的DbContext:
public partial class HarmoniaContext : DbContext {
public HarmoniaContext()
: base("Harmonia") {
}
...virtual properties or the DbSet<>...
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Nyitvatartas>()
.Property(e => e.KezdIdo)
.HasPrecision(0);
modelBuilder.Entity<Nyitvatartas>()
.Property(e => e.BefIdo)
.HasPrecision(0);
}
}
和配置文件
internal sealed class Configuration :DbMigrationsConfiguration<HarmoniaContext> {
public Configuration() {
AutomaticMigrationsEnabled = true;
}
protected override void Seed(HarmoniaContext context) {
context.Beallitas.AddOrUpdate(b => b.Nev, ...}
context.Szerepkor.AddOrUpdate(sz=> sz.RendszerNev, ...}
}
}