C
C#14mo ago
xriba

❔ EF Core Fluent API - Unidirectional one to many relationship

How can I implement a one to many unidirectional relationship using EF Core fluent API? (Image 1) I'd like to avoid navigation properties in Coordinate as I believe Square (or any shape for that matter) needs to know about it's coordinates but the coordinates don't need to know about the shapes. I've tried the code below but it's wrong. (Image 2)
internal class Coordinate
{
private int Id { get; set; }
public int X { get; set; }
public int Y { get; set; }
}

internal class Square
{
private int Id { get; set; }
public ISet<Coordinate> Coordinates { get; set; } = new HashSet<Coordinate>();
}

internal class CoordinateConfiguration : IEntityTypeConfiguration<Coordinate>
{
private const string ID = "Id";

public void Configure(EntityTypeBuilder<Coordinate> builder)
{
builder.Property<int>(ID).ValueGeneratedOnAdd();
builder.HasKey(ID);

builder.Property(x => x.X).IsRequired();
builder.Property(x => x.Y).IsRequired();
builder.HasIndex(x => x.X);
builder.HasIndex(x => x.Y);
}
}

internal class SquareConfiguration : IEntityTypeConfiguration<Square>
{
private const string ID = "Id";

public void Configure(EntityTypeBuilder<Square> builder)
{
builder.Property<int>(ID).ValueGeneratedOnAdd();
builder.HasKey(ID);

builder.HasMany(x => x.Coordinates)
.WithOne()
// .HasForeignKey() ??
.IsRequired();

builder.Navigation(x => x.Coordinates).AutoInclude();
}
}
internal class Coordinate
{
private int Id { get; set; }
public int X { get; set; }
public int Y { get; set; }
}

internal class Square
{
private int Id { get; set; }
public ISet<Coordinate> Coordinates { get; set; } = new HashSet<Coordinate>();
}

internal class CoordinateConfiguration : IEntityTypeConfiguration<Coordinate>
{
private const string ID = "Id";

public void Configure(EntityTypeBuilder<Coordinate> builder)
{
builder.Property<int>(ID).ValueGeneratedOnAdd();
builder.HasKey(ID);

builder.Property(x => x.X).IsRequired();
builder.Property(x => x.Y).IsRequired();
builder.HasIndex(x => x.X);
builder.HasIndex(x => x.Y);
}
}

internal class SquareConfiguration : IEntityTypeConfiguration<Square>
{
private const string ID = "Id";

public void Configure(EntityTypeBuilder<Square> builder)
{
builder.Property<int>(ID).ValueGeneratedOnAdd();
builder.HasKey(ID);

builder.HasMany(x => x.Coordinates)
.WithOne()
// .HasForeignKey() ??
.IsRequired();

builder.Navigation(x => x.Coordinates).AutoInclude();
}
}
5 Replies
phaseshift
phaseshift14mo ago
imo coordinate should be a valuetype, not an entity. But what's wrong?
xriba
xriba14mo ago
Yes, it could very well be a value object. I'm just wondering how to, if possible, implement this relationship. In the example above, I'd like to generate the relationship in Square and not in Coordinate as Coordinate does not need to know anything about the shape. However if you look at the second image, you can see that there's a foreign key on Coordinate.
phaseshift
phaseshift14mo ago
EF is always going to put the FK on the child afaik
xriba
xriba14mo ago
I see, thank you.
Accord
Accord14mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts