question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Asp.Net Core 3.1 LinqToDB.Identity UserManager CreateAsync Error

See original GitHub issue

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)’.”}

Here is an image that shows the error.

https://i.stack.imgur.com/8oZ98.png

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;
    }
}`

Here is my Register api that can be used to create an user by using UserManager.

`  public async Task<JsonResult> Register([FromBody] RegisterApiRequest model)
    {
        try
        {
            if (model.RePassword != model.Password)
            {
                return BadResponse(ResultModel.Error("Repassword must match password"));
            }

            AppUser userEntity = new AppUser
            {
                UserName = model.UserName,
                Email = model.Email,
                CreatedDate = DateTime.Now,
                DetailId = 1
            };

            IdentityResult result = await _userManager.CreateAsync(userEntity, model.Password);
            if (!result.Succeeded)
            {
                Result.Status = false;
                Result.Message = string.Join(",", result.Errors.Select(x => x.Description));
                return BadResponse(Result);
            }
            return OkResponse(Result);
        }
        catch (Exception ex)
        {
            Result.Status = false;
            Result.Message = ex.ToString();
            return BadResponse(Result);
        }
    }`

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

I have asked this question on Stackoverflow. Here is the link.

https://stackoverflow.com/questions/63326498/asp-net-core-3-1-linqtodb-identity-usermanager-createasync-error

Any help would be appreciated!

Environment details

linq2db version: v3.0.1 Database Server: Microsoft Sql Server Database Provider: SqlServerDataProvider Operating system: Windows 10 .NET Framework: .Net Core 3.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sdanylivcommented, Aug 10, 2020

I see some mix of EF and linq2db. DatabaseGenerated(DatabaseGeneratedOption.Identity) is not linq2db attribute. Try to use [Identity] attribute instead. Sorry it’s to late here and I’m just giving ideas without trying them. Method not found exception means that some reflection code is used in exception path, that’s why I’ve mentioned that I have not found anything “criminal” in code.

0reactions
dogaanismailcommented, Aug 11, 2020

Sure, you can close the issue. Thanks a lot for your help.

Best Regards

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asp.Net Core 3.1 LinqToDB.Identity UserManager ...
This problem has been solved by adding LinqToDB.Identity class library into the solution. I have created an issue on Github.
Read more >
ASP.NET CORE Identity UserManager CreateAsync schema ...
I realized that I had the default schema set at myDomain\myNetworkName schema in the dbContext.OnModeCreating(). I changed it to "shared" like below the ......
Read more >
UserManager<TUser>.CreateAsync Method
Creates the specified user in the backing store with no password, as an asynchronous operation.
Read more >
Custom User Management in ASP.NET Core MVC ...
We will build a small yet practical implementation of Custom User Management in ASP.NET Core MVC with Identity. This will cover most of...
Read more >
Configuring LINQ To DB for ASP.NET Core
Available since: 3.0.0-rc.0. In this walkthrough, you will configure an ASP.NET Core application to access a local SQLite Database using LINQ To DB...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found