Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change schema of IdentityUser Tables in Web Api 2

I'm creating a project with Web Api 2.0 and Using Form Authentication. My problem is changing the default schema names of AspNet auto generated tables.

For example;

dbo.AspNetUsers ---> User.AspNetUsers

I have already tried to change them in the database with ALTER SCHEMA query and I have added this model to my project

public class UserAccount:IdentityDbContext<IdentityUser>{
 .....
}

However I have got the following error;

The entity type UserAccount is not part of the model for the current context

Any idea how I can resolve this issue. Or can I change the default schema from dbo to something else with the Owin

like image 858
Erduran Korkut Avatar asked Apr 06 '14 20:04

Erduran Korkut


2 Answers

I have solved the issue, and the you can see how did I do this

First I have executed the following query

ALTER SCHEMA User TRANSFER dbo.AspNetUsers

and also other related tables too.

In the application, I have added and edmx file with empty table set and referenced to the below IdentityDbContext

public partial class LicaDatabaseEntities : IdentityDbContext<UserAccount>
{
    public LicaDatabaseEntities()
        : base("LicaDatabaseEntities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //throw new UnintentionalCodeFirstException();
        modelBuilder.HasDefaultSchema("User");
        base.OnModelCreating(modelBuilder);
    }

}

in the Startup

UserManagerFactory =
            () => new UserManager<UserAccount>(new UserStore<UserAccount>(new LicaDatabaseEntities()));
like image 68
Erduran Korkut Avatar answered Nov 25 '22 19:11

Erduran Korkut


I'm using the latest version of OWIN and EF as of now (2015-02-26).
I changed a few things and it's using "Membership" SQL Schema instead of "dbo"

0- I changed the connectionString's name:

<connectionStrings>
    <clear/>
    <add name="MembershipConnection" connectionString="Server=.;Integrated Security=True;Database=AWU" providerName="System.Data.SqlClient" />
</connectionStrings>

1-1- I changed IdentityModels.cs like this:

public ApplicationDbContext() : base("MembershipConnection", throwIfV1Schema: false) { }

1-2- I added this override method to IdentityModels.cs

protected override void OnModelCreating(DbModelBuilder modelBuilder) {
    modelBuilder.HasDefaultSchema("Membership");
    base.OnModelCreating(modelBuilder);
}

2- I changed the way EF works with SQL since I have "MS-SQL 2014" installed on my machine

<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
    <parameters>
        <parameter value="MembrshipConnection"/>
    </parameters>
</defaultConnectionFactory>

I believe the most important part is 1-2 which tells the EF to use a default Schema when creating SQL tables. I didn't touch OWIN's Startup.cs

like image 28
Achilles Avatar answered Nov 25 '22 19:11

Achilles