ASP MVC5 身份用户抽象
ASP MVC5 Identity User Abstraction
我想使用默认的 Identity 2 提供程序构建 N-tire Web 应用程序。因此,我的数据层包含带有模型定义的纯 c# classes,没有任何外部依赖性。但是,如果不添加 AspNet.Identity 引用,就不可能 link 一些 class 到我的应用程序用户。
我试过制作用户界面 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; }
}
并在基础设施层中用实现替换它:
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; }
}
但是entity framework不会在实体之间创建关系。
是否有任何'right'方法来实现这个或者是否需要添加参考?
有一个解决方法,在我看来这很丑陋但有效。
您将需要 2 个 class,一个用于 User
,另一个用于 ApplicationUser
。 ApplicationUser
必须具有 User
的所有属性。像这样:
//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; }
}
现在,诀窍是将 User
class 映射到 ApplicationUser
class 的相同 table。像这样:
public class UserConfig : EntityTypeConfiguration<User>
{
public UserConfig()
{
HasKey(u => u.Id);
ToTable("AspNetUsers");
}
}
希望对您有所帮助!
我想使用默认的 Identity 2 提供程序构建 N-tire Web 应用程序。因此,我的数据层包含带有模型定义的纯 c# classes,没有任何外部依赖性。但是,如果不添加 AspNet.Identity 引用,就不可能 link 一些 class 到我的应用程序用户。
我试过制作用户界面 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; }
}
并在基础设施层中用实现替换它:
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; }
}
但是entity framework不会在实体之间创建关系。
是否有任何'right'方法来实现这个或者是否需要添加参考?
有一个解决方法,在我看来这很丑陋但有效。
您将需要 2 个 class,一个用于 User
,另一个用于 ApplicationUser
。 ApplicationUser
必须具有 User
的所有属性。像这样:
//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; }
}
现在,诀窍是将 User
class 映射到 ApplicationUser
class 的相同 table。像这样:
public class UserConfig : EntityTypeConfiguration<User>
{
public UserConfig()
{
HasKey(u => u.Id);
ToTable("AspNetUsers");
}
}
希望对您有所帮助!