C
C#2d ago
xsainteer

IdentityUser, how to configure OnModelCreating with domain entities

Im trying to follow Clean Architecture I use a custom class, ApplicationUser (inherits IdentityUser), and its implemented in Infrastructure because it relates to DataBase, external source I have other entities, for example, class Vote, that should have one-to-one relationship to ApplicationUser. But i can not use ApplicationUser class in Domain (where Vote and other entities are stored), leading to that i can not make one-to-one relationship between ApplicationUser and Vote (though i really need to). Im asking for a solution that wouldnt violate Clean Architecture and other practices
2 Replies
glhays
glhaysthis hour
Can you show us the code for these two classes?
xsainteer
xsainteerOPthis hour
entity:
public class Vote
{
public Guid PollId { get; set; }
public Poll Poll { get; set; } = null!;

public Guid PollOptionId { get; set; }
public PollOption PollOption { get; set; } = null!;

public Guid UserId { get; set; }
public IUser User { get; set; } = null!;

public DateTime VotedAt { get; set; }
}
public class Vote
{
public Guid PollId { get; set; }
public Poll Poll { get; set; } = null!;

public Guid PollOptionId { get; set; }
public PollOption PollOption { get; set; } = null!;

public Guid UserId { get; set; }
public IUser User { get; set; } = null!;

public DateTime VotedAt { get; set; }
}
user:
public class VoteHubUser : IdentityUser<Guid>, IUser
{
public string Nickname { get; set; } = null!;
public ICollection<Poll> CreatedPolls { get; set; } = [];
public ICollection<Vote> GivenVotes { get; set; } = [];
}
public class VoteHubUser : IdentityUser<Guid>, IUser
{
public string Nickname { get; set; } = null!;
public ICollection<Poll> CreatedPolls { get; set; } = [];
public ICollection<Vote> GivenVotes { get; set; } = [];
}
after my research i just created an interface IUser (stored in Domain), so i can make a navigation property in onmodelcreating
public interface IUser
{
public string Nickname { get; set; }
public ICollection<Vote> GivenVotes { get; set; }
public ICollection<Poll> CreatedPolls { get; set; }
}
public interface IUser
{
public string Nickname { get; set; }
public ICollection<Vote> GivenVotes { get; set; }
public ICollection<Poll> CreatedPolls { get; set; }
}
thats Vote Configuration:
public class VoteConfiguration : IEntityTypeConfiguration<Vote>
{
public void Configure(EntityTypeBuilder<Vote> builder)
{
builder.HasKey(v => new { v.UserId, v.PollId });

builder.HasOne(v => v.User)
.WithMany(u => u.GivenVotes)
.HasForeignKey(v => v.UserId);

builder.HasOne(v => v.Poll)
.WithMany(p => p.Participants)
.HasForeignKey(v => v.PollId);
}
}
public class VoteConfiguration : IEntityTypeConfiguration<Vote>
{
public void Configure(EntityTypeBuilder<Vote> builder)
{
builder.HasKey(v => new { v.UserId, v.PollId });

builder.HasOne(v => v.User)
.WithMany(u => u.GivenVotes)
.HasForeignKey(v => v.UserId);

builder.HasOne(v => v.Poll)
.WithMany(p => p.Participants)
.HasForeignKey(v => v.PollId);
}
}
im not sure if its right way to do though

Did you find this page helpful?