Hi,
I need to create a view that uses an old Customer table, which has a primary key that doesn't end in "ID". The class and context are:
public partial class CUS_Customer { public int CUS_THIS_RECORD { get; set; } public string CUS_Name { get; set; } //We just need this basic subset of the QTEC.CUS_Customer table initially }
and
public class QTECContext : DbContext { public virtual DbSet<CUS_Customer> CUS_Customer { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Not for public consumption"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<CUS_Customer>(entity => { entity.ToTable("CUS_Customer", "dbo"); entity.Property(e => e.CUS_Name).HasMaxLength(60); }); } }
When I use LINQ to access this data:
public SelectList GetCustomers() { QTECContext db = new QTECContext(); var Customers = from d in db.CUS_Customer orderby d.CUS_Name select new { d.CUS_THIS_RECORD, d.CUS_Name }; return new SelectList(Customers, "CUS_THIS_RECORD", "CUS_Name"); }
I get the following error:
InvalidOperationException: The entity type 'CUS_Customer' requires a primary key to be defined....
The guidelines https://docs.microsoft.com/en-us/ef/core/modeling/keys imply that only properties/columns that end in "ID" can be primary keys. Is this there a way of forcing the DBContext to recognise a primary key of any name?
I'm using Core version 1.1...and please no comments about instantiating DBContexts the way I have. I have very limited time to get a demo done and I know this way works.
Kind regards, Paul