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?
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 theApplicationUser
.ApplicationUser
must have all properties ofUser
. Like this:Now, the trick is mapping the
User
class to the same table of theApplicationUser
class. Like this:Hope it helps!