none
How to modify and Entity FrameWork generated classes RRS feed

  • Question

  • Hi fellows !

    I need to apply [ScriptIgnore] attribute to the Entity classes generated by the EntityFramework to avoid the 'Circular Reference Exception' white Serializing then into JSON using JavaScriptSerializer.Serealize.

    If i go to manually make changes the changes would be gone the next time i will update my model from database (which i dont want). So how do i apply [ScriptIgnore] to entity classess so that the attribute does not get removed if i change the model later?

    I do not want to use ViewModels to avoid the exception and i want to stick to JavaScriptSerializer . I know there is JSON.Net and Newton soft but i dont want to use them at the moment....


    farooq.hnf

    Monday, February 23, 2015 1:52 PM

Answers

  • You don't want to edit them directly because any time you generate a new one you will lose your changes.

    The answer is buddy classes.

    I routinely use the approach for validation.

    You can probably easily find examples if you google, but here's an example buddy class.

    I have an entity Customer which has a class Customer.cs.

    This is Customer.metadata.cs which is a partial class adding attributes  to the properties of Customer:

       [MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]
        public partial class Customer : BaseEntity
        {
           public void MetaSetUp()
           {
            // In wpf you need to explicitly state the metadata file.
            // Maybe this will be improved in future versions of EF.
               TypeDescriptor.AddProviderTransparent(
                   new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Customer),
                   typeof(CustomerMetadata)),
                   typeof(Customer));
           }
           internal sealed class CustomerMetadata
           {
    
               [Required]
               [StringLength(80, MinimumLength=4, ErrorMessage="Invalid customer name")]
               public string CustomerName { get; set; }
               [Required]
               [StringLength(80, MinimumLength = 4, ErrorMessage = "Invalid first line of address")]
               public string Address1 { get; set; }
               [StringLength(80)]
               public string Address2 { get; set; }
               [StringLength(80)]
               public string Address3 { get; set; }
               [Required]
               [StringLength(50, MinimumLength = 4, ErrorMessage = "Invalid Town or City")]
               public string TownCity { get; set; }
               [Required(ErrorMessage = "Town or City is a required field")]
               [StringLength(8)]
               [RegularExpression("(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})"
                   , ErrorMessage= "Invalid pattern post code")]
               public string PostCode { get; set; }
               [Required(ErrorMessage="PostCode is a required field")]
               [Range (0,50000, ErrorMessage= "Credit Limit must be between 0 and 50,000" )]
               [DataType(DataType.Currency)]
               public Nullable<decimal> CreditLimit { get; set; }
               [Required]
               [DataType(DataType.Currency)]
               public Nullable<decimal> Outstanding { get; set; }
               private CustomerMetadata()
               { }
           }
        }

    In stuff like asp you don't need to have code associates that metadata, in wpf you do.


    I used to be a points hound.
    But I'm alright nooooooooooooooooooooooooooooOOOOOWWWW !

    Monday, February 23, 2015 2:10 PM
  • https://msdn.microsoft.com/en-us/library/wa80x488.aspx?f=255&MSPPError=-2147217396

    The only thing you could possibly do is something with partial classes to not mess with the generated classes but allow functionality of the class to be extended.

    Monday, February 23, 2015 4:04 PM
  • You could also modify the T4 templates that generate your class files.  Or move to a code-first workflow.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, February 23, 2015 5:12 PM

All replies

  • Hi fellows !

    I need to apply [ScriptIgnore] attribute to the Entity classes generated by the EntityFramework to avoid the 'Circular Reference Exception' white Serializing then into JSON using JavaScriptSerializer.Serealize.

    If i go to manually make changes the changes would be gone the next time i will update my model from database (which i dont want). So how do i apply [ScriptIgnore] to entity classess so that the attribute does not get removed if i change the model later?

    I do not want to use ViewModels to avoid the exception and i want to stick to JavaScriptSerializer . I know there is JSON.Net and Newton soft but i dont want to use them at the moment....



    farooq.hnf

    • Moved by CoolDadTx Monday, February 23, 2015 3:24 PM EF related
    • Merged by Fred BaoModerator Tuesday, February 24, 2015 6:10 AM Duplicated
    Monday, February 23, 2015 1:11 PM
  • Farooq,

    In my perception you can better take a more dedicated forum for your question.

    https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework


    Success
    Cor

    Monday, February 23, 2015 1:14 PM
  • You don't want to edit them directly because any time you generate a new one you will lose your changes.

    The answer is buddy classes.

    I routinely use the approach for validation.

    You can probably easily find examples if you google, but here's an example buddy class.

    I have an entity Customer which has a class Customer.cs.

    This is Customer.metadata.cs which is a partial class adding attributes  to the properties of Customer:

       [MetadataTypeAttribute(typeof(Customer.CustomerMetadata))]
        public partial class Customer : BaseEntity
        {
           public void MetaSetUp()
           {
            // In wpf you need to explicitly state the metadata file.
            // Maybe this will be improved in future versions of EF.
               TypeDescriptor.AddProviderTransparent(
                   new AssociatedMetadataTypeTypeDescriptionProvider(typeof(Customer),
                   typeof(CustomerMetadata)),
                   typeof(Customer));
           }
           internal sealed class CustomerMetadata
           {
    
               [Required]
               [StringLength(80, MinimumLength=4, ErrorMessage="Invalid customer name")]
               public string CustomerName { get; set; }
               [Required]
               [StringLength(80, MinimumLength = 4, ErrorMessage = "Invalid first line of address")]
               public string Address1 { get; set; }
               [StringLength(80)]
               public string Address2 { get; set; }
               [StringLength(80)]
               public string Address3 { get; set; }
               [Required]
               [StringLength(50, MinimumLength = 4, ErrorMessage = "Invalid Town or City")]
               public string TownCity { get; set; }
               [Required(ErrorMessage = "Town or City is a required field")]
               [StringLength(8)]
               [RegularExpression("(GIR 0AA)|((([A-Z-[QVX]][0-9][0-9]?)|(([A-Z-[QVX]][A-Z-[IJZ]][0-9][0-9]?)|(([A-Z-[QVX]][0-9][A-HJKSTUW])|([A-Z-[QVX]][A-Z-[IJZ]][0-9][ABEHMNPRVWXY])))) [0-9][A-Z-[CIKMOV]]{2})"
                   , ErrorMessage= "Invalid pattern post code")]
               public string PostCode { get; set; }
               [Required(ErrorMessage="PostCode is a required field")]
               [Range (0,50000, ErrorMessage= "Credit Limit must be between 0 and 50,000" )]
               [DataType(DataType.Currency)]
               public Nullable<decimal> CreditLimit { get; set; }
               [Required]
               [DataType(DataType.Currency)]
               public Nullable<decimal> Outstanding { get; set; }
               private CustomerMetadata()
               { }
           }
        }

    In stuff like asp you don't need to have code associates that metadata, in wpf you do.


    I used to be a points hound.
    But I'm alright nooooooooooooooooooooooooooooOOOOOWWWW !

    Monday, February 23, 2015 2:10 PM
  • https://msdn.microsoft.com/en-us/library/wa80x488.aspx?f=255&MSPPError=-2147217396

    The only thing you could possibly do is something with partial classes to not mess with the generated classes but allow functionality of the class to be extended.

    Monday, February 23, 2015 4:04 PM
  • You could also modify the T4 templates that generate your class files.  Or move to a code-first workflow.

    David


    David http://blogs.msdn.com/b/dbrowne/

    Monday, February 23, 2015 5:12 PM