我正在构建一个带有用户登录身份的 .NET MVC 应用程序。该应用程序有四层。
- Web/UI 层,参考了我的域和服务层
- 领域层,没有引用
- 服务层,参考了我的数据层和领域层
- 数据层,参考了我的领域层
我想要我的AppUser类与我的数据层中的实体之一有关系,Company.
The AppUser类驻留在我的域层中,并且继承自身份用户。我放置我的原因AppUser我的域层中的类是因为我需要从我的 Web/UI 层访问它身份配置需要上课。
但是,当我现在想要添加与数据层中的实体的关系时,事情对我来说变得很棘手。
我希望它看起来像 AppUser 类:
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Security.Claims;
using System.Threading.Tasks;
namespace ITKA.Domain.Classes
{
public class AppUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string ContactNumber { get; set; }
public int CompanyId { get; set; }
public Company Company { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AppUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public void Dispose()
{
}
}
}
公司类别如下:
using ITKA.Data.Interfaces;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace ITKA.Data.Entities
{
[Table("CompanyTbl")]
public class Company : IEntity
{
[Key]
public int Id { get; set; }
[StringLength(100)]
public string CompanyName { get; set; }
public virtual ICollection<InventoryItem> InventoryItems { get; set; }
}
}
IdentityConfig 类,如其外观所示
using System;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security;
using ITKA.Domain.Classes;
using ITKA.Domain.Interfaces;
namespace ITKA.Web
{
public class AppUserManager : UserManager<AppUser>
{
public AppUserManager(IUserStore<AppUser> userStore)
: base(userStore)
{
}
public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options, IOwinContext context)
{
var userStore = new UserStore<AppUser>((IdentityDbContext<AppUser>)context.Get<IITKAContext>());
var manager = new AppUserManager(userStore);
manager.UserValidator = new UserValidator<AppUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 8,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true
};
manager.UserLockoutEnabledByDefault = false;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
return manager;
}
}
public class AppSignInManager : SignInManager<AppUser, string>
{
public AppSignInManager(AppUserManager userManager,
IAuthenticationManager authManager)
: base(userManager, authManager) { }
public static AppSignInManager Create
(IdentityFactoryOptions<AppSignInManager> options, IOwinContext context)
{
return new AppSignInManager(context.Get<AppUserManager>(),
context.Authentication);
}
}
public class AppRoleManager : RoleManager<IdentityRole>
{
public AppRoleManager(RoleStore<IdentityRole> roleStore)
: base(roleStore) { }
public static AppRoleManager Create(IOwinContext context)
{
var roleStore = new RoleStore<IdentityRole>
((IdentityDbContext<AppUser>)context.Get<IITKAContext>());
return new AppRoleManager(roleStore);
}
}
}
我不知道如何放置AppUser我的数据层中的类,以便身份配置能够“到达”它,因为这些层彼此没有引用。
我试图移动我的AppUser类到我的数据层并使其使用我添加到域层的接口,IAppUser。我变了身份配置使用我的界面而不是我的类,但这给了我很多波浪线。
我遇到的第一个问题是,之间没有隐式转换IAppUser and IUser,所以我做了我的IAppUser接口继承自IUser并且解决了一些问题身份配置 class.
但在某些地方它仍然抱怨之间没有隐式转换IAppUser和班级身份用户。我无法让我的接口从类继承,所以实验就结束了。
我不知道是否有办法向我添加关系AppUser也许可以通过域层中的接口将类映射到数据层中的实体?这感觉对我来说不起作用,因为我认为 EF 需要隐式地知道它与哪个类/实体建立了关系。由于许多不同的类都可以从接口继承,这对我来说是不可能的。我错了吗?
或者我可以在我的AppUser类属性与接口和 EF 会创建关系吗?如果我的Company类将使用名为的接口ICompany and my AppUser类具有如下属性:
public ICompany Company { get; set; }
它会与公司建立关系吗?
我遇到麻烦时身份配置需要我的 DbContext 类,它也驻留在我的数据层中,但我可以通过使用域层中的接口来解决这个问题。
我真的被困在这里,我不知道如何解决这个问题。我很感谢任何帮助。