❔ 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 Martinson10mo ago
what do I have to do?
Angius
Angius10mo ago
Just reference one from the other
metomorphose
metomorphose10mo ago
Is it one provider per city or vise versa?
Angius
Angius10mo 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
metomorphose10mo ago
It should look pretty similar to the Events property
Angius
Angius10mo ago
Here's a simple one-to-many
Timo Martinson
Timo Martinson10mo ago
one city has many providers
Angius
Angius10mo ago
Then add a list of providers to the city And a city to a provider
Timo Martinson
Timo Martinson10mo ago
how does it work ?
Angius
Angius10mo 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
Accord10mo 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
More Posts
❔ Getting a wrong result on LeetCode although the IDE shows the correct outputSo I believe I still make some mistake in setting cows' Count for this puzzle https://leetcode.com/p❔ 🐞 Getting a wall of `MSB3030` every other call to `publish`Hi all, I'm working on a `dotnet` project that has an `npm` front-end that gets pre-built and throw❔ Maybe Disposable ObjectI want to have a function that returns an HttpClient or an error. The function would take in JSON A❔ 3D Arrays = good tutorial?Does someone have a good guide/tutorial how to explain 3d arrays. the course on udemy isnt to clear ❔ Blazor - Component button with different event depending on the page that call itHi, I have a component that has 4 buttons (Open, Save, Clear, Run), and this component is used in ❔ C# WPF ListView - How do you create expanded info on selection of a ListView Element?I'm new to WPF and have decided to learn by creating a little tracker for the game Escape From Tarko❔ EFCore, help with atomic update along with complex conditionsBasically, I've been trying to do something like this... ```cs var composite = await _dbContext.Inv❔ Display error message if value out of limitHi, i'm using wpf with mvvm pattern and i'd like to know if it's good to do this in order to display❔ project planningIs youtrack any good or do you recommend a different project planning tool? Would love ide integrat❔ Contacts DB StrategyHey guys! I have a question, maybe someone can give a nice shot with this problem. I have DB where