json - C# IgnoreDataMember attribute sets database field to null, how do I overcome this? -
i have built web api usual rest methods.
i have field in database tracking when record inserted (called insertlogtime. works fine.
my classes generated automatically ef6 using database first.
i don't want serialised added [ignoredatamember] attribute field using standard partial metadata classes...
[metadatatype(typeof(membersmetadata))] public partial class members { } public class membersmetadata { [databasegenerated(databasegeneratedoption.none)] public int id { get; set; } [ignoredatamember] public nullable<datetime> insertlogtime { get; set; } }
now when use fiddler put data other fields, insertlogtime field set null.
more importantly, don't want show every field put. customer needs use id , single other field. however, need provide full dataset (without insertlogtime) when doing full post.
{ "id":99, "firstname":"jack spratt", "email":"someone@somewhere.com", "eligible":false }
this works fine when doing post. insert log time ok quick , dirty measure set in controller using datetime.now. know should using default in database i'll later.
ideally customer have insert following when doing put:
{ "id":99, "eligible":true }
but not sure how this. , mentioned, when use full version use post when doing put, sets insertlogtime field null.
here controller
[responsetype(typeof(void))] public ihttpactionresult putmembers(int id, members members) { if (!modelstate.isvalid) return badrequest(modelstate); if (id != members.id) return badrequest(); eligible = members.eligible ?? false; db.entry(members).state = entitystate.modified; try { db.savechanges(); } catch (dbupdateconcurrencyexception) { //notthing of interest here. } return statuscode(httpstatuscode.nocontent); }
any overcoming these problems appreciated.
Comments
Post a Comment