Identity in base class (Fluent Mapping)

6 posts, 0 answers
  1. xkr
    xkr avatar
    1 posts
    Member since:
    Feb 2011

    Posted 08 Feb 2011 Link to this post

    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:
    public interface IEntity
    {
    Guid Id { get; }
    }

    public abstract class Entity : IEntity
    {
    public Guid Id { getset; }
    }

    public class Product : Entity
    {
    public string Name { getset; }
    public DateTime Released { getset; }
    }

    The resulting table should just contain three fields: Id(primary key), Name and Released.

    Thanks
  2. Serge
    Admin
    Serge avatar
    375 posts

    Posted 11 Feb 2011 Link to this post

    Hi xkr,

     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
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  3. DevCraft banner
  4. Paresh
    Paresh avatar
    13 posts
    Member since:
    Jul 2014

    Posted 22 Aug 2014 in reply to Serge Link to this post

    Hi 

    Can this be done using Horizontal mapping strategy?

    Thanks,
    Paresh
  5. Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 27 Aug 2014 Link to this post

    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:
    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.
     
  6. Paresh
    Paresh avatar
    13 posts
    Member since:
    Jul 2014

    Posted 27 Aug 2014 in reply to Boyan Link to this post

    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 
  7. Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 01 Sep 2014 Link to this post

    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
     
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
     
Back to Top
DevCraft banner