Fluent mapping. Inhering from other class

2 posts, 0 answers
  1. Dilshod
    Dilshod avatar
    23 posts
    Member since:
    Feb 2012

    Posted 14 May 2014 Link to this post

    Hi,
    I have mapped the database using Fluent Mapping and edited one of the generated classes. The changes I made is removing the one property and make the class inherit from other class. NOTE: base class is not in the same dll. When I run I get this error.

    An unhandled exception of type 'Telerik.OpenAccess.Exceptions.MetadataException' occurred in Telerik.OpenAccess.Runtime.dll

    Additional information: Mapping for field '_id' is specified in the file 'config', but the field is not present in the class 'WpfApplication2.Student'. --> EntitiesModel1/namespace[WpfApplication2]/class[Student]/field[_id]


    The things I changed in the generated code.
    1) Removed the Id property
    2) Made the class inherit from other class which is in a different dll
    3) Also tried removing KeyGenerator.Guid in Prepare[Entity]PropertyConfigurations.

    Thanks,




  2. Boyan
    Admin
    Boyan avatar
    100 posts

    Posted 19 May 2014 Link to this post

    Hi Dilshod,

    In order  to overcome this issue, you have to create mapping for the base class expanding your Fluent Model. For example let’s create a class named BaseEntity which has a property named Id that represents the primary key. Each entity derives from the BaseEntity class. I have created the following mapping for this class:

    MappingConfiguration<BaseEntity> baseEntityConfiguration = new MappingConfiguration<BaseEntity>();
    baseEntityConfiguration.MapType(x => new { }).Inheritance(InheritanceStrategy.Horizontal);
    baseEntityConfiguration.HasProperty(x => x.Id).HasFieldName("_id");
    mappingConfigurations.Add(baseEntityConfiguration);

    Please note that there is mapping for the Id property and that the inheritance strategy is set to horizontal allowing the Id property to be part of the table that corresponds to each derived class.

    In order to allow derived entities to use the Id declared in the base class the correct field (the one that comes from the base class) has to be set. For this reason the following modification in each of the derived classes mapping has to be made:

    configuration.HasProperty(x => x.Id).IsIdentity(KeyGenerator.Autoinc).HasFieldName("BaseEntity._id") ...

    While it is possible to distribute the model's entities in separate projects (Fluent Libraries) using the AggregateMetadataSource feature of Telerik Data Access I would advice you not to in this case unless you have some consideration that would benefit from that feature. Could you please tell us more about your current implementation scenario that requires the BaseEntitiy class to be distributed as part of another assembly so we could advice you better? Is it possible to use an interface (implemented by the BaseEntitiy class) in order to achieve the same functionality?

    Please do not hesitate to contact us again should you have 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.
     
  3. DevCraft banner
Back to Top