FluentMapping and horizontal inheritance problems

2 posts, 0 answers
  1. Alain
    Alain avatar
    33 posts
    Member since:
    Feb 2009

    Posted 09 Jan 2012 Link to this post

    Hello,

    OpenAccess: 2011.3.1129
    Visual Studio: 2010

    I try to use Open Access in Fluent Mapping mode with a base and derived classes, but OpenAccess gives me an error at runtime:
    Caused by: Invalid object name 'voa_keygen'. Telerik.OpenAccess.RT.sql.SQLException: Invalid object name 'voa_keygen'.
       at Telerik.OpenAccess.RT.Adonet2Generic.Impl.StatementImp.executeQuery(String sql)
       at OpenAccessRuntime.Relational.sql.HighLowRelationalKeyGenerator.IsCached(Connection con, RelationalKeyGeneratorInfoCache infoCache, String tab, String kCol, String vCol, String key, Boolean& ret)
       at OpenAccessRuntime.Relational.sql.HighLowRelationalKeyGenerator.init(String className, RelationalTable theClassTable, Connection con, RelationalKeyGeneratorInfoCache relationalKeyGeneratorInfoCache)

    I tried to follow the instructions in Open Access help guide or from some topics of this forum about 'voa_keygen', but without success.

    Structure of my SQL tables (SQL 2008):
    CREATE TABLE [Categories](
        [ID] [int]IDENTITY(1,1) NOT NULL,
        [Title] [nvarchar](50) NULL,
     CONSTRAINT [PK_Categories] PRIMARY KEY CLUSTERED
    (
        [ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
     
    CREATE TABLE [dbo].[TestObjects](
        [ID] [int] IDENTITY(1,1) NOT NULL,
        [Title] [nvarchar](44) NOT NULL,
        [CategoryID] [int] NOT NULL,
    CONSTRAINT [PK_TestObjects] PRIMARY KEY CLUSTERED
    (
        [ID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    Structure of the classes corresponding to the 2 table above, inherited from a base class (BaseObject):
    Public MustInherit Class BaseObject
      Private m_iId As Integer
     
      Public Property Id() As Integer '(with m_iID field)
    End Class
     
    Public Class Category
      Inherits BaseObject
     
      Private m_sTitle As String
     
      Public Property Title() As String '(with m_sTitle field)
    End Class
     
    Public Class TestObject
      Inherits BaseObject
     
      Private m_nCategory As New Category
      Private m_sTitle As String
     
      Public Property Category() As Category '(with m_nCategory field)
      Public Property Title() As String '(with m_sTitle field)
    End Class

    I define the following code in the derived class of FluentMetadataSource in PrepareMapping() with horizontal inheritance for base class and derived classes:
    Dim nMappingConfigurations As List(Of MappingConfiguration) = New List(Of MappingConfiguration)()
     
    Dim nNamingRules As New NamingRules()
    nNamingRules.CaseMode = CaseChangeModes.PascalCase
    nNamingRules.AddPrefix = "m_n"
     
    Dim nBaseObjectMapping As MappingConfiguration(Of BaseObject) = New MappingConfiguration(Of BaseObject)()
    nBaseObjectMapping.FieldNamingRules = nNamingRules
    nBaseObjectMapping.MapType().Inheritance(InheritanceStrategy.Horizontal)
    nBaseObjectMapping.HasProperty(Function(p) p.Id).HasFieldName("m_iId").IsIdentity(KeyGenerator.Autoinc).ToColumn("ID")
    nMappingConfigurations.Add(nBaseObjectMapping)
     
    Dim nCategoryMapping As MappingConfiguration(Of Category) = New MappingConfiguration(Of Category)()
    nCategoryMapping.FieldNamingRules = nNamingRules
    nCategoryMapping.MapType().ToTable("Categories")
    'nCategoryMapping.HasProperty(Function(p) p.Id).HasFieldName("m_iId").IsIdentity()
    nCategoryMapping.HasProperty(Function(p) p.Title).HasFieldName("m_sTitle").ToColumn("Title")
    nMappingConfigurations.Add(nCategoryMapping)
     
    Dim nTestObjectMapping As MappingConfiguration(Of TestObject) = New MappingConfiguration(Of TestObject)()
    nTestObjectMapping.FieldNamingRules = nNamingRules
    nTestObjectMapping.MapType().ToTable("TestObjects")
    'nTestObjectMapping.HasProperty(Function(p) p.Id).HasFieldName("m_iId").IsIdentity()
    nTestObjectMapping.HasProperty(Function(p) p.Title).HasFieldName("m_sTitle").ToColumn("Title")
    'nTestObjectMapping.HasAssociation(Function(p) p.Category).HasConstraint(Function(p, c) p.Category.Id = c.Id).ToColumn("CategoryID")
    nMappingConfigurations.Add(nTestObjectMapping)

    All mentionned codes are in an assembly created by the project "Telerik OpenAccess Fluent Library" and this assembly is referenced in a asp.net web application.

    When I call the function OpenAccessContext.GetAll(Of TestObject), Open Access gives me the error: Invalid object name 'voa_keygen.

    What is wrong in my code that raises this error?

    Notes:
    1. If I uncomment the lines with "HasProperty... m_iID", Open Access gives me an error at runtime saying that m_iID doesn't exist.
    2. If I uncomment the line "HasAssociation(...", Open Access gives me an error at compilation time: 'A property from the configured type does not take part in the expression'.

    Thank you,
    Alain
  2. Serge
    Admin
    Serge avatar
    375 posts

    Posted 11 Jan 2012 Link to this post

    Hello,

     I took the liberty of modifying your project a bit, in order to make it run as expected. The first issue you had was not specifying identities in your classes. The voa_keygen table is only used (and expected available) when a class is using the internal identity mechanism of OpenAccess and has no property/column defined as a part of the primary key. 

    As to the field not being available, I could not reproduce this problem, uncommenting the line did not cause any problems for me. And as to the last one, in VB, it is impossible to infer the generic types from the HasAssociation call, so you have to explicitly define them with a call containing (Of Category). Also HasConstraint is used to match the primitive properties that are mapped to the same column that the association is, and not in the way you have. You can read more on this in this help article. Using just ToColumn(string) should be enough.

    I hope this proves to be helpful. Please do not hesitate to let us know should you face further trouble. 

    Kind regards,
    Serge
    the Telerik team

    SP1 for Q3’11 of Telerik OpenAccess ORM is available for download

  3. DevCraft banner
Back to Top