I have a partial class and I want to do something like the following:
[MetadataType(typeof(UserMetaData))]
public partial class Person
{
public override string PrivateData
{
get
{
return customDecrypt(base.PrivateData);
}
set
{
base.PrivateData = customEncrypt(value);
}
}
}
the above does not work however.
Is there a way to override the base entity framework classes its properties to allow for custom getter/setter?
Ordinarily, this means that you cannot override them; you cannot override in one partial class the methods that are defined in another partial definition of the same class. Nevertheless, you can override these methods by setting the Generates Double Derived flag for the domain class.
Inheritance cannot be applied to partial classes.
All parts of a partial class should be in the same namespace. Each part of a partial class should be in the same assembly or DLL, in other words you can't create a partial class in source files of a different class library project. Each part of a partial class has the same accessibility.
Partial class, interface and structure was introduced in C# 2.0. Now it is possible to split the definition of an class, interface and structure over more than one source files. Moreover the other parts of the class, struct, or interface should be defined in the same namespace or assembly.
Partial classes have nothing to do with inheritance, and override
is entirely about inheritance.
The partial
keyword on a class just means that it can be declared multiple times in the same assembly. It's exactly the same as if you copied every part of every partial class
into the same file and removed the partial
keyword. Since you can't define the same property/function/etc twice in the same class, you can't define it twice in two separate parts of the same class, even with the partial
keyword.
override
, on the other hand, is used in derived classes to indicate that they're replacing the functionality of the base class they inherit from. If it doesn't explicitly inherit, it inherits from object
, which lets you override ToString()
(among others).
Your best options to do what you want are either to use a custom T4 template to generate the encrypt/decrypt logic, or to set the encrypted properties to protected
or private
in the designer and manually add public
versions which do the decryption.
While you can't override the Entity Framework base classes, there is a work around. In the .edmx model (for DB first, direct in entities if code first) on the propery you want to 'override' change the Getter / Setter to private and rename the property. Then create the partial class with the property using the public name, which will no longer conflict.
In the public partial class property you will be able to access the private renamed property if needed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With