I have a class library that have
namespace Project.Core
{
[Table("ActorBases")]
public class ActorBase
{
//Properties
}
public class Player : ActorBase
{
//Properties
}
[Table("ActorBases")]
public class Character: ActorBase
{
//Properties
}
public class Match
{
//Properties
public Player Organizor { get; set; }
}
}
Then in my web app, I have this:
namespace Project.Web
{
[Table("ActorBases")]
public class Player : Core.Player
{
public virtual ApplicationUser User{ get; set; }
}
}
to connect a player with a user. Now, when I create a DbContext, first I tried this:
using Project.Core
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long>
{
...
protected override void OnModelCreating(ModelBuilder builder)
{
public DbSet<ActorBase> ActorBases { get; set; }
public DbSet<Project.Web.Player> Players { get; set; }
public DbSet<Character> Characters { get; set; }
public DbSet<Match> Matches { get; set; }
}
}
but there's a problem because table Matches
requires Project.Core.Player
in the table ActorBases
with the discriminator Player
and the class Project.Web.Player
also need the same discriminator. So it fails, saying
An error occurred while calling method 'BuildWebHost' on class 'Program'. Continuing without the application service provider. Error: The discriminator value for 'Player' is 'Player' which is the same for 'Player'. Every concrete entity type in the hierarchy needs to have a unique discriminator value.
So second, I tried to solve this issue by changing the definition of Project.Web.Player
to
[Table("ActorBases")]
public class Player_Web : Core.Player
{
public virtual ApplicationUser User{ get; set; }
}
Then I noticed that even though I could get rid of the problem, I couldn't still write something like: var user = dbContext.Matches.Single(t=>t.Id==1).Organizor.User
. Therefore, I've started to think it was essentially a wrong way that I tried to include the connection beween Player
and ApplicationUser
in the database.
To separate (capsulize?) the core from the web app, it's necessary that the core namespace doesn't know anything about User
that is handled by web app. So, the database should looks like:
using Project.Core
protected override void OnModelCreating(ModelBuilder builder)
{
public DbSet<ActorBase> ActorBases { get; set; }
public DbSet<Player> Players { get; set; }
public DbSet<Character> Characters { get; set; }
public DbSet<Match> Matches { get; set; }
}
and I need to implement a function or api that takes an argument of Player
and returns an object of User
corresponding to the player.
Is my understanding correct in terms of designing and referring a class library?