locked
Model building for 2 foreign keys to itself RRS feed

  • Question

  • User1514251478 posted

    Hello, entity framework don't know how to create the foreign keys for my project. See the code for the model below.

        public class Cactus
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public string Slug { get; set; }
            public int? BreederId { get; set; }
            public Breeder Breeder { get; set; }
            public int? FatherId { get; set; }
            [ForeignKey("FatherId")]
            public Cactus Father { get; set; }
            public int? MotherId { get; set; }
            [ForeignKey("MotherId")]
            public Cactus Mother { get; set; }
            public IEnumerable<Cactus> Children { get; set; }
        }

    I have used the following solution (in ApplicationDbContext OnModelCreating) before but it doesn't work in this case since i want to have both a fatherId and a motherId.

    modelBuilder.Entity<Category>(entity =>
        {
            entity
                .HasMany(e => e.Children)
                .WithOne(e => e.Parent) 
                .HasForeignKey(e => e.ParentId);
        });

    Does anyone have any ideas about how to solve this? Thanks in advance! :)

    Friday, July 10, 2020 8:25 PM

All replies

  • User-474980206 posted

    try using the attribute correctly (its applied to the key):

    public class Cactus
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public string Slug { get; set; }
            public int? BreederId { get; set; }
            public Breeder Breeder { get; set; }
            [ForeignKey("Father")]
            public int? FatherId { get; set; }
            public Cactus Father { get; set; }
            [ForeignKey("Mother")]
            public int? MotherId { get; set; }
            public Cactus Mother { get; set; }
            public IEnumerable<Cactus> Children { get; set; }
        }

    Friday, July 10, 2020 8:34 PM
  • User1514251478 posted

    try using the attribute correctly (its applied to the key):

    
    

    Thanks for the reply. Oh i thougt it was the other way around. Anyhow, i fixed that but i still get this error when i try to make my migration:

    Unable to determine the relationship represented by navigation property 'Cactus.Father' of type 'Cactus'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

    Any other thoughts on how this can be fixed?

    Saturday, July 11, 2020 1:25 PM
  • User-217098811 posted

    <trans oldtip="嗨红工厂" newtip="嗨红工厂">Hi Redhatworks</trans>

    I think you can try this

    public class Cactus
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public string Slug { get; set; }
            public int? BreederId { get; set; }
            public Breeder Breeder { get; set; }
    
            public class Father : Cactus
            {
    [ForeignKey("Father")] public int FatherId { get; set; } } public class Mother : Cactus {
    [ForeignKey("Mother")] public int MotherId { get; set; } } public IEnumerable<Cactus> Children { get; set; } }

    Then add in your context

      public DbSet<Father> Fathers { get; set; }
      public DbSet<Mother> Mothers { get; set; }

    Hope this can help you

    Best Regards

    yinqiu

    Wednesday, July 15, 2020 7:07 AM