ServiceStack.OrmLite:在哪里添加运行时属性(而不是内联/设计时属性)?

问题描述 投票:0回答:2

大约四年前,我问过这个问题。然后我决定当时不使用 OrmLite,但我现在又回到了它。

这次我想知道在哪里添加添加运行时属性的代码?我尝试如下,但运行时属性没有出现。

我错过了什么?

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 });
}

c# servicestack ormlite-servicestack
2个回答
1
投票

就像以前很多次一样,我在发布问题后找到了答案。但是,也许它会对其他人有所帮助。

解决方案: 将 SetTableMet() 移至创建 Db 工厂和 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}
}


0
投票

您通常不必执行此操作(因为在使用任何独立 ServiceStack 库时会自动调用它),但如果您要添加运行时属性,则需要确保通过调用 JsConfig 来调用 ServiceStack.Text 静态构造函数。手动 InitStatics(),例如:

private void SetTableMeta()
{
    JsConfig.InitStatics();
    //...
}

否则,

OrmLiteConnectionFactory
也会在其构造函数中调用它,以便您可以在初始化
__dbFactory
后初始化任何运行时属性,例如:

_dbFactory = new OrmLiteConnectionFactory(...); SetTableMeta();

_db = _dbFactory.Open();
© www.soinside.com 2019 - 2024. All rights reserved.