Set Max length for names to 256 and add index

This commit is contained in:
Hao Kung 2015-06-08 14:11:53 -07:00
parent 715790c213
commit d8b3cf238e
2 changed files with 82 additions and 0 deletions

View File

@ -33,9 +33,15 @@ namespace Microsoft.AspNet.Identity.EntityFramework
builder.Entity<TUser>(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<TRole>(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);
});

View File

@ -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()
{