I have a class called Resource
, this is inherited by a class called ResourceMeta
I need to upcast ResourceMeta
to Resource
without it still thinking it is a type of ResourceMeta
.
When I try to save my object using entity framework, it will complain about mappings not existing, rightly so because it will be trying to save ResourceMeta
rather th开发者_C百科an Resource
.
I've tried (Resource)resourceMeta
however this still retains the type of ResourceMeta
just limits the properties available to Resource
.
That is not possible, you can't change the actual type of an object.
To get a Resource object from a ResourceMeta object, you have to create a new Resource object using the data from the ResourceMeta object.
Not possible in simple words , you need to create a new instance of Resource and then return
Is inheritance the correct representation of the association between ResourceMeta
and Resource
instances. If not, you could favor composition instead of inheritance and in this way when you want to save the resource instance you could just do resourceMetaInstance.Resource
.
精彩评论