C
C#6mo ago
shuajb;

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
canton76mo 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?
shuajb;
shuajb;6mo 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
Jimmacle6mo ago
why aren't you using migrations? oh, you did
shuajb;
shuajb;6mo ago
im using them
Jimmacle
Jimmacle6mo ago
i misread i added this table to mssql correctly to mean you weren't
shuajb;
shuajb;6mo 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
Jimmacle6mo 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
shuajb;
shuajb;6mo ago
idk im facing this error about 2 days watched some videos still didnt get the result i want
Jimmacle
Jimmacle6mo ago
the official documentation is the best place to look
Jimmacle
Jimmacle6mo ago
i guess that attribute works both ways
shuajb;
shuajb;6mo 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
Jimmacle6mo 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
shuajb;
shuajb;5mo 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
More Posts
C# Version for development, build, and deploymentI'm having a little trouble understanding a couple things, but mainly: > What language version am aHelp of someone who knows c sharp with .cs files in visual studio to compile my projectANY HELP WILL BE RECEIVED AND WILL NEED TO BE PROFESIONAL AT VISUAL OR PRGRAM LANGUAGES AS RUST,C#,CI need help with a loop in a menuI need a menu that loops until you choose to quit and that writes something if you give a wrong respblazor implementing 404 pageblazor web app .net 8 not triggering <NotFound> tag how to fix it ? ```csharp @using BlazorApp33.ComHow run ms test sequentially not in parallelHello, I want to run ms test in queue, not in parallel. On VS and on my azur pipeline. Is it possibHelp, errors are not showing in VScode.Im new to coding and fo lowing a tutorial on yt but there video show red lines under errors and thatHow to fix " The ConnectionString property has not been initialized." in .Net apiHello world! I'm developing a .Net api using Docker containers, but at the moment to ask a GET requAny WinForms gods here? Having issues with the Editor AttributeI am encountering an issue with a software project restructuring. Initially, my solution combined seUnable to set property 'onsubmit' on object of typeHi everyone! I created a simple login page but it throws me an exception: ``` InvalidOperationExcepScaffold Not Working because `dotnet-aspnet-codegenerator.exe` uses old C# versionI need help with scaffolding identity my C# project, I get this error whenever I scaffold `` D:\Pr