Hi,
I have these 2 classes:
public
class
Base
{
protected
String mValue;
public
String Value
{
get
{
return
mValue;}
set
{ mValue = value; }
}
}
public
class
Derived : Base
{
public
int
Id {
get
;
set
; }
}
and I want to map only the Derived class to a table, something like this:
var derived =
new
MappingConfiguration<Derived>();
derived.MapType(d =>
new
{
Id = d.Id,
});
derived.HasProperty(d => d.Id).IsIdentity(KeyGenerator.Autoinc);
derived.HasProperty(d => d.Value).HasFieldName(
"Base.mValue"
);
For Base class there is no mapping to a table and neither inheritance. Unfortunately this does not work and I get the error:
Additional information: Mapping for field 'Base.mValue' is specified in the file 'config', but the field is not present in the class 'Derived'.
5 Answers, 1 is accepted
Based on the provided information, it seem that you want to do Horizontal inheritance - each immediate subclass is stored in its own table with a "copy" of the fields from the base class (more information about the supported types of inheritance is available here). If this is the case, you need to set the inheritance strategy of the base class through the Inheritance method.
Your mapping should looks like the following code:
protected override IList<
MappingConfiguration
> PrepareMapping()
{
var configurations = new List<
MappingConfiguration
>();
var baseConfiguration = new MappingConfiguration<
Base
>();
baseConfiguration.MapType(x => new { }).Inheritance(Telerik.OpenAccess.InheritanceStrategy.Horizontal);
configurations.Add(baseConfiguration);
var derivedConfiguration = new MappingConfiguration<
Derived
>();
derivedConfiguration.MapType(x => new { });
derivedConfiguration.HasProperty(d => d.Id).IsIdentity(KeyGenerator.Autoinc);
derivedConfiguration.HasProperty(d => d.Value).HasFieldName("Base.mValue");
configurations.Add(derivedConfiguration);
return configurations;
}
I hope this helps. Let us know if you need further information.
Regards,
Pavel Uzunov
Telerik
Thanks for reply Pavel!
I was was not intending to include Base class in the DB but with horizontal inheritance the Base class will not be mapped to a table. So I think this would be the right solution.
I have still another question regarding the mapping.
Up to previous release there was the code generator included. This code generator had used in the generated classes attributes for specifying the mapping properties. However with Q3 release the code generator was discontinued but the attributes class are still available in the SDK, like for example Telerik.OpenAccess.PersistentAttribute.
My question is: in code-only approach, is it possible to specify the mapping using the attributes like in the designer generated code?
Thank you for the feedback.
On your question, it's not possible to specify the mapping using the attributes like in the designer. In general, I would recommend you to use Fluent API with latest Data Access NuGet packages. But the fact that we are no longer shipping our Visual Designer does not mean that you're forced to stop using any earlier version of Telerik Data Access. We will continue to deliver earlier packages to existing clients.
Currently, we have an idea to release a tool (much simpler compared to the New Fluent Model wizard) that will allow the developers to generated database-first models using the Fluent API. The timeframe is not set yet, but the feature request about this is available here.
Additionally, it will be very helpful, if you could share the reasons that push you to mix attributes mapping and fluent mapping.
I hope this information helps.
Regards,
Pavel Uzunov
Telerik
Hi Pavel,
the main reason is that we are trying to establish the strengths and the limitations of the Telerik ORM.
We are developing a new product where we want to use an ORM to access a DB in a modern fashion. Our approach is model-first or code-first. We did like the Visual Designer but unfortunately it was discontinued.
Based on our requirements we build a small candidates list which includes Telerik Fluent Model and so far is doing quite well. We have still some problems to solve as you will see in my other post(s) but I think Telerik is a serious candidate.
Thanks again for your support!
You are welcome and thank you for the information.
Please do not hesitate to getting back to me if you need further assistance.
Pavel Uzunov
Telerik