Skip to content

Commit

Permalink
Change tableName for AspNetUsers
Browse files Browse the repository at this point in the history
  • Loading branch information
rustd authored and rustd committed Oct 20, 2013
1 parent 5beeddf commit d8a0624
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
15 changes: 12 additions & 3 deletions AspnetIdentitySample/Models/AppModel.cs
Expand Up @@ -14,17 +14,17 @@ public class MyUser : IdentityUser
public string HomeTown { get; set; }
}

public class ToDo
public class ToDo
{
public int Id { get; set; }
public string Description { get; set; }
public bool IsDone { get; set; }

// Instead of storing MyUser you can also store UserName or UserId
// and do the lookup in the ToDo Controller by UserName or UserId
// when doing a Create, Update, Lookup or delete operation.
public virtual MyUser User { get; set; }

}
public class MyDbContext : IdentityDbContext<MyUser>
{
Expand All @@ -33,6 +33,15 @@ public MyDbContext()
{
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>()
.ToTable("Users");
modelBuilder.Entity<MyUser>()
.ToTable("Users");
}

public System.Data.Entity.DbSet<AspnetIdentitySample.Models.ToDo> ToDoes { get; set; }
}

Expand Down
15 changes: 13 additions & 2 deletions AspnetIdentitySample/Views/Home/Index.cshtml
Expand Up @@ -23,6 +23,17 @@
</ul>
</dd>
</dl>

<dl>
<dt>Customize Table Name for AspNetUsers</dt>
<dd>
If you want to change the default table name for the Users table, then you can do so
by overriding the default mapping of the EF Code First types to table names.
<strong>Look in Models\AppModel.cs on how we override the table name in ModelCreating event of DbContext</strong>
<a href="http://msdn.microsoft.com/en-US/data/jj591617">For more info on override ModelCreating please visit</a>
</dd>
</dl>

<dl>
<dt>Add profile data for the user</dt>
<dd>
Expand All @@ -49,7 +60,7 @@
<dd>
Do Create, Update, List and Delete Roles.
Only Users In Role Admin can access this page. This uses the [Authorize] on the controller.

</dd>
</dl>
<dl>
Expand All @@ -72,4 +83,4 @@
<li>Only Users in Role Admin can see all ToDoes. Look at ToDoController and All action.</li>
</ul>
</dd>
</dl>
</dl>
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -25,6 +25,13 @@ AspnetIdentitySample
For the code look in <strong>HomeController.cs Profile Action</strong>

</li>
<li>
<b>Customize Table Name for AspNetUsers</b>
If you want to change the default table name for the Users table, then you can do so
by overriding the default mapping of the EF Code First types to table names.
<strong>Look in Models\AppModel.cs on how we override the table name in ModelCreating event of DbContext</strong>
<a href="http://msdn.microsoft.com/en-US/data/jj591617">For more info on override ModelCreating please visit</a>
</li>
<li>
<b>Register a user, Login</b>
Click Register and see the code in AccountController.cs and Register Action.
Expand Down

2 comments on commit d8a0624

@asydneylover
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Paranav,
I want to change the Identity classes' name mainly to remove the prefix "AspNet" because I'd like to see my classes "technology-independent".
But when I try to override the method OnModelCreating, I got an error say "no suitable method found to override". It seems to be that the new Web API has prevented the method OnModelCreating of the base class IdentityDbContext from being override? I'm using ASP.NET Web API 2.1.

@asydneylover
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it by adding using System.Data.Entity.

Please sign in to comment.