0

I get this error after trying to update database from package manager console. Here is some code. Class User is already in database and i am trying to migrate class Poslovnica to server.

There are no primary or candidate keys in the referenced table 'User' that match the referencing column list in the foreign key 'FK_Poslovnica_User_UserID'. Could not create constraint or index. See previous errors.

 public class Poslovnica { public int PoslovnicaID { get; set; } [Required] public string Naziv { get; set; } public string Adresa { get; set; } public User User { get; set; } public int? UserID { get; set; } } 

I already added some classes that have foreign key UserID int them and didnt have any problems

 public class User { public int userID { get; set; } [Required(AllowEmptyStrings = false)] [MaxLength(64)] public string ime { get; set; } } 

model builder from migration

 modelBuilder.Entity("ClassLibrary1.Models.Poslovnica", b => { b.Property<int>("PoslovnicaID") .ValueGeneratedOnAdd() .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); b.Property<string>("Adresa"); b.Property<string>("Naziv") .IsRequired(); b.Property<int?>("UserID"); b.HasKey("PoslovnicaID"); b.HasIndex("UserID"); b.ToTable("Poslovnica"); }); 

migration builder

 migrationBuilder.CreateTable( name: "Poslovnica", columns: table => new { PoslovnicaID = table.Column<int>(nullable: false) .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), Naziv = table.Column<string>(nullable: false), Adresa = table.Column<string>(nullable: true), UserID = table.Column<int>(nullable: true) }, constraints: table => { table.PrimaryKey("PK_Poslovnica", x => x.PoslovnicaID); table.ForeignKey( name: "FK_Poslovnica_User_UserID", column: x => x.UserID, principalTable: "User", principalColumn: "userID", onDelete: ReferentialAction.Restrict); }); 

I appreciate any help!

1 Answer 1

1

You have a 1 to 1 relationship between Poslovnica and User but the foreign key in Poslovnica doesn't match the primary key in User.

Rename the property userID in User to UserID to match the foreign key since you're going by the convention so they must match in order for ef core to pick up on the relationship. and re-update

Edit

Probably the UserID is not recognized as a primary key in the table Users Add the following attributes to the UserID :

 public class User { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int UserID { get; set; } } 

And update your database.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for response! I did it and i still get the same error message
I tried it, still same error. I dont think there is problem with user table because i had allready connected it with few other tables with same connection
@sasko You do know that you have to remove the created migration and create a new one after the change right? Then you update the db
yes, i have some old migrations that i dont want to loose but i deleted all migrations that have "Poslovnica" table in them
anyways thanks for helping me, i created new database and can migrate all classes in it without errors so now i know that there is somewhere conflict in migrations
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.