将日志对象保存到 sqlite 没有 id 只插入一条记录?
Saving a Log Object to sqllite no id only one record gets inserted?
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 数据库。
似乎只有一个对象始终可用
并得到 updated.Hence 没有新记录得到
已创建。
如果您不在 OrmLite 中提供主键,OrmLite 将假定主键是 table 中的第一个 属性,这在这种情况下不是您想要的。您需要通过使用 [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);
}
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 数据库。
似乎只有一个对象始终可用 并得到 updated.Hence 没有新记录得到 已创建。
如果您不在 OrmLite 中提供主键,OrmLite 将假定主键是 table 中的第一个 属性,这在这种情况下不是您想要的。您需要通过使用 [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);
}