❔ Setting up a One-To-Many-Relationship in EntityFramework Core

namespace GetOut.Models;

public class Provider(string slug, string name, string? description)
{
public Guid Id { get; set; } = Guid.NewGuid();

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public string Slug { get; set; } = slug;
public string Name { get; set; } = name;
public string? Description { get; set; } = description;

public bool Verified { get; set; } = false;

public City City { get; set; }
public ICollection<Event>? Events { get; set; }
}
namespace GetOut.Models;

public class Provider(string slug, string name, string? description)
{
public Guid Id { get; set; } = Guid.NewGuid();

public DateTime CreatedAt { get; set; } = DateTime.Now;
public DateTime UpdatedAt { get; set; } = DateTime.Now;
public DateTime? DeletedAt { get; set; }

public string Slug { get; set; } = slug;
public string Name { get; set; } = name;
public string? Description { get; set; } = description;

public bool Verified { get; set; } = false;

public City City { get; set; }
public ICollection<Event>? Events { get; set; }
}
I want to establish a relation between Providers and City. 😉 Thanks.
11 Replies
Timo Martinson
Timo MartinsonOP15mo ago
what do I have to do?
Angius
Angius15mo ago
Just reference one from the other
metomorphose
metomorphose15mo ago
Is it one provider per city or vise versa?
Angius
Angius15mo ago
class Foo
{
public int Id { get; set; }
public Bar Bar { get; set; }
public int BarId { get; set; }
}

class Bar
{
public int Id { get; set; }
public List<Foo> Foos { get; set; }
}
class Foo
{
public int Id { get; set; }
public Bar Bar { get; set; }
public int BarId { get; set; }
}

class Bar
{
public int Id { get; set; }
public List<Foo> Foos { get; set; }
}
metomorphose
metomorphose15mo ago
It should look pretty similar to the Events property
Angius
Angius15mo ago
Here's a simple one-to-many
Timo Martinson
Timo MartinsonOP15mo ago
one city has many providers
Angius
Angius15mo ago
Then add a list of providers to the city And a city to a provider
Timo Martinson
Timo MartinsonOP15mo ago
how does it work ?
Angius
Angius15mo ago
What do you mean? It just does EF picks up on the relationship you created, generates the appropriate SQL to execute, and executes it when you apply the migration
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise 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