问题描述
我想建立默认身份2提供的N-轮胎的Web应用程序。所以,我的数据层包含模型定义纯C#类,没有任何externad依赖。但是,这是不可能不增加AspNet.Identity参考了一些类,我的应用程序用户联系起来。
I want to build N-tire web application with default Identity 2 provider. So, my Data layer contains pure c# classes with model definition, without any externad dependency. But it is impossible to link some classes to my Application User without adding AspNet.Identity reference.
我试图让用户类的接口:
I have tried to make an interface of User class:
public interface ISystemUser
{
string Id { get; set; }
string Title { get; set; }
}
public class Place
{
public int Id { get; set; }
public string Address { get; set; }
public ISystemUser User { get; set; }
}
和基础设施层实现替代它:
And in Infrastructure layer substitute it with implementation:
public class ApplicationUser : IdentityUser, ISystemUser
{
public string Title { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Place> Places { get; set; }
}
但实体框架没有建立实体之间的关系。
But entity framework does not creates relation between entities.
有没有'正确'的方式就实现这个或者是necesarry添加引用?
Is there any 'right' way do implement this or it is necesarry to add reference?
推荐答案
有一个变通方法,这在我看来是pretty难看,但工作。
There's a workaround, which is pretty ugly in my opinion but works.
您将需要2类,一为用户
,另一个是 ApplicationUser
。 ApplicationUser
必须具有用户
的所有属性。像这样的:
You will need 2 classes, one for the User
and another for the ApplicationUser
. ApplicationUser
must have all properties of User
. Like this:
//Domain Layer
public class User
{
public string Id { get; set; }
public string Title { get; set; }
}
//Infrastructure Layer
public class ApplicationUser
{
public string Title { get; set; }
}
现在,关键是映射用户
类的 ApplicationUser 类的同桌。像这样的:
Now, the trick is mapping the User
class to the same table of the ApplicationUser
class. Like this:
public class UserConfig : EntityTypeConfiguration<User>
{
public UserConfig()
{
HasKey(u => u.Id);
ToTable("AspNetUsers");
}
}
希望它帮助!
这篇关于ASP MVC5用户身份抽象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!