Ismail Dogan
Ismail Dogan

Reputation: 335

Asp.Net Core 3.1 LinqToDB.Identity UserManager CreateAsync Error

I have implemented LinqToDB.Identity into my project. I have been trying to create a user by using .Net Identity UserManager, but I am getting an error. I have also implemented LinqToDB.Identity optimizations, such as AddLinqToDBStores and IdentityConnectionFactory.

As I have mentioned about it, I am getting an error like this when I try to create an user.

{"Method not found: 'Int32 LinqToDB.DataExtensions.Insert(LinqToDB.IDataContext, System.__Canon, System.String, System.String, System.String)'."}

enter image description here

Here is my AddLinqToDBStores options and configurations.

 public static void AddDevPlatformAuthentication(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddIdentity<AppUser, LinqToDB.Identity.IdentityRole<int>>(options =>
        {
            options.Password.RequireDigit = true;
            options.Password.RequiredLength = 4;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = true;
            options.Password.RequireLowercase = false;

            options.User.RequireUniqueEmail = true;

            //TODO
            //options.User.RequireUniqueEmail = true; 
            //options.SignIn.RequireConfirmedEmail = true;
            //options.Lockout.MaxFailedAccessAttempts = 5;
            //options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(3);

        }).AddLinqToDBStores<int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim>(new
        IdentityConnectionFactory(new SqlServerDataProvider(ProviderName.SqlServer, SqlServerVersion.v2017), "SqlServerIdentity", DataSettingsManager.LoadSettings().ConnectionString))
            .AddDefaultTokenProviders();

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();

        // Uncomment the following lines to enable logging in with third party login providers

        JwtTokenDefinitions.LoadFromConfiguration(configuration);
        services.ConfigureJwtAuthentication();
        services.ConfigureJwtAuthorization();
    }

Here is my IdentityConnectionFactory class that has inherited from IConnectionFactory interface.

public class IdentityConnectionFactory : IConnectionFactory
{
    private static readonly Dictionary<string, HashSet<string>> _tables = new Dictionary<string, HashSet<string>>();
    private readonly string _configuration;
    private readonly string _connectionString;
    private readonly string _key;
    private readonly IDataProvider _provider;

    public IdentityConnectionFactory(IDataProvider provider, string configuration, string connectionString)
    {
        _provider = provider;
        Configuration.Linq.AllowMultipleQuery = true;
        //DataConnection.AddConfiguration(configuration, connectionString, provider);
        _configuration = configuration;
        _connectionString = connectionString;
        _key = _configuration + "$$" + _connectionString;
    }
    public IDataContext GetContext()
    {
        return new DataContext(_provider, _connectionString);
    }

    public DataConnection GetConnection()
    {
        var db = new DataConnection(_provider, _connectionString);
        db.AddMappingSchema(AdditionalSchema);

        return db;
    }

    protected MappingSchema AdditionalSchema
    {
        get
        {
            if (!(Singleton<MappingSchema>.Instance is null))
                return Singleton<MappingSchema>.Instance;

            Singleton<MappingSchema>.Instance =
                new MappingSchema(_provider.Name) { MetadataReader = new FluentMigratorMetadataReader() };

            return Singleton<MappingSchema>.Instance;
        }
    }

There are so many code blocks that I can not paste here. I would be very happy if someone could help.

If you would like to see the project, you can check here;

https://github.com/dogaanismail/DevPlatform

Upvotes: 3

Views: 603

Answers (1)

Ismail Dogan
Ismail Dogan

Reputation: 335

This problem has been solved by adding LinqToDB.Identity class library into the solution. I have created an issue on Github. You can check from this link.

https://github.com/linq2db/linq2db/issues/2400

I have uninstalled LinqToDB.Identity that is a nuget package. Instead of using LinqToDB.Identity nuget package, it is better to use LinqToDB.Identity class library. In addition, I can debug this class library. It is really useful!

enter image description here

You can obtain LinqToDB.Identity class library with this link https://github.com/linq2db/LinqToDB.Identity/tree/master/src/LinqToDB.Identity

or if you would, you can check my project that is called DevPlatform. https://github.com/dogaanismail/DevPlatform

In addition to all of these, I have added an IdentityAttribute for my AppUser primary key. I did not create an user without this attribute.

public class AppUser : IdentityUser<int>, IEntity
{
    [Required, Identity]
    [Key]
    public override int Id { get => base.Id; set => base.Id = value; }
    public DateTime CreatedDate { get; set; }
    public DateTime? ModifiedDate { get; set; }

    public int? CreatedBy { get; set; }
    public int? ModifiedBy { get; set; }
    public int? StatusId { get; set; }
}

As a result of creating a class library and adding LinqToDB.Identity classes, such as IdentityUser, DefaultConnectionFactory, IConcurrency etc. I created an user successfully by using UserManager.

Upvotes: 2

Related Questions