该代码会从继承的EF构造函数中运行吗?

发布于 2025-01-17 09:12:57 字数 1124 浏览 0 评论 0原文

假设我们有两个从 EF DbContext 继承的 DbContext:

using System.Data.Entity;
using System.Collections.Generic;

namespace myproject.Models
{
    public class MyContext : DbContext
    {
        public MyContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {
            Database.CommandTimeout = Config.DatabaseCommandTimeout;
            Database.SetInitializer<MyContext>(null);
        }

        public MyContext() : this("name=MyDbContextConnectionString") { }
    }
    
    public class MyContext2ReadOnly : MyContext
    { 
        public MyContext2ReadOnly() : base("MyDbContextConnectionStringReadOnly")
        {
        }
    }
}

现在假设我们创建一个 MyContext2ReadOnly() 实例。 我的问题是 - 以下行会 Database.SetInitializer(null);父构造函数中的 是有效的,还是我必须在 MyContext2ReadOnly() 构造函数中再次调用它(?):

 public class MyContext2ReadOnly : MyContext
{ 
    public MyContext2ReadOnly() : base("MyDbContextConnectionStringReadOnly")
    {
       Database.SetInitializer<MyContext2ReadOnly>(null);
    }
}

Imagine we have two DbContexts that inherit from EF DbContext as such:

using System.Data.Entity;
using System.Collections.Generic;

namespace myproject.Models
{
    public class MyContext : DbContext
    {
        public MyContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {
            Database.CommandTimeout = Config.DatabaseCommandTimeout;
            Database.SetInitializer<MyContext>(null);
        }

        public MyContext() : this("name=MyDbContextConnectionString") { }
    }
    
    public class MyContext2ReadOnly : MyContext
    { 
        public MyContext2ReadOnly() : base("MyDbContextConnectionStringReadOnly")
        {
        }
    }
}

Now imagine we create an instance of MyContext2ReadOnly().
My question is - will the following line Database.SetInitializer<MyContext>(null); from the parent constructor be valid, or do I have to call it again in the MyContext2ReadOnly() constructor as such(?):

 public class MyContext2ReadOnly : MyContext
{ 
    public MyContext2ReadOnly() : base("MyDbContextConnectionStringReadOnly")
    {
       Database.SetInitializer<MyContext2ReadOnly>(null);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无语# 2025-01-24 09:12:57

我认为 DbContext 在处理继承和构造函数时并不特殊。所以就做个测试吧。我将定义一个 Db 来替换 DbContext

    public class Db
    {
        public Db(string s)
        {
            Console.WriteLine($"Db {s}");
        }
    }

    public class MyContext : Db
    {
        public MyContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {
            Console.WriteLine($"MyContext {nameOrConnectionString}");
        }

        public MyContext() : this("name=MyDbContextConnectionString") { }
    }

    public class MyContext2ReadOnly : MyContext
    {
        public MyContext2ReadOnly() : base("MyDbContextConnectionStringReadOnly")
        {
            Console.WriteLine($"MyContext2ReadOnly");
        }
    }
//in main
MyContext2ReadOnly myContext2ReadOnly = new MyContext2ReadOnly();
//output
Db MyDbContextConnectionStringReadOnly
MyContext MyDbContextConnectionStringReadOnly
MyContext2ReadOnly

它证明了 MyContext 构造函数中的所有内容都被调用了。

I think DbContext is not special when handling inheritance and constructor. So just have a test. I will define a Db to replace DbContext.

    public class Db
    {
        public Db(string s)
        {
            Console.WriteLine(
quot;Db {s}");
        }
    }

    public class MyContext : Db
    {
        public MyContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {
            Console.WriteLine(
quot;MyContext {nameOrConnectionString}");
        }

        public MyContext() : this("name=MyDbContextConnectionString") { }
    }

    public class MyContext2ReadOnly : MyContext
    {
        public MyContext2ReadOnly() : base("MyDbContextConnectionStringReadOnly")
        {
            Console.WriteLine(
quot;MyContext2ReadOnly");
        }
    }
//in main
MyContext2ReadOnly myContext2ReadOnly = new MyContext2ReadOnly();
//output
Db MyDbContextConnectionStringReadOnly
MyContext MyDbContextConnectionStringReadOnly
MyContext2ReadOnly

It proves everything in MyContext constructor are called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文