myhn/Infrastructure/EntityTypes/CommentEntityType.cs
2021-01-08 16:26:19 +01:00

39 lines
1.1 KiB
C#

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using MyHN.Domain;
namespace MyHN.Infrastructure.EntityTypes
{
public class CommentEntityType : IEntityTypeConfiguration<Comment>
{
public void Configure(EntityTypeBuilder<Comment> builder)
{
builder.ToTable("comments");
builder.HasKey(o => o.Id);
builder.Property(o => o.Content).IsRequired();
builder.Property(o => o.CreatedAt).IsRequired();
builder.HasOne<Link>()
.WithMany()
.HasForeignKey(o => o.LinkId)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
builder.OwnsMany(o => o.Votes, vote =>
{
vote.ToTable("comment_votes");
vote.WithOwner().HasForeignKey("CommentId");
vote.HasOne<IdentityUser>()
.WithMany()
.HasForeignKey(o => o.CreatedBy)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
vote.HasKey("CommentId", nameof(Vote.CreatedBy));
vote.Property(o => o.Direction).IsRequired();
vote.Property(o => o.CreatedAt).IsRequired();
});
}
}
}