C
C#2y ago
:

✅ Foreign key not working

Hi, I tried making a foreign key in my table, to another table which I made but it does not work as intended.
[ForeignKey("FileName")]
public Guid FileName { get; set; }
public TextFileModel TextFileModel { get; set; }
[ForeignKey("FileName")]
public Guid FileName { get; set; }
public TextFileModel TextFileModel { get; set; }
I tried making a filename field which is a fk to a textfilemodel, but it has instead made two separate fields:
11 Replies
Patrick
Patrick2y ago
because by convention your nav prop should be the same as the FK
:
:OP2y ago
So it has to be TextFileModel?
Patrick
Patrick2y ago
is FileName the FK for TextFileModel?
:
:OP2y ago
Yeah I tried this and it seems to have worked
Patrick
Patrick2y ago
public Guid TextFileModelId { get; set; }
public TextFileModel TextFileModel { get; set; }
public Guid TextFileModelId { get; set; }
public TextFileModel TextFileModel { get; set; }
:
:OP2y ago
Alright I have another question, in my textfilemodel I have two foreign keys to the asp.net user table, and for some reason they allow nulls even though I did not specify for them to
:
:OP2y ago
:
:OP2y ago
//Author

[ForeignKey("UserId")]
public string AuthorId { get; set; }

public IdentityUser Author { get; set; }


//LastEditedBy

[ForeignKey("UserId")]
public string LastEditedById { get; set; }

public IdentityUser LastEditedBy { get; set; }
//Author

[ForeignKey("UserId")]
public string AuthorId { get; set; }

public IdentityUser Author { get; set; }


//LastEditedBy

[ForeignKey("UserId")]
public string LastEditedById { get; set; }

public IdentityUser LastEditedBy { get; set; }
and thats the code
Patrick
Patrick2y ago
because strings are nullable by default [Required]
:
:OP2y ago
Alright, thanks!
Accord
Accord2y ago
Closed!
Want results from more Discord servers?
Add your server