EF Reverse Code First Tool is generating a composite key
public vw_ResourcesMap()
{
// Primary Key
this.HasKey(t => new { t.Refno, t.Obsolete }); // Why???
// Properties
this.Property(t => t.Refno)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(t => t.Name)
.HasMaxLength(50);
this.Property(t => t.LocnID)
.IsFixedLength()
.HasMaxLength(2);
this.Property(t => t.Created_by)
.HasMaxLength(50);
// Table & Column Mappings
this.ToTable("vw_Resources");
this.Property(t => t.Refno).HasColumnName("Refno");
this.Property(t => t.Name).HasColumnName("Name");
this.Property(t => t.Head).HasColumnName("Head");
this.Property(t => t.LocnID).HasColumnName("LocnID");
this.Property(t => t.Obsolete).HasColumnName("Obsolete");
this.Property(t => t.Created_by).HasColumnName("Created_by");
this.Property(t => t.Version).HasColumnName("Version");
this.Property(t => t.Rank).HasColumnName("Rank");
}
}
I had to change the Primary Key declaration to this.HasKey(t => t.Refno) to map the relationship otherwise I would get the exception below.
The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.
Maybe it’s a bug — IDK
All is working, but would like to increase understanding and knowledge.
Thanks!
Database view doesn’t have primary key so EF tries to infer the key by using all non nullable columns which don’t use binary data types.