问题
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.
Is there any 'right' way do implement this or it is necesarry to add reference?
回答1:
There's a workaround, which is pretty ugly in my opinion but works.
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; }
}
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");
}
}
Hope it helps!
来源:https://stackoverflow.com/questions/34862770/asp-mvc5-identity-user-abstraction