From 39a25233819dfbfd6b1da7a53c860f88b2a0b78e Mon Sep 17 00:00:00 2001 From: YuukanOO Date: Thu, 10 Dec 2020 21:38:09 +0100 Subject: [PATCH] closes #23 refactor to use OwnsMany --- Infrastructure/EntityTypes/LinkEntityType.cs | 13 ++++++++----- Infrastructure/EntityTypes/VoteEntityType.cs | 18 ------------------ Infrastructure/LinkRepository.cs | 2 +- 3 files changed, 9 insertions(+), 24 deletions(-) delete mode 100644 Infrastructure/EntityTypes/VoteEntityType.cs diff --git a/Infrastructure/EntityTypes/LinkEntityType.cs b/Infrastructure/EntityTypes/LinkEntityType.cs index 237b881..ad688cc 100644 --- a/Infrastructure/EntityTypes/LinkEntityType.cs +++ b/Infrastructure/EntityTypes/LinkEntityType.cs @@ -14,11 +14,14 @@ namespace HN.Infrastructure.EntityTypes builder.Property(o => o.CreatedAt).IsRequired(); builder.HasIndex(o => o.Url).IsUnique(); - builder.HasMany(o => o.Votes) - .WithOne() - .HasForeignKey("LinkId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired(); + 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(); + }); } } } diff --git a/Infrastructure/EntityTypes/VoteEntityType.cs b/Infrastructure/EntityTypes/VoteEntityType.cs deleted file mode 100644 index e57bf9f..0000000 --- a/Infrastructure/EntityTypes/VoteEntityType.cs +++ /dev/null @@ -1,18 +0,0 @@ -using HN.Domain; -using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Metadata.Builders; - -namespace HN.Infrastructure.EntityTypes -{ - public sealed class VoteEntityType : IEntityTypeConfiguration - { - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable("link_votes"); - builder.HasKey("LinkId"); - builder.Property(o => o.Type).IsRequired(); - builder.Property(o => o.CreatedAt).IsRequired(); - } - } - -} \ No newline at end of file diff --git a/Infrastructure/LinkRepository.cs b/Infrastructure/LinkRepository.cs index 6eb37c5..87b8614 100644 --- a/Infrastructure/LinkRepository.cs +++ b/Infrastructure/LinkRepository.cs @@ -18,7 +18,7 @@ namespace HN.Infrastructure public Task GetByIdAsync(Guid id) { - return Entries.Include(l => l.Votes).SingleOrDefaultAsync(o => o.Id == id); + return Entries.SingleOrDefaultAsync(o => o.Id == id); } public Task UpdateAsync(Link link)