c# - Entity Framework doesn't recognise methods like Add, Remove -
i working entity framework on asp.net mvc3 visual studio 2010 , have problem: after changed database , moved project 1 computer another, no longer recognize methods of dbcontext
such add
, remove
, , not know how reproduce them.
mention code worked on computer. did not nothing relevant changes on database model.
on following example, db
dbcontext
, tmsentities
name of instance.
public class categorycontroller : controller { // // get: /category/ private tmsentities db = new tmsentities(); [httppost] public actionresult addcategory(category model) { bool success = true; string status = string.empty; category item = new category(); item.name = model.name.trim(); item.description = model.description; if (string.isnullorempty(item.name)) { success = false; status += "category name can not empty! <br />"; } var duplicate = db.categories.where(a => a.name == item.name).count(); if (duplicate > 0) { success = false; status += "name exists! <br />"; } if (success) { db.categories.add(item); db.savechanges(); return json(new { success = success, status = status }); } else { return json(new { success = success, status = status }); } } }
and after, error such as:
'system.data.objects.objectset' not contain definition 'add' , no extension method 'add' accepting first argument of type
'system.data.objects.objectset' found (are missing using directive or assembly reference?)
instead of using .add() use .addobject() , instead of using .remove use .deleteobject()... beacause
the reason worked because declared context using object content , no dbcontext. dbcontext wrapper on objectcontext. easier use. somehow exercise has mixed 2 up. when creating model , content in place, if teh right project type used , lastest nuget packages in, should t4 (template generates code) uses dbcontext. code above accessing context derived dbcontext. suggest take close @ context def , regenerate it. highly recommend move dbcontext
Comments
Post a Comment