I'm stumped with this. I'm using Entity Framework (EF 6.1.3) and generated a database-first model (this is the weird part because most of the googling I've done has results for code first).
When I'm trying to query the database, I get the following error:
One or more validation errors were detected during model generation:TCCIntegration.DataAccess.Ozone.Context.CORP_MASTER: : EntityType 'CORP_MASTER' has no key defined. Define the key for this EntityType" "Define the key for this EntityType. CorpMaster: EntityType: EntitySet 'CorpMaster' is based on type 'CORP_MASTER' that has no keys defined.
But I can see in the actual SQL database that there is a primary key defined and also in the .EDMX, I can see that the primary key is included in the model.
Plus the problem with it being database-first, I can't add any data annotations to the models because they are automatically generated. Is there something obvious I am missing? (I have tried removing the tables from the .EDMX and re-adding them and also refreshing the .EDMX)
Here's the SQL DDL (Thanks STLDeveloper)
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[CORP_MASTER](
[CORP_MASTER__ID] [varchar](255) NOT NULL,
[CORP_MAST_ACQUIRED] [varchar](2) NULL,
[CORP_MAST_YEAR_START] [int] NULL,CONSTRAINT [PK_CORP_MASTER] PRIMARY KEY
CLUSTERED
(
[CORP_MASTER__ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
EDIT [CodeName47] The EDMX does show that they should be keys
EDIT
I've created an empty console app and tried this again with the same result. I have a class that inherits from DbContext
namespace ConsoleApp2
{
public class OzoneLiveLite : DbContext
{
public DbSet<CORP_MASTER> CorpMaster { get; set; }
}
}
A provider class
namespace ConsoleApp2
{
public class OzoneLiveLiteProvider
{
private readonly OzoneLiveLite _ozoContext;
public OzoneLiveLiteProvider() : this(new OzoneLiveLite())
{
}
public OzoneLiveLiteProvider(OzoneLiveLite ozoneLiveLiteProvider)
{
_ozoContext = ozoneLiveLiteProvider;
}
public void GetProjectFinancials()
{
var corpMaster = _ozoContext.CorpMaster;
var ozoneFinancials = from corpMast in corpMaster select corpMast.CORP_MASTER__ID;
}
public void Dispose()
{
_ozoContext?.Dispose();
}
}
}
and finally the Main class
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var ozoneProvider = new OzoneLiveLiteProvider();
ozoneProvider.GetProjectFinancials();
Console.ReadKey();
}
}
}
Is there possibly something wrong in the way I'm implementing the context?


CORP_MASTERtable, including the definition of the primary key.CREATE TABLEstatement that was used to create theCORP_MASTERtable.CORP_MASTERtable to your question, rather than posting it in a comment.