c# - EntityType 'Worker' has no key defined. Define the key for this EntityType -
my code in solution.models.worker
:
using system; using system.collections.generic; using system.linq; using system.web; using system.data.entity; namespace solution.models { public class worker { public int asmenskodas { get; set; } public string vardas { get; set; } public string pavarde { get; set; } public datetime gimimodata { get; set; } public string adresas { get; set; } public bool aktyvumopozymis { get; set; } } public class workerdbcontext : dbcontext { public dbset<worker> worker { get; set; } } }
i changed web.config
file adding
<connectionstrings> <add name="workerdbcontext" connectionstring="data source=(localdb)\mssqllocaldb;attachdbfilename=|datadirectory|\workers.mdf;integrated security=true" providername="system.data.sqlclient"/> </connectionstrings>
and when try add controller "mvc 5 controller views, using entity framework" configuration
- model class:
worker
(solution.models) - data context class:
workerdbcontext
(solution.models) - controller name:
workerscontroller
i error message saying
there error running selected code generator:
'unable retrieve metadata 'solution.models.worker.' 1 or more validation errors ware detected during model generation:
solution.models.worker::entitytype 'worker' has no key defined.
define key entitytype.
workers:entitytype:entityset 'workers' based on type 'worker' has no keys defined.
any suggestions or i'm doing wrong?
one of model's properties should key. place [key]
annotation on top of 1 of properties. this:
public class worker { [key] public int asmenskodas { get; set; } public string vardas { get; set; } public string pavarde { get; set; } public datetime gimimodata { get; set; } public string adresas { get; set; } public bool aktyvumopozymis { get; set; } }
just don't forget add line using
directive:
using system.componentmodel.dataannotations;
also if have property in model
name id
, don't need add [key]
attribute it.
Comments
Post a Comment