C
C#•5d ago
AlisterKB

Fluent API: defining one to many

Models classes :
C#
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Contact { get; set; }
public ICollection<ShippingAddress> ShippingAddressNavigation { get; set; } = new List<ShippingAddress>();
}
public class ShippingAddress
{
public int Id { get; set; }
public string Address { get; set; }
public int CustomerId { get; set; }
public Customer CustomerNavigation { get; set; }

}
C#
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public string Contact { get; set; }
public ICollection<ShippingAddress> ShippingAddressNavigation { get; set; } = new List<ShippingAddress>();
}
public class ShippingAddress
{
public int Id { get; set; }
public string Address { get; set; }
public int CustomerId { get; set; }
public Customer CustomerNavigation { get; set; }

}
Mappings:
C#
public class CustomerMapping : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.ToTable("customers");
builder.HasKey(c => c.Id);
builder.HasMany<ShippingAddress>(c => c.ShippingAddressNavigation).WithOne(s => s.CustomerNavigation).HasForeignKey(s => s.CustomerId);
}
}

public class ShippingAddressMapping : IEntityTypeConfiguration<ShippingAddress>
{
public void Configure(EntityTypeBuilder<ShippingAddress> builder)
{
builder.ToTable(nameof(ShippingAddress));
builder.HasKey(s=>s.Id);
builder.HasOne<Customer>(S => S.CustomerNavigation).WithMany(c => c.ShippingAddressNavigation).HasForeignKey(a=>a.CustomerId);
}
}
C#
public class CustomerMapping : IEntityTypeConfiguration<Customer>
{
public void Configure(EntityTypeBuilder<Customer> builder)
{
builder.ToTable("customers");
builder.HasKey(c => c.Id);
builder.HasMany<ShippingAddress>(c => c.ShippingAddressNavigation).WithOne(s => s.CustomerNavigation).HasForeignKey(s => s.CustomerId);
}
}

public class ShippingAddressMapping : IEntityTypeConfiguration<ShippingAddress>
{
public void Configure(EntityTypeBuilder<ShippingAddress> builder)
{
builder.ToTable(nameof(ShippingAddress));
builder.HasKey(s=>s.Id);
builder.HasOne<Customer>(S => S.CustomerNavigation).WithMany(c => c.ShippingAddressNavigation).HasForeignKey(a=>a.CustomerId);
}
}
So I've commented out one, each time and ran the database, keeping one at the time, builder.HasMany<ShippingAddress>(c => c.ShippingAddressNavigation).WithOne(s => s.CustomerNavigation).HasForeignKey(s => s.CustomerId); builder.HasOne<Customer>(S => S.CustomerNavigation).WithMany(c => c.ShippingAddressNavigation).HasForeignKey(a=>a.CustomerId); appears i get the same results so here are my questions: 1. is there any difference between defining the relationship from Customer or ShippingAddress that I've failed to observe? Am I right to assume I'm achieving the same thing just from different side of the relationship? 2. is one way better in terms of anything (convention, readability, etc)?
7 Replies
Angius
Angius•5d ago
1. No difference at all, no 2. Matter of preference, I'd say
AlisterKB
AlisterKBOP•5d ago
Thanks a lot! appreciate it!
Angius
Angius•5d ago
As a side note, you can take advantage of the "fluent" part of "fluent API" and not repeat builder. so many times
builder
.ToTable(...)
.HasMany(...)
.WithOne(...)
.HasForeignKey(...);
builder
.ToTable(...)
.HasMany(...)
.WithOne(...)
.HasForeignKey(...);
AlisterKB
AlisterKBOP•5d ago
(: appreciate the tip! gotta add builder again after hasKey though right?
Angius
Angius•5d ago
Maybe? You can just reorder the calls so it's the last one Generally, the fluent pattern is based around the idea of a method returning the thing it's being called on, so some sort of
public SomethingBuilder DoFoo(this SomethingBuilder builder, Func<...> fn)
public SomethingBuilder DoFoo(this SomethingBuilder builder, Func<...> fn)
So check if the method returns the builder If so, you can continue chaining off of it
AlisterKB
AlisterKBOP•5d ago
got it! so has key does't wanna play nice with others but all the other ones are fine 😛 thank you so much again!
Angius
Angius•5d ago
:Ok:

Did you find this page helpful?