❔ 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
I've tried the code below but it's wrong. (Image 2)
I'd like to avoid navigation properties in
CoordinateCoordinate 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();
}
}
