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!