Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy my Entity Framework Code First Entities are being detached?
    text
    copied!<p>I am using the code first (EF5) approach to create my domain classes. I perform a query to pull a Parent record w/ a collection of child's. I need to edit my parent and perform CRUD operations on childs.</p> <p>The problem that when saving my objects (parent/child) the entities state is Detached thus without putting an extra logic I don't now if the objects where modified, deleted or inserted.</p> <p>How can I enable EF to keep traking of my entities or not detaching my entities?</p> <p>Here is my code:</p> <pre><code>public class myDbContext : DbContext { #region ConnectionStrings public myDbContext() : base(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["ActiveDB_my"]].ToString()) { this.Configuration.LazyLoadingEnabled = true; Database.SetInitializer&lt;myDbContext&gt;(null); //this.Configuration.ProxyCreationEnabled = true; //this.Configuration.AutoDetectChangesEnabled = true; } #endregion #region EntityFramework protected override void OnModelCreating(DbModelBuilder modelBuilder) { //Entity Framework Configurations modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); //Load Entity Configurations dbomyAnotations.Load(ref modelBuilder); base.OnModelCreating(modelBuilder); } #endregion #region EntityRegistrations //Client public DbSet&lt;ClientLocation&gt; ClientLocations { get; set; } //Templates public DbSet&lt;Template&gt; Templates { get; set; } #endregion } namespace App.myTrak.BL { public class Templates { public static ClientLocation getTemplatesForClientLocation(int clientLocationId) { ClientLocation entity = null; using (myDbContext db = new myDbContext()) { entity = db.ClientLocations .Where(c =&gt; c.ClientLocationId == clientLocationId) .Include(t =&gt; t.Templates) .FirstOrDefault(); } return entity; } public static void saveForTemplates(ClientLocation clientLocations) { int Id = clientLocations.ClientLocationId; var db = new myDbContext(); //Client Location db.ClientLocations.Attach(clientLocations); db.Entry(clientLocations).State = System.Data.EntityState.Unchanged; db.Entry(clientLocations).Property(x =&gt; x.ShowPresoldProducts).IsModified = true; //App Templates foreach (var template in clientLocations.Templates) { db.Templates.Attach(template); db.Entry(template).State = System.Data.EntityState.Unchanged; if (template.DeleteDate != null) { db.Entry(template).Property(x =&gt; x.DeleteUserId).IsModified = true; db.Entry(template).Property(x =&gt; x.DeleteDate).IsModified = true; template.DeleteDate = Shared.Common.DateTimeUTC(); } else if (template.TemplateId == 0) db.Entry(template).State = System.Data.EntityState.Added; else { //Modified db.Entry(template).Property(x =&gt; x.TemplateName).IsModified = true; db.Entry(template).Property(x =&gt; x.Description).IsModified = true; db.Entry(template).Property(x =&gt; x.AppTypeId).IsModified = true; db.Entry(template).Property(x =&gt; x.ModifyUserId).IsModified = true; db.Entry(template).Property(x =&gt; x.ModifyDate).IsModified = true; template.ModifyDate = Shared.Common.DateTimeUTC(); template.ModifyUserId = CurrentUserId; } } db.SaveChanges(); db.Database.Connection.Close(); } } } </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