Şöyle bir hata alıyorum.
`
The exception 'The types of the properties specified for the foreign key {'ProductId' : ProductId} on entity type 'ProductVariant' do not m
atch the types of the properties in the principal key {'Id' : AggregateRootId<Guid>} on entity type 'Product'. Provide properties that use the same types in the same order.' was thrown while attempting to create an instance. For the different patterns sup
ported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
`
product sınıfı, uzun olmasın diye alakasız fieldları ve methodları sildim
public class Product: AggregateRoot<ProductId, Guid>
{
#pragma warning disable CS8618
public Product() {}
#pragma warning restore CS8618
public Product(
ProductId id,
List<ProductVariant>? variants) : base(id)
{
_variants = variants ?? [];
}
private List<ProductVariant> _variants = [];
public IReadOnlyList<ProductVariant> Variants => _variants.AsReadOnly();
}
ProductVariant sinifi
public class ProductVariant: Entity<ProductVariantId>
{
public ProductVariant(
ProductVariantId id,
ProductId productId,
Product product,): base(id)
{
Product = product;
ProductId = productId;
}
#pragma warning disable CS8618
private ProductVariant() {}
#pragma warning restore CS8618
public ProductId ProductId { get; }
public Product Product { get; }
}
productId
public sealed class ProductId: AggregateRootId<Guid>
{
public override Guid Value { get; protected set; }
// ReSharper disable once ConvertToPrimaryConstructor
public ProductId(Guid value)
{
Value = value;
}
public ProductId()
{
Value = Guid.NewGuid();
}
public override IEnumerable<object> GetEqualityComponents()
{
yield return Value;
}
}
productVariantId
public class ProductVariantId(Guid value): ValueObject
{
public override IEnumerable<object> GetEqualityComponents()
{
yield return Value;
}
public Guid Value { get; } = value;
}
base sınıflarımda sunlar, alakasiz fieldları ve metodları sildim yoksa çok uzun oluyor
public abstract class AggregateRoot<TId, TIdType> : Entity<TId>
where TId : AggregateRootId<TIdType> {
public new AggregateRootId<TIdType> Id { get; protected set; }
protected AggregateRoot(TId id) : base(id) {
Id = id;
}
#pragma warning disable CS8618
protected AggregateRoot() { }
#pragma warning restore CS8618
}
public abstract class AggregateRootId<TId>: ValueObject
{
public abstract TId Value { get; protected set; }
}
public abstract class Entity<TId>: IEquatable<Entity<TId>>, IHasDomainEvents
{
/// <summary>
/// Gets or inits the value of the id
/// </summary>
// ReSharper disable once NullableWarningSuppressionIsUsed
public TId Id { get; protected set; } = default!;
protected Entity(TId id) {
Id = id;
}
/// <summary>
/// Initializes a new instance of the <see cref="Entity{TId}"/> class
/// </summary>
protected Entity() { }
}
public abstract class ValueObject: IEquatable<ValueObject> {
protected ValueObject()
{
}
}
fluent api configurationlarım
product configuration, uzun olmasın diye alakasız kısımları sildim
public class Product: IEntityTypeConfiguration<Domain.Aggregates.Product.Product>
{
public void Configure(EntityTypeBuilder<Domain.Aggregates.Product.Product> builder)
{
builder.ToTable($"{nameof(Domain.Aggregates.Product.Product)}s");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.ValueGeneratedNever()
.HasConversion(
id => id.Value,
value => new ProductId(value));
builder.HasMany(x => x.Variants)
.WithOne(x => x.Product)
.HasForeignKey(x => x.ProductId)
.HasPrincipalKey(x => x.Id);
}
}
product variant configuration sınıfım
public class ProductVariant: IEntityTypeConfiguration<Domain.Aggregates.Product.Entities.ProductVariant>
{
public void Configure(EntityTypeBuilder<Domain.Aggregates.Product.Entities.ProductVariant> builder)
{
builder.ToTable($"{nameof(Domain.Aggregates.Product.Entities.ProductVariant)}s");
builder.HasKey(x => x.Id);
builder.Property(x => x.Id)
.ValueGeneratedNever()
.HasConversion(
id => id.Value,
value => new ProductVariantId(value));
builder.Property(x => x.ProductId)
.ValueGeneratedNever()
.HasConversion(
id => id.Value,
value => new ProductId(value));
builder.HasOne(x => x.Product)
.WithMany(x => x.Variants)
.HasForeignKey(x => x.ProductId)
.HasPrincipalKey(x => x.Id);
}
}
sorunun sebebinin az cok ne olduğunu biliyor gibiyim ama nasıl çözecegimi kavrayamadım
Ef core primary key türü olarak ProductId yerine base class taki AggregateRootId<Guid> olarak algılıyor. ProductVariant sınıfında ise foreign keyin türü ProductId
`