This question is locked. New answers and comments are not allowed.
Hi,
I am trying to convert a existing model to OpenAccess for evaluation but I have a little problem with the mapping of an existing model.
All domain classes in our model derive from an abstract base class Entity which just contains an Id Guid field - how can i generate a fluent mapping for domain classes which have the identity column in an abstract base class?
A simple example:
The resulting table should just contain three fields: Id(primary key), Name and Released.
Thanks
I am trying to convert a existing model to OpenAccess for evaluation but I have a little problem with the mapping of an existing model.
All domain classes in our model derive from an abstract base class Entity which just contains an Id Guid field - how can i generate a fluent mapping for domain classes which have the identity column in an abstract base class?
A simple example:
public interface IEntity
{
Guid Id { get; }
}
public abstract class Entity : IEntity
{
public Guid Id { get; set; }
}
public class Product : Entity
{
public string Name { get; set; }
public DateTime Released { get; set; }
}
The resulting table should just contain three fields: Id(primary key), Name and Released.
Thanks
5 Answers, 1 is accepted
0
Hi xkr,
I hope this is helpful, however if you face further trouble do not hesitate to contact us back. All the best,
Serge
the Telerik team
You can easily define the setup needed for this to work using our inheritance API in the Fluent Mapping. You can have a look at our online documentation for more info on the matter. You basically have to map the classes with either vertical or flat mapping strategy. Following is a sample FluentMetadataSource that I have created in order to test your scenario.
public
class
MyFluentMetadataSource : FluentMetadataSource
{
protected
override
IList<MappingConfiguration> PrepareMapping()
{
List<MappingConfiguration> list =
new
List<MappingConfiguration>();
MappingConfiguration<Entity> entityConfiguration =
new
MappingConfiguration<Entity>();
entityConfiguration.MapType().ToTable(
"Entities"
);
entityConfiguration.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Guid);
MappingConfiguration<Product> productConfiguration =
new
MappingConfiguration<Product>();
productConfiguration.MapType()
.Inheritance(Telerik.OpenAccess.InheritanceStrategy.Flat)
.ToTable(
"Entities"
);
list.Add(entityConfiguration);
list.Add(productConfiguration);
return
list;
}
}
I hope this is helpful, however if you face further trouble do not hesitate to contact us back. All the best,
Serge
the Telerik team
0
Paresh
Top achievements
Rank 1
answered on 22 Aug 2014, 06:52 PM
Hi
Can this be done using Horizontal mapping strategy?
Thanks,
Paresh
Can this be done using Horizontal mapping strategy?
Thanks,
Paresh
0
Hello Paresh,
Generally similar set-up is possible using Fluent Mapping and Horizontal inheritance. You just have to keep in mind the following:
1. The identity property in the base class has to be backed up by a protected field.
2. You need to specify the mapping for the primary keys in both the base class and in each inheritance child. Also you need to specify that the Identity property is back-up by a field that is located in the base class.
For example, let say that you have class named Animal which would be the base class and another one named Dog which would be the child in the inheritance. They look like this:
Then the mapping of those classes should look like this (I have marked mapping of the identity in red):
Please let us know if you have any more questions or any further assistance.
Regards,
Boyan
Telerik
Generally similar set-up is possible using Fluent Mapping and Horizontal inheritance. You just have to keep in mind the following:
1. The identity property in the base class has to be backed up by a protected field.
2. You need to specify the mapping for the primary keys in both the base class and in each inheritance child. Also you need to specify that the Identity property is back-up by a field that is located in the base class.
For example, let say that you have class named Animal which would be the base class and another one named Dog which would be the child in the inheritance. They look like this:
public
class
Animal
{
protected
int
_id;
public
virtual
int
Id
{
get
{
return
this
._id; }
set
{
this
._id = value;}
}
protected
int
_name;
public
virtual
int
Name
{
get
{
return
this
._name; }
set
{
this
._name = value;}
}
protected
int
_age;
public
virtual
int
Age
{
get
{
return
this
._age; }
set
{
this
._age = value; }
}
}
public
class
Dog : Animal
{
protected
int
_bestFriend;
public
virtual
int
BestFriend
{
get
{
return
this
._bestFriend; }
set
{
this
._bestFriend = value; }
}
}
Then the mapping of those classes should look like this (I have marked mapping of the identity in red):
MappingConfiguration<Animal> animalConfiguration =
new
MappingConfiguration<Animal>();
animalConfiguration.MapType(x =>
new
{ }).Inheritance(Telerik.OpenAccess.InheritanceStrategy.Horizontal);
animalConfiguration.HasProperty(x => x.Id).IsIdentity().HasFieldName(
"_id"
);
animalConfiguration.HasProperty(x => x.Name).HasFieldName(
"_name"
);
animalConfiguration.HasProperty(x => x.Age).HasFieldName(
"_age"
); ;
MappingConfiguration<Dog> dogConfiguration =
new
MappingConfiguration<Dog>();
dogConfiguration
.MapType(x =>
new
{ })
.ToTable(
"Dog"
);
dogConfiguration.HasProperty(d => d.Id).IsIdentity(KeyGenerator.Autoinc).HasFieldName(
"Animal._id"
);
dogConfiguration.HasProperty(d => d.BestFriend).HasFieldName(
"_bestFriend"
);
Please let us know if you have any more questions or any further assistance.
Regards,
Boyan
Telerik
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
0
Paresh
Top achievements
Rank 1
answered on 27 Aug 2014, 04:10 PM
Hi Boyan,
Thank you for your reply. I got this code to working with only specifying identity in the Dog class and without using HasField method.
Is there any specific reason for doing #2 mentioned in your post?
Thanks,
Paresh
Thank you for your reply. I got this code to working with only specifying identity in the Dog class and without using HasField method.
Is there any specific reason for doing #2 mentioned in your post?
Thanks,
Paresh
0
Hello Paresh,
You are right that this would work without explicitly specifying backing fields. I wanted to depict a more wider scenario that included those fields as well.
The important thing here is that the mapping of the identity column should be part of the mapping of each hierarchy child class as well.
Do not hesitate to get back to up with any further questions.
Regards,
Boyan
Telerik
You are right that this would work without explicitly specifying backing fields. I wanted to depict a more wider scenario that included those fields as well.
The important thing here is that the mapping of the identity column should be part of the mapping of each hierarchy child class as well.
Do not hesitate to get back to up with any further questions.
Regards,
Boyan
Telerik
OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.