Permalink
Browse files

Change tableName for AspNetUsers

  • Loading branch information...
rustd rustd
rustd authored and rustd committed Oct 20, 2013
1 parent 5beeddf commit d8a0624797bdab75901206e62977a178bc9cb79a
Showing with 32 additions and 5 deletions.
  1. +12 −3 AspnetIdentitySample/Models/AppModel.cs
  2. +13 −2 AspnetIdentitySample/Views/Home/Index.cshtml
  3. +7 −0 README.md
@@ -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>
{
@@ -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; }
}
@@ -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>
@@ -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>
@@ -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>
View
@@ -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.

2 comments on commit d8a0624

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.

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

Please sign in to comment.