c# - Trying to add a controller, unable to retrieve metadata, no primary key define, even though it looks like there is -
all found far said define primary key [key] , must named id or yourclassnameid. far can see have correct format i'm still getting error:
![unable retrieve metadata 'dartpro.models.members'. 1 or more validation errors detected during model generation:
dartpro.models.playerscores: :entitytype 'playerscores' has no key defined. define key entitytype. players_scores: entitytype: entityset 'players_scores' based on tpye 'playerscores' has no keys defined.]1
when creating controller this:
![model class: (dartpro.models)
data context class: dartconnection(dartpro.models)]2
here's code:
using system; using system.collections.generic; using system.linq; using system.web; using system.data.entity; using system.componentmodel.dataannotations.schema; using system.componentmodel.dataannotations; namespace dartpro.models { public class dartconnection : dbcontext { public dbset<members> member_details { get; set;} public dbset<playerscores> players_scores { get; set;} } [table("menmbersprofile")] public class members { [key] public int membersid { get; set; } public string forename { get; set; } public string surname { get; set; } public string nickname { get; set; } public string contactnumber { get; set; } public string address { get; set; } } [table("playersgamedetials")] public class playerscores { [key] public int playerscoresid { get; set; } public int higestscore { get; set; } public double threedartaverage { get; set; } public double onedartaverage { get; set; } public int dartsthrown { get; set; } public int membersid { get; set; } } }
you have typos in code
[table("menmbersprofile")] should membersprofile? menmbers
[table("playersgamedetials")] typo? details not detials.
i suggest manually create database tables matching code first classes , make sure have no typos, , set primary keys on tables in database match [key] attribute in classes.
i have following override in model, control pluralisation of table names.
also if want control target of dbconext can add own contructor
public class dartconnection : dbcontext { public dartconnection() { // override connection string this.database.connection.connectionstring = configurationmanager.connectionstrings["myappsettingdb"].connectionstring; } protected override void onmodelcreating(dbmodelbuilder modelbuilder) { modelbuilder.conventions.remove<pluralizingtablenameconvention>(); } }
Comments
Post a Comment