Entity Framework - Updates with eSQL
-
I am currently learning EF and I am coding a Windows app as part of the learning process. Given this code fragment: using (var context = new MovieList_EFEntities()) { string esql = "select value m from Movies as m Where m.MovieId = " + Convert.ToInt32(movieId) + ""; var Movies = context.CreateQuery< Movie>(esql); foreach (var movie in Movies) { movie.Name = tbTitle.Text; context.SaveChanges(); } } SaveChanges throws an exception - New transaction not allowed. The Movie entity maps to the equivalent database table composed of 2 fields, the PK (identity) and the Name field. I want to change the Name. Why is the above not permitted? As a workaround, I created a Movie object with the 2 field values assigned. I did not get the desired effect. My db record was not updated, rather, EF inserted a new record. I will figure this out eventually. Was hoping someone can help me out before I do. Thank-you.
-
I am currently learning EF and I am coding a Windows app as part of the learning process. Given this code fragment: using (var context = new MovieList_EFEntities()) { string esql = "select value m from Movies as m Where m.MovieId = " + Convert.ToInt32(movieId) + ""; var Movies = context.CreateQuery< Movie>(esql); foreach (var movie in Movies) { movie.Name = tbTitle.Text; context.SaveChanges(); } } SaveChanges throws an exception - New transaction not allowed. The Movie entity maps to the equivalent database table composed of 2 fields, the PK (identity) and the Name field. I want to change the Name. Why is the above not permitted? As a workaround, I created a Movie object with the 2 field values assigned. I did not get the desired effect. My db record was not updated, rather, EF inserted a new record. I will figure this out eventually. Was hoping someone can help me out before I do. Thank-you.
You should try to get the movie from the context using LINQ.
using var context = new MovieList_EFEntities())
{
int id = Convert.ToInt32(movieId);
Movie mov = context.Movies.SingleOrDefault(m => m.MovieId == id);
if (mov != null)
{
mov.Name = tbTitle.Text;
}
else
{
// Movie was not found.
}
context.SaveChanges(); // Should work now!
}Hope it helps, good luck! :)
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
} -
You should try to get the movie from the context using LINQ.
using var context = new MovieList_EFEntities())
{
int id = Convert.ToInt32(movieId);
Movie mov = context.Movies.SingleOrDefault(m => m.MovieId == id);
if (mov != null)
{
mov.Name = tbTitle.Text;
}
else
{
// Movie was not found.
}
context.SaveChanges(); // Should work now!
}Hope it helps, good luck! :)
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}I figured it out eventually. I agree with you. Better to use LINQ. Thank-you for the reply.
-
I figured it out eventually. I agree with you. Better to use LINQ. Thank-you for the reply.
Glad you found it. No problem! :)
It's an OO world.
public class Naerling : Lazy<Person>{
public void DoWork(){ throw new NotImplementedException(); }
}