2

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; }
    }
}
Community
  • 1
  • 1
djblois
  • 963
  • 1
  • 17
  • 52
  • possible duplicate of [Entity Framework: table without primary key](http://stackoverflow.com/questions/3996782/entity-framework-table-without-primary-key) – mjw Aug 14 '15 at 13:22
  • You can't change the database, so you definitely don't use migrations. That's what @bubi is trying to tell you. Using code-first is OK, but just disable migrations. – Gert Arnold Aug 14 '15 at 21:23

1 Answers1

1

If the field contains unique data you can mark it as Key in EF and disable all automatic migrations. It should work.

EDIT
You have to disable all the migrations because if EF tries to build missing objects it won't be able.
At the end you will not have the most performant database and also you need to implement referential integrity constraints by yourself.

EDIT2
The problem here is SQL Server because EF is trying to create the relationship from PTEInteractiveCourse.STCIProductInteractiveInfoID to STCIProductInteractiveInfo that does not really have a primary key (EF thinks it has but it is not true). You should try to disable all the automatic migrations. Look here to do so How can I disable migration in Entity Framework 6.0

Community
  • 1
  • 1
bubi
  • 6,414
  • 3
  • 28
  • 45