fluent configuration and dot in table name

2 posts, 0 answers
  1. Paul Borodaev
    Paul Borodaev avatar
    1 posts
    Member since:
    Mar 2006

    Posted 29 Sep 2012 Link to this post

    Ihave simple contract
    [EnableClientAccess]
        [DataContract]
        public class LogEvent
        { 
            [Key]
            [DataMember]
            public virtual int EventId { get; set; }
            [DataMember]
            public virtual string Source { get; set; }
            [DataMember]
            public virtual DateTime Timestamp { get; set; }
            [DataMember]
            public virtual byte? EventTypeId { get; set; } //SeverityId         
            [DataMember]
            public virtual int? UserId { get; set; }
            [DataMember]
            public virtual string Message { get; set; }
        }
    DDL (MS SQL 2008):
    CREATE TABLE [dbo].[System.Log](
        [EventId] [int] IDENTITY(1,1) NOT NULL,
        [Source] [nvarchar](80) NULL,
        [Timestamp] [datetime] NOT NULL,
        [EventTypeId] [tinyint] NOT NULL,
        [ActivityTypeId] [tinyint] NULL,
        [UserId] [smallint] NULL,
        [Message] [nvarchar](4000) NOT NULL,
     CONSTRAINT [PK_System.Log] PRIMARY KEY CLUSTERED
    (
        [EventId] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]

    & fluent configuration
    protected
    override IList<MappingConfiguration> PrepareMapping()
            {
                List<MappingConfiguration> configurations = new List<MappingConfiguration>(); //preparedConfigs
     
                var logEventMapping = new MappingConfiguration<LogEvent>();
                logEventMapping.MapType(logEvent => new
                {
                    EventId = logEvent.EventId,
                    EventTypeId = logEvent.EventTypeId,
                    Message = logEvent.Message,
                    Source = logEvent.Source,
                    Timestamp = logEvent.Timestamp,
                    UserId = logEvent.UserId
                }).ToTable("System.Log"); //<<<Problem here
                logEventMapping.HasProperty(logEvent => logEvent.EventId).IsIdentity(KeyGenerator.Autoinc);
                  
                configurations.Add(logEventMapping);

                return configurations;
            }
    When I'm trying to get entities:
    var db = new FluentModelContext();
    var events = db.GetAll<LogEvent>();

    exception thrown (on populating)
    {"Error executing query: Telerik.OpenAccess.RT.sql.SQLException: Invalid object name \"Log\".\r\n   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery()\r\n   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery()\r\n   at OpenAccessRuntime.Relational.fetch.FetchResultImp.Execute()\nSQL:\nSELECT a.[EventId] AS COL1, a.[EventTypeId] AS COL2, a.[Message] AS COL3, a.[Source] AS COL4, a.[Timestamp] AS COL5, a.[UserId] AS COL6 FROM [Log] a ORDER BY COL1  Telerik.OpenAccess.RT.sql.SQLException: Недопустимое имя объекта \"Log\".\r\n   at Telerik.OpenAccess.RT.Adonet2Generic.Impl.PreparedStatementImp.executeQuery()\r\n   at OpenAccessRuntime.Relational.conn.PooledPreparedStatement.executeQuery()\r\n   at OpenAccessRuntime.Relational.fetch.FetchResultImp.Execute()"} Telerik.OpenAccess.OpenAccessException {Telerik.OpenAccess.Exceptions.DataStoreException}

    I've tried "[System.Log]", "'System.Log'", "'dbo'.'System.Log'", "[dbo].[System.Log]",
    but nothing worked for me (similar exception - Invalid object name "log_event"... or something related to base LogEvent class (but there is no base class!)
    )
    Changing table name to "Log" - works, but I need this prefix.



  2. PetarP
    Admin
    PetarP avatar
    754 posts

    Posted 02 Oct 2012 Link to this post

    Hello Paul,

     As you have probably already figured out this is caused by our internal representation of the string you pass in. We are splitting the string using the dot symbol as a separator and we are taking the last part as an actual table name while the first one acts as a schema name. If you look closely you will see that the .ToTable method actually takes a TableName as parameter. You can use that table name to specify the correct structure of your relational model and pass that as parameter.
    Your code should look like this:

    TableName tableName = new TableName("System.Log",string.Empty);
    logEventMapping.MapType(logEvent => new
                {
                    EventId = logEvent.EventId,
                    EventTypeId = logEvent.EventTypeId,
                    Message = logEvent.Message,
                    Source = logEvent.Source,
                    Timestamp = logEvent.Timestamp,
                    UserId = logEvent.UserId
                }).ToTable(tableName);
    Please note that in the above example you can replace the sting.Empty with an actual schema name.
    I hope that works for you.

    Regards,
    Petar
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
Back to Top