2

I have a base class I would like to extend in order to add and/or modify some of the existing properties for it to be serialized to an SQL database.

Particularly, I have a non-trivial property that I would like to serialize as string:

public class BaseClass {
    public NonTrivialType Id {get;set;}
}

Therefore, I was thinking of re-declaring in this derived class a new property with the same name:

public class DerivedClass : BaseClass {
    public new string Id {get;set;}
}

This way, EntityFramework would pick that up, serialize it and use it as key using the following configuration in my DbContext's OnModelCreating function:

modelBuilder.Entity<DerivedClass>().ToTable("MyTable").HasKey(_ => _.Id)

However, I get a validation exception, which means that EF is not doing what I wanted him to do.

My intention here is to "hide" the original declaration and only show/serialize the new one. Is that even possible?

marcofo88
  • 375
  • 2
  • 10
  • yes that should work IMO. derivedclass will shadow the Id member because of the new keyword. are you sure the cause for the validationexception is Id? could you show exception details, and on which LOC does it throw? – Cee McSharpface Jan 29 '18 at 13:11
  • related: https://stackoverflow.com/q/23206559/1132334, "EF probably adds my override in first, then proceeds to add the base property as well, even though I'm trying to hide it, and that's when it crashes" – Cee McSharpface Jan 29 '18 at 13:14
  • Thanks for your reply! The exception I get is the following: {"An unexpected exception was thrown during validation of 'Id' when invoking System.ComponentModel.DataAnnotations.MaxLengthAttribute.IsValid. See the inner exception for details."} Additional information: An unexpected exception was thrown during validation of 'Id' when invoking System.ComponentModel.DataAnnotations.MaxLengthAttribute.IsValid. Inner Exception: {"Unable to cast object of type 'NonTrivialType' to type 'System.Array'."} – marcofo88 Jan 29 '18 at 13:29
  • Btw, the error is thrown when SaveChanges is invoked. – marcofo88 Jan 29 '18 at 13:34
  • this proves that the framework is still accessing the base class Id member. Can you make the nontrivialtype one a private or protected property, somethnig like `IdInternal` and flesh out the `public string Id` accessors with the serializing/deserializing logic? – Cee McSharpface Jan 29 '18 at 14:02
  • Exactly! The thing is that doing what you say would imply modifying the base class, which I'm trying to avoid (the base class is a business one and I didn't want it to be tainted with this data manipulation logic). My idea is to keep all these concerns in the derived class, which is like a DTO class for the business one. – marcofo88 Jan 29 '18 at 14:22
  • would adding the `notmapped` attribute to the base class Id also count as a modification? No idea if it even helps, but maybe [worth a try](https://stackoverflow.com/q/10385248/1132334) – Cee McSharpface Jan 29 '18 at 14:26
  • 1
    Doing so now raises an `InvalidOperationException`saying that: The key component 'Id' is not a declared property on type 'PlpTransform'. Verify that it has not been explicitly excluded from the model and that it is a valid primitive property. I'm probably going to just use a new name in my derived class and ignore the property in the base one. – marcofo88 Jan 29 '18 at 14:35

0 Answers0