Reputation: 29
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?
Upvotes: 0
Views: 413
Reputation: 29
And that's exactly what it came down to, a bit of ignorance and a whole lot of stupid on my part. On my context class I was inheriting from DbContext() instead of the auto generated partial entities class.
so my context looked like
public class OzoneLiveLite : DbContext
{
public DbSet<CORP_MASTER> CorpMaster { get; set; }
public DbSet<JCS_FINYEAR> JcsFinyear { get; set; }
public DbSet<JCS_JOB> JcsJob { get; set; }
public DbSet<JCS_JOB__JCS_JOB_SUBJOB> JcsJobJcsJobSubjob { get; set; }
public DbSet<JCS_FINYEAR__JCFY_PERIOD> JcsFinyearJcfyPeriod { get; set; }
}
Instead of
public class OzoneLiveLite : Ozone_Live_LiteEntities
{
public DbSet<CORP_MASTER> CorpMaster { get; set; }
public DbSet<JCS_FINYEAR> JcsFinyear { get; set; }
public DbSet<JCS_JOB> JcsJob { get; set; }
public DbSet<JCS_JOB__JCS_JOB_SUBJOB> JcsJobJcsJobSubjob { get; set; }
public DbSet<JCS_FINYEAR__JCFY_PERIOD> JcsFinyearJcfyPeriod { get; set; }
}
To be honest I'm still not sure why it allowed me to build and only failed at runtime but it looks like my issue is solved for the moment.
Thank you all for the assist
Upvotes: 1
Reputation: 525
Cause
If you don't have standard primary key names and non integer primary keys the ef may not identify your primary keys.
You can make the column as primary key by the below steps
Property section of the column
Upvotes: 0