Julien Leicher 66cc78d30e comment-a-link (#24)
Closes #23 refactor to use OwnsMany
2020-12-11 09:46:42 +01:00

28 lines
821 B
C#

using HN.Domain;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace HN.Infrastructure.EntityTypes
{
public sealed class LinkEntityType : IEntityTypeConfiguration<Link>
{
public void Configure(EntityTypeBuilder<Link> builder)
{
builder.ToTable("links");
builder.HasKey(o => o.Id);
builder.Property(o => o.Url).IsRequired().HasMaxLength(500);
builder.Property(o => o.CreatedAt).IsRequired();
builder.HasIndex(o => o.Url).IsUnique();
builder.OwnsMany(o => o.Votes, vote =>
{
vote.ToTable("link_votes");
vote.WithOwner().HasForeignKey("LinkId");
vote.HasKey("LinkId");
vote.Property(o => o.Type).IsRequired();
vote.Property(o => o.CreatedAt).IsRequired();
});
}
}
}