using ServiceStack;
using ServiceStack.OrmLite;
public static string SqliteFileDb = "~/App_Data/db.sqlite".MapHostAbsolutePath();
private static void CreateX(Message msg)
{
//Using Sqlite DB- improved
var dbFactory = new OrmLiteConnectionFactory(SqliteFileDb, SqliteDialect.Provider);
// Wrap all code in using statement to not forget about using db.Close()
using (var db = dbFactory.Open())
{
db.CreateTableIfNotExists<Message>();
Message notex = new Message();
notex.Content = msg.Content;
notex.Datestamp = msg.Datestamp;
notex.Facility = msg.Facility;
notex.Hostname = msg.Hostname;
notex.LocalDate = msg.LocalDate;
notex.RemoteIP = msg.RemoteIP;
notex.Severity = msg.Severity;
db.Save(notex))
db.Close();
}
}
public class Message
{
public FacilityType Facility { get; set; }
public SeverityType Severity { get; set; }
public DateTime Datestamp { get; set; }
public string Hostname { get; set; }
public string Content { get; set; }
public string RemoteIP{ get; set; }
public DateTime LocalDate { get; set; }
}
有人可以建议如何解决这个问题吗我正在保存系统日志消息的情况使用 ServiceStack ORM 到 SQLITE 数据库。
似乎只有一个对象始终可用并得到更新。因此没有新的记录获得创建。
如果在 OrmLite 中未提供主键,OrmLite 将假定主键是表中的第一个属性,在这种情况下这不是您想要的。您要么需要通过使用 [PrimaryKey]
属性注释来告诉 OrmLite 它应该将哪个属性用于主键,或者只需添加一个数据库将自行填充的自动递增主键,例如:
public class Message
{
[AutoIncrement]
public in Id { get; set; }
public FacilityType Facility { get; set; }
public SeverityType Severity { get; set; }
public DateTime Datestamp { get; set; }
public string Hostname { get; set; }
public string Content { get; set; }
public string RemoteIP{ get; set; }
public DateTime LocalDate { get; set; }
}
此外,db.Close()
using 语句中是多余的,在这种情况下,您不想与 OrmLite 的高级 Save()
API 一起使用,因此您应该只拥有:
using (var db = dbFactory.Open())
{
db.CreateTableIfNotExists<Message>();
Message notex = new Message();
notex.Content = msg.Content;
notex.Datestamp = msg.Datestamp;
notex.Facility = msg.Facility;
notex.Hostname = msg.Hostname;
notex.LocalDate = msg.LocalDate;
notex.RemoteIP = msg.RemoteIP;
notex.Severity = msg.Severity;
db.Insert(notex);
}