C
C#10mo 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
canton710mo 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
ShuajbM10mo 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
Jimmacle10mo ago
why aren't you using migrations? oh, you did
ShuajbM
ShuajbM10mo ago
im using them
Jimmacle
Jimmacle10mo ago
i misread i added this table to mssql correctly to mean you weren't
ShuajbM
ShuajbM10mo 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
Jimmacle10mo 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
ShuajbM10mo ago
idk im facing this error about 2 days watched some videos still didnt get the result i want
Jimmacle
Jimmacle10mo ago
the official documentation is the best place to look
Jimmacle
Jimmacle10mo ago
i guess that attribute works both ways
ShuajbM
ShuajbM10mo 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
Jimmacle10mo 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
ShuajbM10mo 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