C
C#11mo ago
ShuajbM

Some help with foreign key in .net

When im trying to add a foreign key to a table it shows an error "ALTER table "
14 Replies
canton7
canton711mo ago
I'm afraid we need significantly more information to help you. * What is the structure of the table you're trying to add a foreign key to? * What is the foreign key pointing to? * What is the exact, full error message you've got?
ShuajbM
ShuajbMOP11mo ago
i have 2 tables : Puna(it means Jobs) and Categories . In Category i have attributes like :
c#
public class Category
{
[Key]
public int Id { get; set; }
[Required]
[DisplayName("Emri i kategorise")]
[MaxLength(30)]
public string Name { get; set; }

[DisplayName("Display Order")]
[Range(1, 100)]
public int DisplayOrder { get; set; }
c#
public class Category
{
[Key]
public int Id { get; set; }
[Required]
[DisplayName("Emri i kategorise")]
[MaxLength(30)]
public string Name { get; set; }

[DisplayName("Display Order")]
[Range(1, 100)]
public int DisplayOrder { get; set; }
i added this table to mssql correctly so i did this migration through console and it was added successfully then i have other class Puna(Jobs) with those attributes:
c#
public class Puna
{
[Key]
public int Id { get; set; }

[Required]
[MaxLength(100)]
[DisplayName("Pozita e Punes")]
public string Emri_Pozites { get; set; }

[Required]
[MaxLength(100)]
public string Pershkrimi { get; set; }
[Required]
public string Kerkesat { get; set; }
[Required]
public string Lokacioni { get; set; }

public string ImageUrl { get; set; }

public int CategoryId { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }



}
c#
public class Puna
{
[Key]
public int Id { get; set; }

[Required]
[MaxLength(100)]
[DisplayName("Pozita e Punes")]
public string Emri_Pozites { get; set; }

[Required]
[MaxLength(100)]
public string Pershkrimi { get; set; }
[Required]
public string Kerkesat { get; set; }
[Required]
public string Lokacioni { get; set; }

public string ImageUrl { get; set; }

public int CategoryId { get; set; }
[ForeignKey("CategoryId")]
public Category Category { get; set; }



}
Jimmacle
Jimmacle11mo ago
why aren't you using migrations? oh, you did
ShuajbM
ShuajbMOP11mo ago
im using them
Jimmacle
Jimmacle11mo ago
i misread i added this table to mssql correctly to mean you weren't
ShuajbM
ShuajbMOP11mo ago
then when i try to add these foreign key when i add migration then update-database it shows this : "The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Punet_Categories_CategoryId". The conflict occurred in database "Jobify", table "dbo.Categories", column 'Id'." i dont have this attribute still in my database
Jimmacle
Jimmacle11mo ago
isn't the ForeignKey attribute supposed to go on the key property, not the navigation property? technically you don't need it at all as it should be figured out by convention
ShuajbM
ShuajbMOP11mo ago
idk im facing this error about 2 days watched some videos still didnt get the result i want
Jimmacle
Jimmacle11mo ago
the official documentation is the best place to look
Jimmacle
Jimmacle11mo ago
i guess that attribute works both ways
ShuajbM
ShuajbMOP11mo ago
so instead using ForeignKey annotation maybe use this [ForeignKey(nameof(Blog))] still it doesnt work how can i alter the table in console
Jimmacle
Jimmacle11mo ago
if you're using migrations, you don't altering the database schema outside of migrations is how you end up with problems like this
ShuajbM
ShuajbMOP11mo ago
so i should store them in another database ? i started in a new database and occured the same problem <<<<The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_Punet_Categories_CategoryId". The conflict occurred in database "Jobify", table "dbo.Categories", column 'Id'>>>>
Want results from more Discord servers?
Add your server