1

I have this class

public class Client
{
    public int Id { get; set; }
    public ICollection<Recipe> Recipes { get; set; }
    public ICollection<Recipe> Favorites { get; set; }
}

that has 2 1 to many relationship with the recipe class

public class Recipe
{
    public int Id { get; set; }
    public int ClientId { get; set; }
    public Client Client { get; set; }
}

How can I describe these two relationships? do I need an extra class (favorite)?

Thanks for the help.

EDIT:

I should've been more clear. Client.Recipes is the recipes the client actually owns:

modelBuilder.Entity<Client>() 
    .HasMany(c => c.Recipes)
    .WithRequired(r => r.Client)
    .HasForeignKey(r => r.ClientId);

the problem with Client.Favorites is that it doesn't own them thus Recipe.ClientId is invalid for this particular relationship. I need a relational table for this, ut do I need to express it in a class or can it be expressed in Fluent Api? If yes, how?

Sorry if I wasn't explicit at first.

João Sequeira
  • 157
  • 3
  • 12
  • See the [fluent answer](http://stackoverflow.com/a/5559300/2030565) and here's one with [data annotations](http://stackoverflow.com/questions/28582454/ef-6-how-to-set-two-foreign-keys-to-same-table). – Jasen Feb 22 '17 at 18:32
  • Can't the same recipe belong to another client? That would make it many to many. – Gert Arnold Feb 22 '17 at 19:18
  • So now my question is: can one recipe be favored by more than one client? (I think so) – Gert Arnold Feb 22 '17 at 20:42
  • favorites are many to many concept, talking database wise, and so it is the same for EF – Antoine Pelletier Feb 22 '17 at 20:44
  • Yes, I see that now. Yikes I'm way off. But then I should have a client collection in recipes in order to declare the many to many with the model builder. Correct? – João Sequeira Feb 22 '17 at 21:00
  • If this is EF6 (not ef-core), yes, that's possible. Or leave the model as it is and use the `WithMany-HasMany` mapping for `Favorites`. – Gert Arnold Feb 22 '17 at 21:05
  • @Gert Arnold - I'm probably tired.. but it doens't feel logical to have a client collection in recipes. I can't add a `HasMany-WithMany` without it. It's EF 6.1.3 – João Sequeira Feb 22 '17 at 21:12
  • Yes you can, using the WithMany overload without parameters (`WithMany()`). – Gert Arnold Feb 22 '17 at 21:14
  • @GertArnold that's the knock out punch. Thank you very much. – João Sequeira Feb 22 '17 at 21:18

1 Answers1

1

You should be able to define them as a standard 1 to N relationship. Should be something similar to this

class MyContext : DbContext
{
    public DbSet<Client> Clients { get; set; }
    public DbSet<Recipe> Recipes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Client>()
            .HasOne(p => p.Clients )
            .WithMany(b => b.Recipes )   .HasForeignKey(x => x.ClientId );

        modelBuilder.Entity<Client>()
            .HasOne(p => p.Clients )
            .WithMany(b => b.Favorites )   .HasForeignKey(x => x.ClientId );
    }
}
Steven Ackley
  • 593
  • 7
  • 31