Search code examples
c#servicestackormlite-servicestack

ServiceStack.OrmLite: Where to add runtime attributes (instead of inline/design-time attributes)?


Some 4 years ago, I asked this question. I then decided not to use OrmLite at that point, but I am back at it now again.

This time, I would like to know where I add the code for adding runtime attributes? I tried as below, but the runtime attributes does not appear.

What am I missing?

public ActorNotificationDbHandler()
{
    DbAccount dbAccount = DBAccounts[0];
    SetTableMeta();

    _dbFactory = new OrmLiteConnectionFactory($"Uid={dbAccount.Username};Password={dbAccount.Password};Server={dbAccount.Address};Port={dbAccount.Port};Database={dbAccount.Database}", MySqlDialect.Provider);
    _db = _dbFactory.Open();

    if (_db.CreateTableIfNotExists<ActorNotification>())
    {
        _db.Insert(new ActorNotification { CreatedTime = DateTime.Now, ToActor = 123, Status = ActorNotification.ActorNotificationStatuses.Created });
        _db.Insert(new ActorNotificationMessage { ActorNotificationId = 1, MessageState = ActorNotificationMessage.MessageStates.Created });
    }

    var result = _db.SingleById<ActorNotification>(1);
    result.PrintDump(); //= {Id: 1, Name:Seed Data}
}

private void SetTableMeta()
{
    typeof(ActorNotification).GetProperty("Id").AddAttributes(new IndexAttribute { Unique = true }, new AutoIncrementAttribute());
    typeof(ActorNotification).GetProperty("ToActor").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotification).GetProperty("CoreObjectId1").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotification).GetProperty("CoreObjectId2").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotification).GetProperty("Status").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotification).GetProperty("CreatedTime").AddAttributes(new IndexAttribute { Unique = false });

    typeof(ActorNotificationMessage).GetProperty("Id").AddAttributes(new IndexAttribute { Unique = true }, new AutoIncrementAttribute());
    typeof(ActorNotificationMessage).GetProperty("ActorNotificationId").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotificationMessage).GetProperty("FrontEndServerHandler").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotificationMessage).GetProperty("TimeCreated").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotificationMessage).GetProperty("TimeStatusChanged").AddAttributes(new IndexAttribute { Unique = false });
    typeof(ActorNotificationMessage).GetProperty("MessageState").AddAttributes(new IndexAttribute { Unique = false });
}


Solution

  • As so many times before, I find the answer after I post the question. But, maybe it will help someone else.

    Solution: Move the SetTableMet() to AFTER creating the Db factory and db:

    public ActorNotificationDbHandler()
    {
        DbAccount dbAccount = Core.Server.SRefCoreServer.dbHandler.DBAccounts[0];
                
        _dbFactory = new OrmLiteConnectionFactory($"Uid={dbAccount.Username};Password={dbAccount.Password};Server={dbAccount.Address};Port={dbAccount.Port};Database={dbAccount.Database}", MySqlDialect.Provider);
        _db = _dbFactory.Open();
        SetTableMeta();
    
        if (_db.CreateTableIfNotExists<ActorNotification>())
        {
            _db.Insert(new ActorNotification { CreatedTime = DateTime.Now, ToActor = 123, Status = ActorNotification.ActorNotificationStatuses.Created });
            _db.Insert(new ActorNotificationMessage { ActorNotificationId = 1, MessageState = ActorNotificationMessage.MessageStates.Created });
        }
    
        var result = _db.SingleById<ActorNotification>(1);
        result.PrintDump(); //= {Id: 1, Name:Seed Data}
    }