gpt4 book ai didi

entity-framework - base.OnModelCreating(modelBuilder) 是必要的吗?

转载 作者:行者123 更新时间:2023-12-04 02:04:41 26 4
gpt4 key购买 nike

我正在覆盖 OnModelCreating , 在方法中有 base.OnModelCreating(modelBuilder);

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}

查看有关如何实现此功能的代码示例和手册,我对是否需要它感到困惑。有时它在那里,有时不在那里。有时在方法的开头,有时在结尾。

我该怎么办?

最佳答案

根据documentation ,如果您直接从 DbContext 继承也没关系类(class):

This method is called when the model for a derived context has been initialized, but before the model has been locked down and used to initialize the context. The default implementation of this method does nothing, but it can be overridden in a derived class such that the model can be further configured before it is locked down.



换句话说,因为 is 什么都不做 根据定义 , 调用 base.OnModelCreating没有必要,但同时如果被调用也不会受到伤害 - 这就是为什么有时它在那里,有时不存在。有时在方法的开头,有时在结尾。

关于entity-framework - base.OnModelCreating(modelBuilder) 是必要的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39576176/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com