I'm coding a web by MVC4. I use Entity Framework and Visual Studio Express 2012 for web. When I code Insert for Student, have a problem:
demoMVCEntities db = new demoMVCEntities();
n412_Student _newStudent = new n412_Student();
_newStudent.Student_FirstName = _fname;
_newStudent.Student_LastName = _lname;
_newStudent.Student_Phone = _phone;
_newStudent.Student_Email = _email;
_newStudent.Student_Province = _provinces;
db.n412_Student.AddObject(_newStudent);
AddObject is red underlined. Message Error:
"'System.Data.Entity.DBSet' does not contain a definition for 'AddObject' and no extension method 'AddObject' accepting a first argument of type...."
Please help me.. thanks..!
AddObject
is a method of the ObjectContext
but in newer versions of Entity Framework you are typically working against a DbContext
.This new class uses DbSet<TEntity>
instead of old ObjectSet<TEntity>
. New set class has method Add
. So, as the exception said, you are working with a DBSet
object ( n412_Student
), that have an Add
method. You need to do this:
db.n412_Student.Add(_newStudent);
If you want to see the difference between AddObject
and Add
methods, you can check this post
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.