whisperdancer / AspNet.Identity.Dapper

AspNet Identity 2.0 Storage Provider using Dapper
85 stars 29 forks source link

A parameterless default constructor or one matching signature (System.String LoginProvider, System.String ProviderKey, System.Int32 MemberId) is required for Microsoft.AspNet.Identity.UserLoginInfo materialization #1

Open fireole opened 9 years ago

fireole commented 9 years ago

Hello, I downloaded the dapper solution and love it. I mostly have reading through the code and learning some things.

Anyways, to Repo this issue:

Got the code. Right clicked on the Databae.Member project and published to localhost db. Ran the project and default UI shows up. Went to register. Peeked in db Members table to validate my info is there. Good. Clicked on my email in header, and then popped above message that showed up in UserLoginsTable.cs -> FindByUserId(int memberId)

Any ideas on what I could be missing. Thank You

image

DumboJetEngine commented 9 years ago

Solution:

        public List<UserLoginInfo> FindByUserId(int memberId)
        {
            return db.Connection.Query<UserLoginInfoEx>("Select * from MemberLogin where MemberId = @memberId", new { memberId = memberId })
                .Select(x => new UserLoginInfo(x.LoginProvider, x.ProviderKey))
                .ToList();
        }
    public class UserLoginInfoEx
    {
        /// <summary>
        ///     Provider for the linked login, i.e. Facebook, Google, etc.
        /// </summary>
        public string LoginProvider { get; set; }

        /// <summary>
        ///     User specific key for the login provider
        /// </summary>
        public string ProviderKey { get; set; }
    }

Note however, that this source code needs further improvements. E.g. it seems that Identity relied on EF for caching data to avoid hitting the DB too often. You will probably have to implement some custom caching as well... (so do I... :( )

CanMehmetK commented 9 years ago

Same Problem accurs on my side too ... Also many time hitting to db is a problem :( Any one could handle ?

fireole commented 8 years ago

has anybody been able to get this to work? @DumboJetEngine , have you been able to get a working going with caching mechanism that you mentioned. I am about ready to bite the bullet and use the EF version i have already. I am just so swamped with work right now and might have to circle back on this.

DumboJetEngine commented 8 years ago

@fireole Not yet (I have it in my todo list), but I think it would not be hard to add some caching. Here are some links. This seems to be using MemoryCache: http://stevescodingblog.co.uk/net4-caching-with-mvc/ If you like, you might be able to use WebCache instead: http://patrickdesjardins.com/blog/using-the-system-web-cache-object-into-your-mvc3-project Here is their difference: http://stackoverflow.com/a/13704407 I hope this helps. :)

ghost commented 8 years ago

Hello Guys,

Someone has fixed @fireole issue? It's happening with me either.

This method FindByUserId.

My table MemberLogin does not have any data, it suppose to has?

Sorry for my english and thanks!

DumboJetEngine commented 8 years ago

@lucashalen Have you tried the code above?

ghost commented 8 years ago

Yes I tried and not worked. :(

DumboJetEngine commented 8 years ago

Make sure you use this:

...... new UserLoginInfoEx(x.LoginProvider, x.ProviderKey))

instead of this:

...... new UserLoginInfo(x.LoginProvider, x.ProviderKey))
ghost commented 8 years ago

But in this case I'll have to change the return type too, right?

public List < UserLoginInfo > FindByUserId(int memberId)

for

public List < UserLoginInfoEx > FindByUserId(int memberId)

DumboJetEngine commented 8 years ago

My bad. This is the correct code:

    public List<UserLoginInfo> FindByUserId(int memberId)
    {
            return db.Connection.Query<UserLoginInfoEx>("Select * from MemberLogin where MemberId = @memberId", new {memberId = memberId})
                .Select(x => new UserLoginInfo(x.LoginProvider, x.ProviderKey))
                .ToList();
    }

I have fixed it above too. I hope this helps. :)

ghost commented 8 years ago

UserLoginInfoEx is a generic type or I must define a class for it?

Well, I'll try and come back later!

Very thanks for the help! :+1:

jonfreynik commented 4 years ago

Wow 2015 this is an oldie but I'm implementing a custom User Identity login using dapper and this project has been helping - another solution to this problem would be to change the query to only return LoginProvider and ProviderKey from the table instead of *

public List<UserLoginInfo> FindByUserId(long userId)
{
    return db.Connection
        .Query<UserLoginInfo>("SELECT LoginProvider, ProviderKey FROM UserLogins WHERE UserId = @UserId", 
        new { UserId = userId }).ToList();
}

P.S. my UserId is a Int64 / bigint / long instead of a regular int32 / int.