C
C#7mo ago
MaybeJon

✅ Fluent API Relationship Help

Hey there, I am trying to write out the relationship between the Route and OrderRoute table as shown in the picture, but I am lost on actually how to do it. This is my first time working with EF in general. Any advice would be appreciated. What I have so far:
modelBuilder.Entity<Route>(entity =>
{
entity.HasKey(e => e.RouteId); // PK
entity.Property(e => e.RouteId).ValueGeneratedOnAdd(); // AUTO_INCREMENT
entity.Property(e => e.Destination).HasMaxLength(50); // MAX 50 CHARS
entity.Property(e => e.Destination).IsRequired(); // NOT NULL
entity.Property(e => e.Km).IsRequired(); // NOT NULL
entity.Property(e => e.Time).IsRequired(); // NOT NULL
entity.Property(e => e.West).HasMaxLength(50); // MAX 50 CHARS
entity.Property(e => e.West).IsRequired(); // NOT NULL
entity.Property(e => e.East).HasMaxLength(50); // MAX 50 CHARS
entity.Property(e => e.East).IsRequired(); // NOT NULL

// Relationship to OrderRoute
entity.HasOne<OrderRoute>();
});

modelBuilder.Entity<OrderRoute>(entity =>
{
entity.HasKey(e => e.OrderRouteId);
entity.Property(e => e.OrderRouteId).ValueGeneratedOnAdd();
entity.Property(e => e.RouteId).IsRequired();
entity.Property(e => e.ContractId).IsRequired();

// Relationship to Route
entity.HasMany<Route>();
});
modelBuilder.Entity<Route>(entity =>
{
entity.HasKey(e => e.RouteId); // PK
entity.Property(e => e.RouteId).ValueGeneratedOnAdd(); // AUTO_INCREMENT
entity.Property(e => e.Destination).HasMaxLength(50); // MAX 50 CHARS
entity.Property(e => e.Destination).IsRequired(); // NOT NULL
entity.Property(e => e.Km).IsRequired(); // NOT NULL
entity.Property(e => e.Time).IsRequired(); // NOT NULL
entity.Property(e => e.West).HasMaxLength(50); // MAX 50 CHARS
entity.Property(e => e.West).IsRequired(); // NOT NULL
entity.Property(e => e.East).HasMaxLength(50); // MAX 50 CHARS
entity.Property(e => e.East).IsRequired(); // NOT NULL

// Relationship to OrderRoute
entity.HasOne<OrderRoute>();
});

modelBuilder.Entity<OrderRoute>(entity =>
{
entity.HasKey(e => e.OrderRouteId);
entity.Property(e => e.OrderRouteId).ValueGeneratedOnAdd();
entity.Property(e => e.RouteId).IsRequired();
entity.Property(e => e.ContractId).IsRequired();

// Relationship to Route
entity.HasMany<Route>();
});
Thanks
No description
16 Replies
Angius
Angius7mo ago
You only really need to configure one side of the relationship
class Foo
{
public int Id { get; set; }
public int BarId { get; set; } // optional
public Bar Bar { get; set; }
}
class Bar
{
public int Id { get; set; }
public List<Foo> Foos { get; set; }
}
class Foo
{
public int Id { get; set; }
public int BarId { get; set; } // optional
public Bar Bar { get; set; }
}
class Bar
{
public int Id { get; set; }
public List<Foo> Foos { get; set; }
}
builder.Entity<Foo>(entity => {
entity.HasOne(f => f.Bar)
.WithMany(b => b.Foos);
});
builder.Entity<Foo>(entity => {
entity.HasOne(f => f.Bar)
.WithMany(b => b.Foos);
});
Jimmacle
Jimmacle7mo ago
yeah you can get rid of most of that configuration if you follow EF conventions
Angius
Angius7mo ago
Like non-nullable properties automatically being required
MaybeJon
MaybeJon7mo ago
Do you mean attributes in the class themselves instead of using the modelBuilder? Thank you!
Jimmacle
Jimmacle7mo ago
no, i mean nothing at all EF can determine things like primary keys, navigations, required or not, etc just by how you define the properties
MaybeJon
MaybeJon7mo ago
Oh.. I didn't know that
Jimmacle
Jimmacle7mo ago
Conventions for relationship discovery - EF Core
How navigations, foreign keys, and other aspects of relationships are discovered by EF Core model building conventions
Jimmacle
Jimmacle7mo ago
i only use the fluent API to configure things that can't be detected by convention
Angius
Angius7mo ago
For example
class Foo
{
// It's named `Id` so it's the primary key
public int Id { get; set; }
// The name `BarId` means it's the foreign key to `Bar`
// Neither this nor `Bar` are nullable, so neither will be
// the foreign key in the database
public int BarId { get; set; }
// And this sets the relationship to `Bar`
public Bar Bar { get; set; }
}
class Foo
{
// It's named `Id` so it's the primary key
public int Id { get; set; }
// The name `BarId` means it's the foreign key to `Bar`
// Neither this nor `Bar` are nullable, so neither will be
// the foreign key in the database
public int BarId { get; set; }
// And this sets the relationship to `Bar`
public Bar Bar { get; set; }
}
MaybeJon
MaybeJon7mo ago
That's neat but looks very confusing to me tbh. We're required to write it all out for this group project, so it's easier for all my team members to understand Gotcha that's actually such a great feature Thanks a bunch
Jimmacle
Jimmacle7mo ago
school and teaching a less optimal way to do things name a better duo
MaybeJon
MaybeJon7mo ago
Lol yup.. still teaching .NET Framework
Jimmacle
Jimmacle7mo ago
you shouldn't mix annotations and fluent imo between that and EF conventions your model is like triple redundant lul
MaybeJon
MaybeJon7mo ago
Fair But relationship wise with the image I provided, would that be accurate?
Jimmacle
Jimmacle7mo ago
i don't think that's how you're supposed to use the ForeignKey attribute but if you're using some old EF version maybe it is, i've only used Core
MaybeJon
MaybeJon7mo ago
Gotcha, thanks a bunch
Want results from more Discord servers?
Add your server
More Posts