Base solution for your next web application
Open Closed

Multiple Navigation Properties from same entity - powertool #6938


User avatar
0
PekitP created

Hi,

Trying to create a entity in powertool, lets call it "note". To this note i want to add 2 words ie 2 Navigation Properties. These words is selected from entity Word. But when trying to to this in powertool it get following error:

"Both relationships between 'Note.Word' and 'Word' and between 'Note.Word2' and 'Word' could use {'WordId'} as the foreign key. To resolve this configure the foreign key properties explicitly on at least one of the relationships"

Any idea on how to solve this or do it another way?

Best /Peter


6 Answer(s)
  • User Avatar
    0
    yekalkan created

    Remove one of the word variables. See below screenshot.

    We need to fix this for rad tool.

  • User Avatar
    1
    bbakermmc created

    Dont you also need a second foreign key. Otherwise how would you ever get the 2nd word. I think what the user was saying is the Word Tables has a FK of WordId, but his note table has two references to it WordId and WordId2.

    So SQL would be Select * from note left join word on word.wordId = note.wordid left join word on word.wordId = note.wordid2

    
    [ForeignKey("WordId2")]
    Public Word Word2 {get;set;)
    
  • User Avatar
    0
    PekitP created

    Hi,

    Added foreignkeys explicit on both words, and it resolved the ref issue, but now throws a SQL error instead

    	public virtual int? WordId { get; set; }
    
        [ForeignKey("WordId1")]
        public Word Word { get; set; }
    
        public virtual int? WordId2 { get; set; } 
    
        [ForeignKey("WordId2")]
        public Word Word2 { get; set; }
        
    

    Exception thrown: 'System.Data.SqlClient.SqlException' in System.Private.CoreLib.dll

  • User Avatar
    0
    ismcagdas created
    Support Team

    @PekitP

    I think you should also add a migration. If that doesn't solve your problem, could you share the full stack trace ?

  • User Avatar
    0
    kiandra created

    @PekitP

    Appears you have a typo in your example above.

    The line:

    [ForeignKey("WordId1")]
    

    ...should reference the name of the property correctly

    [ForeignKey("WordId")]
    
  • User Avatar
    0
    PekitP created
    Hi,
    Here is the answer, core EF in most cases it doesn't like use of numbers, and favors references without them.
    Rad tool works with multiple refs to same table when naming the navigation properties like "Wordone" and "Wordtwo" instead of "Word1" and "Word2". I guess it then sorts out the keys. Below is the generated working code: (Good to add to your to-do documentation for Rad tool)
    public virtual int? Wordone { get; set; }
    public Word Wordo { get; set; }
    	
    public virtual int? Wordtwo { get; set; }
    public Word Wordt { get; set; }
    
    Best/Peter