none
Entity Framework 5 - Update Method for non loaded entity properties RRS feed

  • Question

  • Hi all,

    I have done a fair bit of digging around for this and struggled to find a definitive answer. Using my old standard practice I have a generic UnitOfWork class and Generic Repository/DbSet update method.

    I have an entity with a number of 'joins' or properties which point to other entities within the DbContext, a brief example below:

        public class Container
        {                
            public int ContainerId { get; set; }

            [MaxLength(50)]
            public string Alias { get; set; }

            [MaxLength(1024)]
            public string Name { get; set; }

            [MaxLength(1024)]
            public string Description { get; set; }

            [MaxLength(1024)]
            public string ContainerUri { get; set; }
            
            [Required]
            public ObjectType ObjectType { get; set; } //ObjectType entity

            [Required]
            public Source Source { get; set; }  //Source entity

            public int State {get;set;}

    }

    In the code I load an object from the database, update the State (for example) and then call dbContext.SaveChanges() it results in the save failing validation due to the ObjectType and Source being null (not loaded in the select)

    Is there a way round this when using a Generic CRUD repository? All I want to do is update a few properties on the parent object without having to load the child objects explicitly either during the initial 'select' or during the update.

    The updates I am doing are generally done in batches so loops of 500 updates doing 2x calls to the database seems excessive for what I need as does building a separate repository class for each of my classes.

    Id just like to be able to change properties which have actually changed......

    Thanks

    Stu

    • Moved by CoolDadTx Tuesday, July 2, 2013 2:03 PM EF related
    Tuesday, July 2, 2013 7:50 AM

Answers

All replies