0

A developer has left our company, and has left us saddled with a bunch of C# code, using Entity Framework. I'm not a C# expert, and have never used Entity Framework. So - drinking from the end of a firehose.

I have a problem with one piece of code that isn't saving changes. At least not in a production or test environment from a web server. Running the code in debug will save the data each and every time. To the same test database that doesn't work when run from the web site (other data entry forms ARE saving to the database, just this one seems to be a problem).

I've researched, and a lot of suggestions seem to stem from Entity Framework not realizing the data is "actually" changed. So, I added a line of code to try to set the EntityState to Added in order to force it - still not joy.

public partial class xDBContext : DbContext
{
     public xDBContext() : base("PromiseConnectionString")
     {
     }

}    
...    

public partial class xPromise_AllPrograms
{
    [Key]
    public int PrimID { get; set; }

    public int FormId { get; set; }

    [StringLength(100)]
    public string Degree_Text { get; set; }
}

...

public ActionResult AddDegree(int formId, string degree)
{
   using (xDBContext context = new xDBContext())
   {
      var program = new x_AllPrograms()
      {
          FormId = formId,
          Degree_Text = degree
      };

      context.Promise_AllPrograms.Add(program);
      context.Entry(program).State = System.Data.Entity.EntityState.Added;
      context.SaveChanges();
   }

   return PartialView("_ViewPrograms", GetPrograms(formId));
}

Obviously, any Degree_Text added should save into the resulting table, and will be re-displayed via GetPrograms.

The data simply isn't saved - UNLESS I run it via debug on my local desktop.

Ideas?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
TDU
  • 117
  • 1
  • 11
  • 1
    I'm grasping at straws here, but without a better understanding of your environment........is it possible he defined multiple settings, one per build configuration (debug v. release) using a technique like those mentioned in the answers to this question:? https://stackoverflow.com/questions/3090311/different-application-settings-depending-on-configuration-mode – Richard II Oct 30 '19 at 19:16
  • I'd recommend that you set up a remote debugger so you can see what's going on, step by step. – devlin carnate Oct 30 '19 at 19:29
  • Is there some "hidden" exception handler, which would swallow possible exception thrown by this code? – Fabio Oct 30 '19 at 19:31
  • I don't know if it's a typo but should `var program = new xPromise_AllPrograms ...`? Still something like that would break in dev. I'd also be leaning towards an exception being eaten. It's highly unusual that the same EF code running locally vs. a web server would yield different behavior. One thing to check would be database permissions if the connection string is using windows auth for instance. Running locally would be connecting via your credentials while on the server, whatever the AppPool account is set as. The table in question could possibly need an explicit grant applied?? – Steve Py Oct 30 '19 at 21:28
  • Missing migrations could be another guess – Fabio Oct 31 '19 at 05:21
  • Thanks all for your replies. Wending through various solutions. – TDU Oct 31 '19 at 14:23

0 Answers0