Manage Identities (EF / Membership) in a WCF service : 'The entity type CustomApplicationUser is not part of the model for the current context'
<p>I would like to know if it's possible to manage users from a <code>WCF service</code>.
For example, my app - which is a client of my WCF service - call a function <code>CreateUser(String login, string password, string email)</code>, and my service create this user using EF identity and / or ASP Membership.</p>

<p>I've installed nuget packages : <code>OWIN/EF.Identity/ASP membership</code>.</p>

<p>I've already tried to create classes in my WCF service :</p>

<p><strong>ApplicationDBContext :</strong></p>

<pre><code>public class ApplicationDbContext : IdentityDbContext&lt;ApplicationUser&gt;
{
    public ApplicationDbContext()
        : base("Scorpion_V1Users")
    {

    }
}
</code></pre>

<p><strong>ApplicationUser :</strong> </p>

<pre><code>public class CustomApplicationUser : IdentityUser
{
    public CustomApplicationUser()
    {
        this.Id = Guid.NewGuid().ToString();
    }

    public CustomApplicationUser(string userName): this()
    {
        UserName = userName;
    }

}
</code></pre>

<p><strong>My custom UserStore :</strong></p>

<pre><code>public class CustomUserStore : IUserStore&lt;CustomApplicationUser&gt;, IUserPasswordStore&lt;CustomApplicationUser&gt;, IUserSecurityStampStore&lt;CustomApplicationUser&gt;
{
    UserStore&lt;IdentityUser&gt; userStore = new UserStore&lt;IdentityUser&gt;(new CustomApplicationDbContext());
    public CustomUserStore()
    {
    }

    public Task CreateAsync(CustomApplicationUser user)
    {
        var context = userStore.Context as CustomApplicationDbContext;
        context.Users.Add(user);
        context.Configuration.ValidateOnSaveEnabled = false;
        return context.SaveChangesAsync();
    }

    public void Create(CustomApplicationUser user)
    {
        var context = userStore.Context as CustomApplicationDbContext;
        context.Users.Add(user);
        context.Configuration.ValidateOnSaveEnabled = false;
        context.SaveChanges();
    }

    public Task DeleteAsync(CustomApplicationUser user)
    {
        var context = userStore.Context as CustomApplicationDbContext;
        context.Users.Remove(user);
        context.Configuration.ValidateOnSaveEnabled = false;
        return context.SaveChangesAsync();
    }

    public Task&lt;CustomApplicationUser&gt; FindByIdAsync(string userId)
    {
        var context = userStore.Context as CustomApplicationDbContext;
        return context.Users.Where(u =&gt; u.Id.ToLower() == userId.ToLower()).FirstOrDefaultAsync();
    }

    public Task&lt;CustomApplicationUser&gt; FindByNameAsync(string userName)
    {
        var context = userStore.Context as CustomApplicationDbContext;
        return context.Users.Where(u =&gt; u.UserName.ToLower() == userName.ToLower()).FirstOrDefaultAsync();
    }

    public Task UpdateAsync(CustomApplicationUser user)
    {
        var context = userStore.Context as CustomApplicationDbContext;
        context.Users.Attach(user);
        context.Entry(user).State = EntityState.Modified;
        context.Configuration.ValidateOnSaveEnabled = false;
        return context.SaveChangesAsync();
    }

    public void Dispose()
    {
        userStore.Dispose();
    }

    public Task&lt;string&gt; GetPasswordHashAsync(CustomApplicationUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.GetPasswordHashAsync(identityUser);
        SetApplicationUser(user, identityUser);
        return task;
    }

    public Task&lt;bool&gt; HasPasswordAsync(CustomApplicationUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.HasPasswordAsync(identityUser);
        SetApplicationUser(user, identityUser);
        return task;
    }

    public Task SetPasswordHashAsync(CustomApplicationUser user, string passwordHash)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.SetPasswordHashAsync(identityUser, passwordHash);
        SetApplicationUser(user, identityUser);
        return task;
    }

    public Task&lt;string&gt; GetSecurityStampAsync(CustomApplicationUser user)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.GetSecurityStampAsync(identityUser);
        SetApplicationUser(user, identityUser);
        return task;
    }

    public Task SetSecurityStampAsync(CustomApplicationUser user, string stamp)
    {
        var identityUser = ToIdentityUser(user);
        var task = userStore.SetSecurityStampAsync(identityUser, stamp);
        SetApplicationUser(user, identityUser);
        return task;
    }

    private static void SetApplicationUser(CustomApplicationUser user, IdentityUser identityUser)
    {
        user.PasswordHash = identityUser.PasswordHash;
        user.SecurityStamp = identityUser.SecurityStamp;
        user.Id = identityUser.Id;
        user.UserName = identityUser.UserName;
    }

    private IdentityUser ToIdentityUser(CustomApplicationUser user)
    {
        return new IdentityUser
        {
            Id = user.Id,
            PasswordHash = user.PasswordHash,
            SecurityStamp = user.SecurityStamp,
            UserName = user.UserName
        };
    }
}
</code></pre>

<p>and my <strong>UserManager</strong> : </p>

<pre><code>public class CustomUserManager 
{
}
</code></pre>

<p>I've created all AspNet tables on my database and added an other EDMX on my WCF project, pointing to them. The newly created connection string is correct (ScorpionV1Users).</p>

<p><a href="https://i.stack.imgur.com/pmE8L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/pmE8L.png" alt="Tables"></a></p>

<pre><code>&lt;add name="Scorpion_V1Users" connectionString="metadata=res://*/Database.Users.csdl|res://*/Database.Users.ssdl|res://*/Database.Users.msl;provider=System.Data.SqlClient;provider connection string=&amp;quot;data source=TANTO;initial catalog=Scorpion_V1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&amp;quot;" providerName="System.Data.EntityClient" /&gt;
</code></pre>

<p>When I try</p>

<pre><code>private CustomUserStore _userStore = new CustomUserStore();
...
var user = new CustomApplicationUser { UserName = login, Email = mail };
_userStore.Create(user);
</code></pre>

<p>in <strong>UserStore</strong>,</p>

<pre><code>public void Create(CustomApplicationUser user)
    {
        var context = userStore.Context as CustomApplicationDbContext;
        context.Users.Add(user);
        context.Configuration.ValidateOnSaveEnabled = false;
        context.SaveChanges();
    }
</code></pre>

<p>the line </p>

<pre><code>context.Users.Add(user);
</code></pre>

<p>throw an error : 'The entity type CustomApplicationUser is not part of the model for the current context'.</p>

<p>I've seen other issues like that, <a href="https://stackoverflow.com/questions/22592770/asp-net-identity-error-the-entity-type-applicationuser-is-not-part-of-the-model/" title="stackoverflow issue">here</a>, or <a href="https://stackoverflow.com/questions/23893710/the-entity-type-applicationuser-is-not-part-of-the-model-for-the-current-context" title="stackoverflow issue">here</a>, but it does not resolve mine aswell.</p>

<p>I think my model to create AspNetUsers is not complete.
What else do I need (classes, nuget package or smt else) to implement it ?</p>

<p>Thanks in advance.</p>