From d8b3cf238e3a106ebb3e08b1eb9f5661f997cdc0 Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Mon, 8 Jun 2015 14:11:53 -0700 Subject: [PATCH] Set Max length for names to 256 and add index --- .../IdentityDbContext.cs | 10 +++ .../SqlStoreTestBase.cs | 72 +++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs index 1b10d0f892..da79b11553 100644 --- a/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs +++ b/src/Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs @@ -33,9 +33,15 @@ namespace Microsoft.AspNet.Identity.EntityFramework builder.Entity(b => { b.Key(u => u.Id); + b.Index(u => u.NormalizedUserName).Name("UserNameIndex"); + b.Index(u => u.NormalizedEmail).Name("EmailIndex"); b.Table("AspNetUsers"); b.Property(u => u.ConcurrencyStamp).ConcurrencyToken(); + b.Property(u => u.UserName).MaxLength(256); + b.Property(u => u.NormalizedUserName).MaxLength(256); + b.Property(u => u.Email).MaxLength(256); + b.Property(u => u.NormalizedEmail).MaxLength(256); b.Collection(u => u.Claims).InverseReference().ForeignKey(uc => uc.UserId); b.Collection(u => u.Logins).InverseReference().ForeignKey(ul => ul.UserId); b.Collection(u => u.Roles).InverseReference().ForeignKey(ur => ur.UserId); @@ -44,9 +50,13 @@ namespace Microsoft.AspNet.Identity.EntityFramework builder.Entity(b => { b.Key(r => r.Id); + b.Index(r => r.NormalizedName).Name("RoleNameIndex"); b.Table("AspNetRoles"); b.Property(r => r.ConcurrencyStamp).ConcurrencyToken(); + b.Property(u => u.Name).MaxLength(256); + b.Property(u => u.NormalizedName).MaxLength(256); + b.Collection(r => r.Users).InverseReference().ForeignKey(ur => ur.RoleId); b.Collection(r => r.Claims).InverseReference().ForeignKey(rc => rc.RoleId); }); diff --git a/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs b/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs index eecbe46072..e5f3d8e52c 100644 --- a/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs +++ b/test/Microsoft.AspNet.Identity.EntityFramework.Test/SqlStoreTestBase.cs @@ -2,11 +2,13 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; +using System.Data.SqlClient; using System.Linq; using System.Linq.Expressions; using System.Security.Claims; using System.Threading.Tasks; using Microsoft.AspNet.Identity.Test; +using Microsoft.Data.Entity.Relational; using Microsoft.Framework.DependencyInjection; using Xunit; @@ -105,6 +107,76 @@ namespace Microsoft.AspNet.Identity.EntityFramework.Test CreateContext(); } + [Fact] + public void EnsureDefaultSchema() + { + VerifyDefaultSchema(CreateContext()); + } + + internal static void VerifyDefaultSchema(TestDbContext dbContext) + { + var sqlDb = dbContext.Database as RelationalDatabase; + var sqlConn = sqlDb?.Connection; + + Assert.NotNull(sqlConn); + using (var db = new SqlConnection(sqlConn.ConnectionString)) + { + db.Open(); + Assert.True(VerifyColumns(db, "AspNetUsers", "Id", "UserName", "Email", "PasswordHash", "SecurityStamp", + "EmailConfirmed", "PhoneNumber", "PhoneNumberConfirmed", "TwoFactorEnabled", "LockoutEnabled", + "LockoutEnd", "AccessFailedCount", "ConcurrencyStamp", "NormalizedUserName", "NormalizedEmail")); + Assert.True(VerifyColumns(db, "AspNetRoles", "Id", "Name", "NormalizedName", "ConcurrencyStamp")); + Assert.True(VerifyColumns(db, "AspNetUserRoles", "UserId", "RoleId")); + Assert.True(VerifyColumns(db, "AspNetUserClaims", "Id", "UserId", "ClaimType", "ClaimValue")); + Assert.True(VerifyColumns(db, "AspNetUserLogins", "UserId", "ProviderKey", "LoginProvider", "ProviderDisplayName")); + + VerifyIndex(db, "AspNetRoles", "RoleNameIndex"); + VerifyIndex(db, "AspNetUsers", "UserNameIndex"); + VerifyIndex(db, "AspNetUsers", "EmailIndex"); + db.Close(); + } + } + + internal static bool VerifyColumns(SqlConnection conn, string table, params string[] columns) + { + var count = 0; + using ( + var command = + new SqlCommand("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS where TABLE_NAME=@Table", conn)) + { + command.Parameters.Add(new SqlParameter("Table", table)); + using (var reader = command.ExecuteReader()) + { + while (reader.Read()) + { + count++; + if (!columns.Contains(reader.GetString(0))) + { + return false; + } + } + return count == columns.Length; + } + } + } + + internal static void VerifyIndex(SqlConnection conn, string table, string index) + { + using ( + var command = + new SqlCommand( + "SELECT COUNT(*) FROM sys.indexes where NAME=@Index AND object_id = OBJECT_ID(@Table)", conn)) + { + command.Parameters.Add(new SqlParameter("Index", index)); + command.Parameters.Add(new SqlParameter("Table", table)); + using (var reader = command.ExecuteReader()) + { + Assert.True(reader.Read()); + Assert.True(reader.GetInt32(0) > 0); + } + } + } + [Fact] public void CanCreateUserUsingEF() {