Problem creating foreign key in Entity

Asked

Viewed 140 times

0

I’m trying to create a Primary key composed, until everything well. With this I need to make a Foreign key composed.

I have more or less the following scenario (it is a hypothetical scenario but reflects what I need and my mistake):

public class Keys {
    [Key, Column(Order = 0)]
    public int Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int Key_2 { get; set; }

    // order is important here as defined in "KeyAuthorities" table
    [ForeignKey("KeyAuthorities", Column(Order = 0)]
    public int KeyAuthorities_Key_1 { get; set; }

    [ForeignKey("KeyAuthorities", Column(Order = 1)]
    public int KeyAuthorities_Key_2 { get; set; }

}

public class KeyAuthorities {
    [Key, Column(Order = 0)]
    public int KeyAuthorities_Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int KeyAuthorities_Key_2 { get; set; }

}

The Foreignkeyattribute on Property 'Keyauthorities' on type 'Portaladmcc.Models.Keys' is not Valid. The Foreign key name 'Keyauthorities' was not found on the dependent type 'Portaladmcc.Models.Keys'. The Name value should be a comma separated list of Foreign key Property Names.

What that mistake means?

  • It seems that you missed closing the Foreignkey parentheses. The error indicates that you do not have the "Keyauthorities" browsing property in the "Key" class"

  • What that means basically?

  • I put an answer. I don’t know if the second way really works. I don’t use model with compound FK.

1 answer

0

The error indicates that the EF did not find the property with the name you defined as FK in the annotation.

You can try adding a navigation property to FK:

public class Keys {
    [Key, Column(Order = 0)]
    public int Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int Key_2 { get; set; }

    // order is important here as defined in "KeyAuthorities" table
    [ForeignKey("Authorities")]
    public int KeyAuthorities_Key_1 { get; set; }

    [ForeignKey("Authorities")]
    public int KeyAuthorities_Key_2 { get; set; }

    public KeyAuthorities Authorities { get; set; }
}

public class KeyAuthorities {
    [Key, Column(Order = 0)]
    public int KeyAuthorities_Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int KeyAuthorities_Key_2 { get; set; }

}

The other option to specify FK with annotations would be to annotate the navigation property with the names of the fields that form FK:

public class Keys {
    [Key, Column(Order = 0)]
    public int Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int Key_2 { get; set; }

    // order is important here as defined in "KeyAuthorities" table
    public int KeyAuthorities_Key_1 { get; set; }

    public int KeyAuthorities_Key_2 { get; set; }

    [ForeignKey("KeyAuthorities_Key_1, KeyAuthorities_Key_2")]
    public KeyAuthorities Authorities { get; set; }
}

public class KeyAuthorities {
    [Key, Column(Order = 0)]
    public int KeyAuthorities_Key_1 { get; set; }

    [Key, Column(Order = 1)]
    public int KeyAuthorities_Key_2 { get; set; }

}
  • It keeps making the same mistake, both ways, and theoretically I’m putting navigation map to FK

  • Same mistake is strange. I put another name for FK which is now a property in the class.

  • This Foreingkey parameter has the name of the class that references it?

  • Type, I have to indicate the file name or the name of the Foreign key?

  • No. It has the attribute name or the name of the navigation property. When it is referenced in the parent class, it needs to be in a collection. This tutorial should help you better understand how to set up FK.

  • Let’s open a chat and explain the situation better?

  • Can it be that way? It makes it easier to explain

  • I open a new project here and put these classes in option 2 and it generated the tables with the compound Fks. Try this first to eliminate other problems in your model.

  • I don’t know if it’s some kind of block, but the chat opens just as a read to me.

  • straight, how strange

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.