c# - Keep getting Entitytype has no key defined -
i know question duplicate, unfortunately did not find solution problem. though there lot of posts on matter.
so, topic says, getting exception saying:
entitytype has no key defined
my model looks this:
public class questionmodel { public int id { get; set; } [displayname("question")] public string question { get; set; } public list<string> responses { get; set; } [displayname("number of answers")] public int numberofanswers { get; set; } }
and code in controller:
[httppost] public actionresult registerquestion(questionmodel model) { if (modelstate.isvalid) { using (var context = new databasecontext()) { var question = new questionmodel { numberofanswers = model.numberofanswers, question = model.question, responses = model.responses }; context.question.add(question); context.savechanges(); } } return view("registeredquestion"); }
reading several posts, exception might occur if:
- key missing: (i have key, here called ...
int id {...}
- class name not equal id member name: (this not problem me here, using
int id
) - member not have getter/setter: (but have gettter , setter)
- the id member not public: (all properties public)
confusion in entity framework when multiple ids present. example:
public int oneid { get; set; } public int anotherid { get; set; } not problem in case, have 1 id. in case, did try adding key annotation, id property became:
[key] public int id { get; set; }
but no luck. have taken note reaons error came across, still miss out someting. highly appreciated
i restarted vs13 (which don't often, since it's open). helped. rebuild project, enough. there must have been funny going on. sorry wasting time. wasted own time not restarting vs, never thought of option.
Comments
Post a Comment