我在用户身份类和另一个名为group的类之间创建了一个实体框架m:m关系。
虽然我已经为这两个类提供了一个链接到另一个类的ICollection属性,但它并没有自动创建多对多的表,而是我用流畅的API - GroupUsers表创建了它。一切似乎都没问题。
当我尝试注册用户时,它会失败。通过查看生成的SQL,似乎期望Group_Id字段出现在Users表中 - 不确定它为什么要查找它,因为它现在位于一个单独的表中。
组类(实体只有一个id属性)
public class Group : Entity
{
public Group()
{
this.Users = new HashSet<User>();
}
[MaxLength(100)]
public string Name { get; set; }
public virtual ICollection<User> Users { get; set; }
}
用户类(自定义应用程序用户类)
public class User : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
{
public User()
{
this.Groups = new HashSet<Group>();
}
[Required]
[MaxLength(100)]
public string Firstname { get; set; }
[Required]
[MaxLength(100)]
public string Lastname { get; set; }
public virtual ICollection<Group> Groups { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<User, int> manager, string authenticationType)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, authenticationType);
// Add custom user claims here
return userIdentity;
}
}
如上所示,两者都与其他类有属性关系。
Fluent API:
modelBuilder.Entity<User>().HasMany(u => u.Groups).WithMany(g => g.Users).Map(x =>
{
x.MapLeftKey("GroupId");
x.MapRightKey("UserId");
x.ToTable("GroupUsers");
});
以上所有内容都创建了表:User,Group和GroupUsers。
我原本预计这会按原样运作。
不幸的是,当我尝试注册用户时,它仍然期望Group_Id出现在User表中:
exec sp_executesql N'SELECT
[Limit1].[Id] AS [Id],
[Limit1].[Firstname] AS [Firstname],
[Limit1].[Lastname] AS [Lastname],
[Limit1].[Email] AS [Email],
[Limit1].[EmailConfirmed] AS [EmailConfirmed],
[Limit1].[PasswordHash] AS [PasswordHash],
[Limit1].[SecurityStamp] AS [SecurityStamp],
[Limit1].[PhoneNumber] AS [PhoneNumber],
[Limit1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed],
[Limit1].[TwoFactorEnabled] AS [TwoFactorEnabled],
[Limit1].[LockoutEndDateUtc] AS [LockoutEndDateUtc],
[Limit1].[LockoutEnabled] AS [LockoutEnabled],
[Limit1].[AccessFailedCount] AS [AccessFailedCount],
[Limit1].[UserName] AS [UserName],
[Limit1].[Group_Id] AS [Group_Id] ---why is this here??
FROM ( SELECT TOP (1)
[Extent1].[Id] AS [Id],
[Extent1].[Firstname] AS [Firstname],
[Extent1].[Lastname] AS [Lastname],
[Extent1].[Email] AS [Email],
[Extent1].[EmailConfirmed] AS [EmailConfirmed],
[Extent1].[PasswordHash] AS [PasswordHash],
[Extent1].[SecurityStamp] AS [SecurityStamp],
[Extent1].[PhoneNumber] AS [PhoneNumber],
[Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed],
[Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled],
[Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc],
[Extent1].[LockoutEnabled] AS [LockoutEnabled],
[Extent1].[AccessFailedCount] AS [AccessFailedCount],
[Extent1].[UserName] AS [UserName],
[Extent1].[Group_Id] AS [Group_Id]
FROM [dbo].[AspNetUsers] AS [Extent1]
WHERE ((UPPER([Extent1].[UserName])) = (UPPER(@p__linq__0))) OR ((UPPER([Extent1].[UserName]) IS NULL) AND (UPPER(@p__linq__0) IS NULL))
) AS [Limit1]',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'myemail@myemail.com'
当我单独运行SQL时,我得到:
列名称无效&#39; Group_Id&#39;。
任何想法为什么它不能正常工作以及为什么group_id应该在那里,即使它在流畅的API中指定它不应该是?
谢谢,