简体   繁体   English

C#调用基类构造函数混淆

[英]C# calling base class constructor confusion

In my asp.net MVC app I am using entity framework and Identity for user authentication. 在我的asp.net MVC应用程序中,我使用实体框架和Identity进行用户身份验证。 So my DbContext class looks like the following (it works): 所以我的DbContext类看起来如下(它的工作原理):

public class PropertyContext : IdentityDbContext<AppUser>
    {
        public PropertyContext() : base("name=PropertyBDConnection") { }
         ...
    }

I am passing a string to the base constructor of PropertyContext . 我将一个字符串传递给PropertyContext的基础构造函数。 Therefore, I can assume that IdentityDbContext has a constructor that takes a string as argument. 因此,我可以假设IdentityDbContext有一个构造函数,它接受一个字符串作为参数。

However in the github repository of asp.net identity ( here IdentityDbContext.cs ) I found the following- 但是在asp.net身份的github存储库( 这里是IdentityDbContext.cs )中,我发现了以下内容 -

public class IdentityDbContext<TUser> : 
      IdentityDbContext<TUser, IdentityRole, string> 
      where TUser : IdentityUser
{ }

No constructor at all. 根本没有构造函数。 Certainly I am missing something or looking in wrong place. 当然,我错过了一些东西或者看错了地方。

是的,你看错了地方:根据文档, IdentityDbContext 有三个构造函数 ,其中一个构造函数带有一个string

  • IdentityDbContext()
  • IdentityDbContext(DbConnection, DbCompiledModel, Boolean)
  • IdentityDbContext(String)

I don't have actually access to the source code, but i want to point out a tricky feature of C# that are Implicit operators: https://msdn.microsoft.com/en-us/library/z5z9kes2.aspx 我实际上没有访问源代码,但我想指出C#的一个棘手的功能是隐式运算符: https//msdn.microsoft.com/en-us/library/z5z9kes2.aspx

since IdentityDBContext, in your linked source, has a constructor that take a DBContextOptions, that class could use an implicit converter to convert from string to an instance of DBContextOptions 由于IdentityDBContext在您的链接源中有一个带有DBContextOptions的构造函数,因此该类可以使用隐式转换器将字符串转换为DBContextOptions的实例

here a snippet done for you to explain how this work, that simulate a possible way to achieve what you see, this doesn't mean that actual this is the case, probably you are just pointing to a wrong codebase, but this is a possibility 这里有一个片段,为你解释这是如何工作,模拟一种可能的方式来实现你所看到的,这并不意味着实际情况就是这样,可能你只是指向一个错误的代码库,但这是一种可能性

using System;
public class Program {
    public class IdentityDbContext {
        public DbContextOptions Options { get; set; }
        public IdentityDbContext(DbContextOptions options){
            this.Options = options;
        }
    }

    public class DbContextOptions {
        public string Config { get; set; }

        public DbContextOptions(string config){
            this.Config = config;
        }

        public static implicit operator DbContextOptions(string config) {
            return new DbContextOptions(config);
        }
    }

    public static void Main()
    {
        IdentityDbContext f = new IdentityDbContext(new DbContextOptions("test")); //it's ok
        Console.WriteLine(f.Options.Config);

        IdentityDbContext f2 = new IdentityDbContext("testWithImplicit");
        Console.WriteLine(f2.Options.Config);
    }
}

update: added a fiddle link: https://dotnetfiddle.net/aykOqq 更新:添加了一个小提琴链接: https//dotnetfiddle.net/aykOqq

The IdentityDBContext class that your are referring to inherits from another class IdentityDbContext<TUser, TRole, TKey> which then inherits from DbContext. 您引用的IdentityDBContext类继承自另一个类IdentityDbContext<TUser, TRole, TKey> ,然后继承自DbContext。 Look at line 29 of the above reference file IdentityDbContext.cs 查看上面参考文件IdentityDbContext.cs的第29行

IdentityDbContext<TUser> inherits from IdentityDbContext<TUser, IdentityRole, string> , which in turn inherits from DbContext . IdentityDbContext<TUser>继承自IdentityDbContext<TUser, IdentityRole, string> ,后者继承自DbContext DbContext contains the constructor which takes a single string. DbContext包含带有单个字符串的构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM