I have an existing table (that is in SQL server) that I need to use in my solution that has no primary key. It has a unique field that it uses as the key but was never set up as the PK. I asked my boss and he said we cannot change it. How do I use that field as a primary key, even though it is not labeled as one?
I am using code first migrations.
I found this when searching? but all the answers refer to a view, not a table.
Entity Framework: table without primary key
here is my model:
public partial class STCIProductInteractiveInfo
    {
        public Guid STCIProductInteractiveInfoID { get; set; }
        public Guid MarketingTextID { get; set; }
        public DateTime ModifyDate { get; set; }
        public int ModifyUserID { get; set; }
        public string STCICourseID { get; set; }
        public string STCICourseName { get; set; }
        public byte STCIProductEmailHTML { get; set; }
        public string STCIProductEmailHTMLDateType { get; set; }
        public int STCIProductEmailHTMLFileLength { get; set; }
        public byte STCIProductEmailText { get; set; }
        public string STCIProductEmailTextDataType { get; set; }
        public string STCIProductEmailTextFileLength { get; set; }
        public string STCITrackingClassCode { get; set; }
        public string STCITrackingClassName { get; set; }
        public int u_product_id { get; set; }
    }
After marking STCIProductInteractiveInfoID like this:
[Key]
public Guid STCIProductInteractiveInfoID { get; set; }
I am getting this error:
There are no primary or candidate keys in the referenced table 'dbo.STCIProductInteractiveInfo' that match the referencing column list in the foreign key 'FK_dbo.PTEInteractiveCourses_dbo.STCIProductInteractiveInfo_STCIProductInteractiveInfoID'. Could not create constraint. See previous errors.
I have put this code in my DataContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Add Primary key for STCIProductInteractiveInfo
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<STCIProductInteractiveInfo>().HasKey(x => new { x.STCIProductInteractiveInfoID });
        }
and as requested here is my PTEInteractiveCourse model:
namespace PTEManager.Domain
{
    public class PTEInteractiveCourse
    {
        [Key]
        public Guid PTEInteractiveCourseId { get; set; }
        [ScaffoldColumn(false)]
        [Required]
        public DateTime ModifyDate { get; set; }
        [ScaffoldColumn(false)]
        [Display(Name = "")]
        [Required]
        public int Status { get; set; }
        [Display(Name = "STCI Course")]
        [ForeignKey("STCICourseName")]
        [Required]
        public Guid STCIProductInteractiveInfoID { get; set; }
        public virtual STCIProductInteractiveInfo STCICourseName { get; set; }
    }
}
 
     
    