From 57cf391581a4a7604812c8b3eebabe1e8f852707 Mon Sep 17 00:00:00 2001 From: David Fowler Date: Fri, 26 Sep 2014 01:27:29 -0700 Subject: [PATCH] React to EF changes - Id properties are automatically identity columns - Disable identity generation for the CustomPocoTest since they were setting the id manually - Refactored the CustomDbContext so the the TValue is TKey instead --- .../CustomPocoTest.cs | 51 +++++++++++-------- 1 file changed, 31 insertions(+), 20 deletions(-) diff --git a/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs b/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs index 6a3bff428d..fabe351144 100644 --- a/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs +++ b/test/Microsoft.AspNet.Identity.SqlServer.Test/CustomPocoTest.cs @@ -1,14 +1,16 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using Microsoft.AspNet.Identity.Test; -using Microsoft.Data.Entity; -using Microsoft.Framework.DependencyInjection; -using Microsoft.Framework.DependencyInjection.Fallback; -using Microsoft.Framework.OptionsModel; using System; using System.Linq; using System.Threading.Tasks; +using JetBrains.Annotations; +using Microsoft.AspNet.Identity.Test; +using Microsoft.Data.Entity; +using Microsoft.Data.Entity.Metadata; +using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.DependencyInjection.Fallback; +using Microsoft.Framework.OptionsModel; using Xunit; namespace Microsoft.AspNet.Identity.SqlServer.Test @@ -24,28 +26,37 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test public string UserName { get; set; } } - public class CustomDbContext : DbContext where TUser : class + public class CustomDbContext : DbContext where TKey : IEquatable { - public DbSet Users { get; set; } + public DbSet> Users { get; set; } - public CustomDbContext(IServiceProvider services) : - base(services, services.GetService>().Options) { } + public CustomDbContext(IServiceProvider services) : + base(services, services.GetService>().Options) + { } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + modelBuilder.Entity>().Property(p => p.Id) + .GenerateValuesOnAdd(generateValues: false); + } } - public CustomDbContext GetContext() where TUser : class + public CustomDbContext GetContext() where TKey : IEquatable { var services = new ServiceCollection(); services.Add(OptionsServices.GetDefaultServices()); services.AddEntityFramework().AddSqlServer(); services.SetupOptions(options => options.UseSqlServer(ConnectionString)); var serviceProvider = services.BuildServiceProvider(); - return new CustomDbContext(serviceProvider); + return new CustomDbContext(serviceProvider); } - public CustomDbContext CreateContext(bool delete = false) where TUser : class + public CustomDbContext CreateContext(bool delete = false) where TKey : IEquatable { - var db = GetContext(); + var db = GetContext(); if (delete) { db.Database.EnsureDeleted(); @@ -70,14 +81,14 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test public void DropDb() { - var db = GetContext>(); + var db = GetContext(); db.Database.EnsureDeleted(); } [Fact] public async Task CanUpdateNameGuid() { - using (var db = CreateContext>(true)) + using (var db = CreateContext(true)) { var oldName = Guid.NewGuid().ToString(); var user = new User { UserName = oldName, Id = Guid.NewGuid() }; @@ -94,7 +105,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [Fact] public async Task CanUpdateNameString() { - using (var db = CreateContext>(true)) + using (var db = CreateContext(true)) { var oldName = Guid.NewGuid().ToString(); var user = new User { UserName = oldName, Id = Guid.NewGuid().ToString() }; @@ -111,7 +122,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [Fact] public async Task CanCreateUserInt() { - using (var db = CreateContext>(true)) + using (var db = CreateContext(true)) { var user = new User { Id = 11 }; db.Users.Add(user); @@ -126,7 +137,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [Fact] public async Task CanCreateUserIntViaSet() { - using (var db = CreateContext>(true)) + using (var db = CreateContext(true)) { var user = new User { Id = 11 }; var users = db.Set>(); @@ -142,7 +153,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [Fact] public async Task CanUpdateNameInt() { - using (var db = CreateContext>(true)) + using (var db = CreateContext(true)) { var oldName = Guid.NewGuid().ToString(); var user = new User { UserName = oldName, Id = 1 }; @@ -159,7 +170,7 @@ namespace Microsoft.AspNet.Identity.SqlServer.Test [Fact] public async Task CanUpdateNameIntWithSet() { - using (var db = CreateContext>(true)) + using (var db = CreateContext(true)) { var oldName = Guid.NewGuid().ToString(); var user = new User { UserName = oldName, Id = 1 };