Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework Context.SaveChanges not working at all

I'm having problems with this code. I´m able to connect to an mdf example database archive and generate the entity model. Althought I´m able to query the context model and retrieve information from the DB, when I try to update, delete or insert anything in the context and translate the changes to the DB Context.SaveChanges is not working. There is no Exception, the Entity model is updated properly, but the DB does not have the change. Thanks in regard

public void addCourse(int courseId, int deptId, string courseTitle)
{    
SchoolContexto = new SchoolEntities();            

Course mycourse= new Course();
mycourse.CourseID = courseId;
mycourse.Credits = 10;
mycourse.DepartmentID = deptId;
mycourse.Title = courseTitle;
SchoolContexto.Courses.Add(mycourse);            

SchoolContexto.SaveChanges();
SchoolContexto.Dispose();
}
like image 466
user3471933 Avatar asked Mar 28 '14 09:03

user3471933


People also ask

How do I use SaveChanges in Entity Framework?

In Entity Framework, the SaveChanges() method internally creates a transaction and wraps all INSERT, UPDATE and DELETE operations under it. Multiple SaveChanges() calls, create separate transactions, perform CRUD operations and then commit each transaction. The following example demonstrates this.

What does the DbContext SaveChanges () method return?

Returns. The number of state entries written to the underlying database. This can include state entries for entities and/or relationships.

What SaveChanges return value in Entity Framework?

SaveChanges() always returns 0 – Entity Framework According to EF documentation, SaveChanges() should return a number of affected rows.


3 Answers

Make property of .mdf file in your solution as Copy to output Directory: "Copy only if newer"

Otherwise your db file will overwrite every time it runs

like image 122
Vishal Avatar answered Oct 22 '22 01:10

Vishal


Another way to add a new entity to the context is to change its state to Added. Have you tried this

using (var entities = new SchoolEntities()) 
{ 
    Course mycourse= new Course();
    mycourse.CourseID = courseId;
    mycourse.Credits = 10;
    mycourse.DepartmentID = deptId;
    mycourse.Title = courseTitle;
    context.Entry(mycourse).State = EntityState.Added;
    entities.SaveChanges(); 
}
like image 43
Suresh Kumar Veluswamy Avatar answered Oct 22 '22 00:10

Suresh Kumar Veluswamy


i suggest you to use this code :

public void addCourse(int courseId, int deptId, string courseTitle)
{    
     SchoolEntities entities = new SchoolEntities();            

     Course mycourse= new Course();
     mycourse.CourseID = courseId;
     mycourse.Credits = 10;
     mycourse.DepartmentID = deptId;
     mycourse.Title = courseTitle;
     entities.Courses.Add(mycourse);            

     entities.SaveChanges();

 }

if this is not working i suggest you to check your app.config file :)

like image 2
Reda Avatar answered Oct 21 '22 23:10

Reda