Note that there are some explanatory texts on larger screens.

plurals
  1. POEF5 The changes to the database were committed successfully ... Unable to set field/property
    text
    copied!<p>I have been searching for days now trying to figure this one out. It saves my records correctly but throws the following error:</p> <p><code>The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: Unable to set field/property Actors on entity type BOR.DataModel.StagComplaint. See InnerException for details.</code></p> <p>I am using Code First and EF 5 in a C# Web Forms solution with a supporting WCF Service. Here are my POCO classes:</p> <pre><code>public partial class StagComplaint : ComplaintBase { public IList&lt;StagParcel&gt; Parcels { get; set; } public IList&lt;StagActor&gt; Actors { get; set; } public IList&lt;StagRectification&gt; Rectifications { get; set; } public ComplaintType ComplaintType { get; set; } public int ComplaintTypeID { get; set; } public StagComplaint() { this.Parcels = new List&lt;StagParcel&gt;(); this.Actors = new List&lt;StagActor&gt;(); this.Rectifications = new List&lt;StagRectification&gt;(); } } public class ComplaintBase : BORBase { public string Number { get; set; } public int ParentID { get; set; } public int TaxYear { get; set; } public string Category { get; set; } public double BuildingValue { get; set; } public double LandValue { get; set; } public double OwnerOpinion { get; set; } public string Notes { get; set; } } public class BORBase { [Required] public DateTime CreationDate { get; set; } public int ID { get; set; } [MaxLength(25)] [Required] public string UserIdentification { get; set; } } public partial class StagParcel : ParcelBase { public virtual StagActor Owner { get; set; } [ForeignKey("Owner")] public int OwnerID { get; set; } public StagAddress Address { get; set; } [IgnoreDataMember] public virtual StagComplaint Complaint { get; set; } public int ComplaintID { get; set; } public StagParcel() { this.Address = new StagAddress(); } } public class ParcelBase : BORBase { public string Number { get; set; } public double BuildingValue { get; set; } public double LandValue { get; set; } public double OwnerOpinion { get; set; } public string LandUseCode { get; set; } public string NeighborhoodCode { get; set; } public string TaxDistrict { get; set; } public string SchoolDistrict { get; set; } public int SchoolBoardID { get; set; } } public partial class StagActor : ActorBase { public StagAddress Address { get; set; } public virtual IList&lt;StagEmail&gt; Emails { get; set; } public virtual IList&lt;StagPhone&gt; Phones { get; set; } [IgnoreDataMember] public virtual StagComplaint Complaint { get; set; } public int ComplaintID { get; set; } public virtual Role Role { get; set; } public int RoleID { get; set; } public StagActor() { this.Emails = new List&lt;StagEmail&gt;(); this.Phones = new List&lt;StagPhone&gt;(); this.Address = new StagAddress(); } } public class ActorBase : BORBase { public string Name { get; set; } } public class StagRectification : BORBase { public bool Active { get; set; } public string Notes { get; set; } public virtual RectificationType RectificationType { get; set; } public int RectificationTypeID { get; set; } [IgnoreDataMember] public virtual StagComplaint Complaint { get; set; } public int ComplaintID { get; set; } } </code></pre> <p>This is the client side code I am using to create the Complaint:</p> <pre><code>public int AddParcelsToStagingComplaint(List&lt;string&gt; parcelIDs, string userID) { StagComplaint comp = new StagComplaint(); int Result = 0; using (BORServiceClient db = new BORServiceClient()) { comp = new StagComplaint() { BuildingValue = 111222, Category = "*", LandValue = 222333, Number = "*", TaxYear = DateTime.Now.Year, ComplaintTypeID = 1, UserIdentification = userID, CreationDate = DateTime.Now, }; StagAddress ca = new StagAddress() { Line1 = "670 Harvard Blvd", City = "Cleveland", State = "OH", ZipCode = "44113", }; List&lt;StagPhone&gt; ps = new List&lt;StagPhone&gt;(); ps.Add(new StagPhone() { Number = "5556664646", Type = PhoneTypes.Home, UserIdentification = userID, CreationDate = DateTime.Now, }); comp.Actors.Add( new StagActor() { Name = "Joe Schmoe", Address = ca, Phones = ps, RoleID = 1, UserIdentification = userID, CreationDate = DateTime.Now, } ); StagAddress aa = new StagAddress() { City = wp.Address.City, Line1 = wp.Address.Line1, Line2 = wp.Address.Line2, State = wp.Address.State, ZipCode = wp.Address.ZipCode, }; ps = new List&lt;StagPhone&gt;(); ps.Add(new StagPhone() { Number = "4448887878", Type = PhoneTypes.Work, UserIdentification = userID, CreationDate = DateTime.Now, }); StagParcel p = new StagParcel() { Address = new StagAddress() { Line1 = "4 Oxford Drive", City = "Hudson", State = "OH", ZipCode = "44236" }, BuildingValue = wp.BuildingValue, LandUseCode = wp.LandUseCode, LandValue = wp.LandValue, NeighborhoodCode = wp.NeighborhoodCode, Number = wp.Number, Owner = new StagActor() { Name = "Owner Person", Address = aa, RoleID = 2, Phones = ps, UserIdentification = userID, CreationDate = DateTime.Now, }, OwnerOpinion = wp.OwnerOpinion, SchoolBoardID = wp.SchoolBoardID, SchoolDistrict = wp.SchoolDistrict, TaxDistrict = wp.TaxDistrict, UserIdentification = userID, CreationDate = DateTime.Now, }; comp.Parcels.Add(p); ServiceResponse&lt;int&gt; saved = db.AddComplaint((ComplaintBase)comp, Contexts.Staging, userID); if (saved.WasSuccessful) Result = saved.Result; } // using the database return Result; } // AddParcelsToStagingComplaint - Method </code></pre> <p>Here is the WCF method that gets called:</p> <pre><code>using (StagComplaintRepo cr = new StagComplaintRepo()) { cr.Add((StagComplaint)complaint, userID); if (cr.Save()) { Result.Result = complaint.ID; Result.WasSuccessful = true; } else { Result.AddException(string.Format("Unable to create a new Complaint in the {0} context.", context)); } // if the save was successful } // using the Complaint Repository </code></pre> <p>And here is the BaseRepository that has the Save and Add methods:</p> <pre><code>public abstract class BaseRepository&lt;T&gt; : IDisposable, IRepository&lt;T&gt; where T : class { public virtual bool Save(bool detectChanges = false) { if (detectChanges == true) this.Entities.ChangeTracker.DetectChanges(); return (this.Entities.SaveChanges() &gt; 0); } public virtual void Add(T entity, string userID) { this.Entities.Set&lt;T&gt;().Add(entity); } ... } </code></pre> <p>It fails on the above this.Entities.SaveChanges() call with the error mentioned at the top of this post. There is no extra inner exception. If I only fill in the Complaint properties that are required and are part of that object, it works. But once I add a Parcel with an Actor it fails.</p> <p>I assume it is something simple, perhaps a switch needs to be turned on or off. But similar errors all seem to reference AcceptChanges and that is not the issue here. At least based on the error message. Any help would be appreciated.</p> <p><strong>EDIT</strong> Here is the full stack trace:</p> <pre><code>at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() at System.Data.Entity.Internal.LazyInternalContext.SaveChanges() at System.Data.Entity.DbContext.SaveChanges() at BOR.WebService.Repositories.BaseRepository`1.Save(Boolean detectChanges) in d:\DevProjects\BOR\WebService\Main\Source\WebServiceSolution\WcfServiceProject\Repositories\BaseRepository.cs:line 22 at BOR.WebService.BORService.AddComplaint(ComplaintBase complaint, Contexts context, String userID) in d:\DevProjects\BOR\WebService\Main\Source\WebServiceSolution\WcfServiceProject\BORService.svc.cs:line 65 </code></pre> <p>Line 22 is:</p> <pre><code>return (this.Entities.SaveChanges() &gt; 0); </code></pre> <p>Line 65 is:</p> <pre><code>if (cr.Save()) { </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload