0

我正在使用代码优先 (EF5) 方法来创建我的域类。我执行查询以提取带有孩子集合的父记录。我需要编辑我的父母并对孩子执行 CRUD 操作。

保存我的对象(父/子)时实体状态是分离的问题,因此没有添加额外的逻辑,如果对象被修改、删除或插入,我现在不这样做。

如何让 EF 继续跟踪我的实体或不分离我的实体?

这是我的代码:

public class myDbContext : DbContext
{
    #region ConnectionStrings

    public myDbContext()
        : base(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["ActiveDB_my"]].ToString())
    {
        this.Configuration.LazyLoadingEnabled = true;
        Database.SetInitializer<myDbContext>(null);

        //this.Configuration.ProxyCreationEnabled = true;
        //this.Configuration.AutoDetectChangesEnabled = true;
    }

    #endregion

    #region EntityFramework

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //Entity Framework Configurations

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        //Load Entity Configurations
        dbomyAnotations.Load(ref modelBuilder);

        base.OnModelCreating(modelBuilder);
    }

    #endregion

    #region EntityRegistrations

    //Client
    public DbSet<ClientLocation> ClientLocations { get; set; }

    //Templates
    public DbSet<Template> 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 => c.ClientLocationId == clientLocationId)
                .Include(t => 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 => 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 => x.DeleteUserId).IsModified = true;
                    db.Entry(template).Property(x => 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 => x.TemplateName).IsModified = true;
                    db.Entry(template).Property(x => x.Description).IsModified = true;
                    db.Entry(template).Property(x => x.AppTypeId).IsModified = true;
                    db.Entry(template).Property(x => x.ModifyUserId).IsModified = true;
                    db.Entry(template).Property(x => x.ModifyDate).IsModified = true;

                    template.ModifyDate = Shared.Common.DateTimeUTC();
                    template.ModifyUserId = CurrentUserId;
                }
            }

            db.SaveChanges();
            db.Database.Connection.Close();
        }
    }
}
4

0 回答 0