大约4年前,我asked this question。然后我决定不再使用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 });
}
答案 0 :(得分: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}
}
答案 1 :(得分:0)
您通常不必这样做(因为它在使用任何独立的ServiceStack库时会自动调用),但是如果您要添加运行时属性,则需要确保通过调用{来调用ServiceStack.Text静态构造函数{3}}手动,例如:
private void SetTableMeta()
{
JsConfig.InitStatics();
//...
}
否则OrmLiteConnectionFactory
也会在其构造函数中调用它,因此您可以在初始化__dbFactory
后初始化任何运行时属性,例如:
_dbFactory = new OrmLiteConnectionFactory(...); SetTableMeta();
_db = _dbFactory.Open();