Entity framework relationships

后端 未结 2 1234
轮回少年
轮回少年 2021-01-26 15:08

I have these three entities:

public class Dog
{
    public int DogId { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    publ         


        
2条回答
  •  情歌与酒
    2021-01-26 15:57

    I've only done this using the fluent method (when I was learning I found you can do everything in fluent, but not with annotations, so I've not looked into them), the following creates a many to many between my Unit entity and my UnitService entity:

    modelBuilder.Entity()
                .HasMany(u => u.Services)
                .WithMany(us => us.Units);
    

    This code is in the protected override void OnModelCreating(DbModelBuilder modelBuilder) method.

    In your case Event is Unit and Dog is UnitService.

    Oh ooops, you don't need that at all, your 'join' table is your results table, in my case I don't care about the join table so its all hidden. Maybe something like:

       modelBuilder.Entity()
                   .HasMany(e => e.Results);
       modelBuilder.Entity()
                   .HasMany(d => d.Results);
    

提交回复
热议问题