base.OnModelCreating(modelBuilder)是否必要? [英] Is base.OnModelCreating(modelBuilder) necessary?

查看:66
本文介绍了base.OnModelCreating(modelBuilder)是否必要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要覆盖 OnModelCreating ,并且该方法中有一行 base.OnModelCreating(modelBuilder);

I'm overriding OnModelCreating, and in the method there's the line base.OnModelCreating(modelBuilder);

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

查看有关如何实现此功能的代码示例和手册,无论是否需要它,我都很困惑.有时它在那里,有时没有.有时在方法开始时,有时在方法结束时.

Looking at code samples and manuals on how to implement this, I get confused as to wether I need it or not. Sometimes it's there, sometimes not. Sometimes at the beginning of the method, other times at the end.

我该怎么办?

推荐答案

根据

According to the documentation, it doesn't matter if you inherit directly from the 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.

换句话说,由于定义上没有任何作用,因此不必调用 base.OnModelCreating ,但是同时调用它也不会受到伤害-这就是为什么有时它在那里,有时不在那里.有时在方法开始时,有时在方法结束时.

In other words, since is does nothing by definition, calling base.OnModelCreating is not necessary, but at the same time will not hurt if called - that's why Sometimes it's there, sometimes not. Sometimes at the beginning of the method, other times at the end.

这篇关于base.OnModelCreating(modelBuilder)是否必要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆