none
ObjectContext.Tranlate does not work on partial class properties RRS feed

  • Question

  • I use partial classes with EF entities, and I am using Translate method to initialize my object graph, however, Translate method doesn't seem to set the properties on my partial class.

    Any help will be appreciated!

    Thanks,

    Wednesday, April 29, 2015 6:12 PM

Answers

  • Hello sli119,

    From your description, if you just add an additional partial class and add a property as below:

    public partial class Order
    
        {
    
            public Order()
    
            {
    
                this.OrderDetail = new HashSet<OrderDetail>();
    
            }
    
    
            public int OrderID { get; set; }
    
            public string OrderName { get; set; }
    
    
            public virtual ICollection<OrderDetail> OrderDetail { get; set; }
    
    
        }
    
    
        public partial class Order
    
        {
    
            public string OrderDate { get; set; }
    
        }
    

    Unfortunately, for the ObjectContext.Tranlate, it does not work because if you check the source code of this method(you could check this link for details: http://referencesource.microsoft.com/#System.Data.Entity/System/Data/Objects/ObjectContext.cs,e3caf9aa0fe858de):

    private ObjectResult<TElement> InternalTranslate<TElement>(DbDataReader reader, string entitySetName, MergeOption mergeOption, bool readerOwned)
    
            {
    
                …
    
                CollectionColumnMap columnMap;
    
                // for enums that are not in the model we use the enum underlying type
    
                if (MetadataHelper.TryDetermineCSpaceModelType<TElement>(this.MetadataWorkspace, out modelEdmType) ||
    
                    …
    
            }      
    

    It defines a CollectionColumnMap variable to map columns in DbDataReader and the entity type, while the information of columns of the entity type is stored in the MetadataWorkspace which is generated based on the edmx file. While I guess that you actually do not add this column in the database to that mapped table.

    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.

    Thursday, April 30, 2015 8:51 AM
    Moderator

All replies

  • Hello sli119,

    From your description, if you just add an additional partial class and add a property as below:

    public partial class Order
    
        {
    
            public Order()
    
            {
    
                this.OrderDetail = new HashSet<OrderDetail>();
    
            }
    
    
            public int OrderID { get; set; }
    
            public string OrderName { get; set; }
    
    
            public virtual ICollection<OrderDetail> OrderDetail { get; set; }
    
    
        }
    
    
        public partial class Order
    
        {
    
            public string OrderDate { get; set; }
    
        }
    

    Unfortunately, for the ObjectContext.Tranlate, it does not work because if you check the source code of this method(you could check this link for details: http://referencesource.microsoft.com/#System.Data.Entity/System/Data/Objects/ObjectContext.cs,e3caf9aa0fe858de):

    private ObjectResult<TElement> InternalTranslate<TElement>(DbDataReader reader, string entitySetName, MergeOption mergeOption, bool readerOwned)
    
            {
    
                …
    
                CollectionColumnMap columnMap;
    
                // for enums that are not in the model we use the enum underlying type
    
                if (MetadataHelper.TryDetermineCSpaceModelType<TElement>(this.MetadataWorkspace, out modelEdmType) ||
    
                    …
    
            }      
    

    It defines a CollectionColumnMap variable to map columns in DbDataReader and the entity type, while the information of columns of the entity type is stored in the MetadataWorkspace which is generated based on the edmx file. While I guess that you actually do not add this column in the database to that mapped table.

    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.

    Thursday, April 30, 2015 8:51 AM
    Moderator
  • Thanks Fred,

    I know translate works with none edmx object, was hoping that it is smart enough to pick up none edmx properties on edmx objects as well.

    Is there anyway around this?

    Thanks


    • Edited by sli119 Thursday, April 30, 2015 4:47 PM
    Thursday, April 30, 2015 4:43 PM
  • Hello sli119,

    >> Is there anyway around this?

    Since the new added property could be detected at runtime, you could consider use the refection to write yourself Tranlate method based on the existed one, replace the logic to collect the columns map collection with refection instead of using the metadataworkspace.

    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, May 1, 2015 5:57 AM
    Moderator