none
How to Upgrade to DBContext from ObjectContext and what to do with default constructors. RRS feed

  • Question

  • For our newer projects we are using DBContext in Model First but we are currently upgrading some of our older Model First projects to DBContext T4 Genderation from ObjectContext Default/Legacy Generation.

    The area that is stumping me is that we have some Partial Classes that extend the generated classes. This is done mostly for adding validation attributes but also we have few classes that define a default constructor for the class to populate default values for properties.

        [MetadataType(typeof(Case_Validation))]
        public partial class Case     {
            public Case()
            {
                DateEntered = DateTime.Now;
            }
        }
    
    
        public class Case_Validation
        {
            [ScaffoldColumn(false)]
            [Display(Name = "Case Id", Order = 1)]
            public int Id { get; set; }
    
            [ScaffoldColumn(false)]
            public int PersonId { get; set; }
        }
    The problem is that the T4 Template also generates a default constructor.  So I need some guidance in this upgrade scenario. How do you set the default values with DBContext so that when the object gets newed up it will by default have those property values assigned?


    Eric

    Friday, April 11, 2014 4:46 PM

Answers