简体   繁体   中英

C# calling base class constructor confusion

In my asp.net MVC app I am using entity framework and Identity for user authentication. So my DbContext class looks like the following (it works):

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

I am passing a string to the base constructor of PropertyContext . Therefore, I can assume that IdentityDbContext has a constructor that takes a string as argument.

However in the github repository of asp.net identity ( here IdentityDbContext.cs ) I found the following-

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

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

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

The IdentityDBContext class that your are referring to inherits from another class IdentityDbContext<TUser, TRole, TKey> which then inherits from DbContext. Look at line 29 of the above reference file IdentityDbContext.cs

IdentityDbContext<TUser> inherits from IdentityDbContext<TUser, IdentityRole, string> , which in turn inherits from DbContext . DbContext contains the constructor which takes a single string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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