Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Entity Framework - Updates with eSQL

Entity Framework - Updates with eSQL

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasehelpquestionlearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Member 8225180
    wrote on last edited by
    #1

    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.

    Sander RosselS 1 Reply Last reply
    0
    • M Member 8225180

      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.

      Sander RosselS Offline
      Sander RosselS Offline
      Sander Rossel
      wrote on last edited by
      #2

      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(); }
      }

      M 1 Reply Last reply
      0
      • Sander RosselS Sander Rossel

        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(); }
        }

        M Offline
        M Offline
        Member 8225180
        wrote on last edited by
        #3

        I figured it out eventually. I agree with you. Better to use LINQ. Thank-you for the reply.

        Sander RosselS 1 Reply Last reply
        0
        • M Member 8225180

          I figured it out eventually. I agree with you. Better to use LINQ. Thank-you for the reply.

          Sander RosselS Offline
          Sander RosselS Offline
          Sander Rossel
          wrote on last edited by
          #4

          Glad you found it. No problem! :)

          It's an OO world.

          public class Naerling : Lazy<Person>{
          public void DoWork(){ throw new NotImplementedException(); }
          }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups