none
EF6, Update deattached entity's navigation properties.

    Question

  • Hello.

    I'm using EF6 and GenericRepository pattern as DAL.

    Here is my model:

        public class Project
        {
            public Project()
            {
                ProjectAssets = new List<ProjectAsset>();
            }
    
            public int ProjectID { get; set; }
            public string Name { get; set; }
            public virtual ICollection<ProjectAsset> ProjectAssets { get; set; }
        }
    
        public class Asset
        {
            public Asset()
            {
                Revisions = new List<Revision>();
            }
    
            public int AssetID { get; set; }
            public string Name { get; set; }
            public short Type { get; set; }
            public virtual ICollection<Revision> Revisions { get; set; }
        }
    
        public class ProjectAsset
        {
            public int MappingID { get; set; }
            public int ProjectID { get; set; }
            public int AssetID { get; set; }
            public virtual Asset Asset { get; set; }
            public virtual Project Project { get; set; }
        }

    Now i want to create new Asset and add it to ProjectAsset of current project(it's Business Entities, not EF Entities, in BLL.GenericRepository<TEntity, TBEntity> it's mapping to POCO Entities).

    var asset = ServiceLocator.Default.ResolveType<IPipeLine>().CreateAsset((short) type, fileName);
    Project.ProjectAssets.Add(new ProjectAsset(Project, asset));
    Project = ServiceLocator.Default.ResolveType<IPipeLine>().UpdateProject(Project);
    
    //....
    
    public void Update<TEntity>(TEntity entity) where TEntity : class
            {
                var fqen = GetEntityName<TEntity>();
    
                object originalItem;
                var key = ((IObjectContextAdapter)DbContext).ObjectContext.CreateEntityKey(fqen, entity);
                if (((IObjectContextAdapter)DbContext).ObjectContext.TryGetObjectByKey(key, out originalItem))
                    ((IObjectContextAdapter)DbContext).ObjectContext.ApplyCurrentValues(key.EntitySetName, entity);
            }

    So, after 

    UpdateProject(Project);

    I have ProjectAsset with not null Project and Asset properties (I creating ProjectAsset setting only ProjectID and AssetID), but MappingID still == 0 so there is no record of this ProjectAsset in DB. 

    I think the problem that EF don't see any chages after ApplyCurrentValues so it's even don't trying to add it to DB (i've used IntelliSense to see sql command and there is no activity on Apply or SaveChanges).

    Can you please help me, how to update navigation properties properly.

               
    Thursday, October 17, 2013 8:21 AM

Answers

All replies

  • Hello,

    Thanks for posting to this forum.

    As far as I know, ApplyCurrentValues() method  copies the scalar values from the supplied object into the object in the System.Data.Objects.ObjectContext that has the same key.

    So the operation will cover the data in the System.Data.Objects.ObjectContext ranther than in the database, we need to call the db.SaveChanges() at last method to complete the transaction.

      using (DBContext18_1 db = new DBContext18_1())
    
                {
    
                    //db.Database.Create();
    
                    db.Assets.Load();
    
    
                    Asset asset = new Asset() { AssetID = 6, Name = "A003", Type = 3 };
    
                    asset.Type = 4;
    
    
                    ((IObjectContextAdapter)db).ObjectContext.ApplyCurrentValues("Assets", asset);
    
    
                    db.SaveChanges();
    
                }
    

    The codes above, if I do not call the db.SaveChanges(), the operation will not affect the database. The type will remain to be 3 rather 4.

    If I have misunderstood, please let me know.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 18, 2013 6:19 AM
    Moderator
  • Yeah, sorry, it's my fault.

    Ofcourse i call SaveChanges.

    Friday, October 18, 2013 8:20 PM
  • Glad to hear that you have found a solution, and if it is possible could you please share the solution for this issue.

    Thanks&Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, October 21, 2013 8:34 AM
    Moderator