1 // Copyright (c) .NET Foundation. All rights reserved.
2 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3 
4 using System;
5 using Microsoft.EntityFrameworkCore;
6 using Microsoft.Extensions.DependencyInjection;
7 using Xunit;
8 
9 namespace Microsoft.AspNetCore.Identity.EntityFrameworkCore.Test
10 {
11     public class StringUser : IdentityUser
12     {
StringUser()13         public StringUser()
14         {
15             Id = Guid.NewGuid().ToString();
16             UserName = Id;
17         }
18     }
19 
20     public class StringRole : IdentityRole<string>
21     {
StringRole()22         public StringRole()
23         {
24             Id = Guid.NewGuid().ToString();
25             Name = Id;
26         }
27     }
28 
29     public class UserStoreStringKeyTest : SqlStoreTestBase<StringUser, StringRole, string>
30     {
UserStoreStringKeyTest(ScratchDatabaseFixture fixture)31         public UserStoreStringKeyTest(ScratchDatabaseFixture fixture)
32             : base(fixture)
33         { }
34 
35         [Fact]
AddEntityFrameworkStoresCanInferKey()36         public void AddEntityFrameworkStoresCanInferKey()
37         {
38             var services = new ServiceCollection();
39             services.AddLogging()
40                 .AddSingleton(new TestDbContext(new DbContextOptionsBuilder<TestDbContext>().Options));
41             // This used to throw
42             var builder = services.AddIdentity<StringUser, StringRole>().AddEntityFrameworkStores<TestDbContext>();
43 
44             var sp = services.BuildServiceProvider();
45             using (var csope = sp.CreateScope())
46             {
47                 Assert.NotNull(sp.GetRequiredService<UserManager<StringUser>>());
48                 Assert.NotNull(sp.GetRequiredService<RoleManager<StringRole>>());
49             }
50         }
51 
52         [Fact]
AddEntityFrameworkStoresCanInferKeyWithGenericBase()53         public void AddEntityFrameworkStoresCanInferKeyWithGenericBase()
54         {
55             var services = new ServiceCollection();
56             services.AddLogging()
57                 .AddSingleton(new TestDbContext(new DbContextOptionsBuilder<TestDbContext>().Options));
58             // This used to throw
59             var builder = services.AddIdentityCore<IdentityUser<string>>().AddRoles<IdentityRole<string>>().AddEntityFrameworkStores<TestDbContext>();
60 
61             var sp = services.BuildServiceProvider();
62             using (var csope = sp.CreateScope())
63             {
64                 Assert.NotNull(sp.GetRequiredService<UserManager<IdentityUser<string>>>());
65                 Assert.NotNull(sp.GetRequiredService<RoleManager<IdentityRole<string>>>());
66             }
67         }
68     }
69 }