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

查看:26
本文介绍了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.

我该怎么处理它?<​​/p>

What should I do with it?

推荐答案

根据文档,直接继承DbContext类也没关系:

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.

换句话说,由于 根据定义,is 什么都不做,调用 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天全站免登陆