Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将日志对象保存到sqllite没有id仅插入一条记录?_C#_Sqlite_<img Src="//i.stack.imgur.com/WM7S8.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">ormlite Servicestack - Fatal编程技术网 ormlite-servicestack,C#,Sqlite,ormlite Servicestack" /> ormlite-servicestack,C#,Sqlite,ormlite Servicestack" />

C# 将日志对象保存到sqllite没有id仅插入一条记录?

C# 将日志对象保存到sqllite没有id仅插入一条记录?,c#,sqlite,ormlite-servicestack,C#,Sqlite,ormlite Servicestack,使用ServiceStack; 使用ServiceStack.OrmLite; 公共静态字符串SqliteFileDb=“~/App_Data/db.sqlite”.MapHostAbsolutePath(); 私有静态void CreateX(消息消息消息) { //使用sqlitedb-profected var dbFactory=new-OrmLiteConnectionFactory(SqliteFileDb,sqlitedianal.Provider); //将所有代码包装在usin

使用ServiceStack;
使用ServiceStack.OrmLite;
公共静态字符串SqliteFileDb=“~/App_Data/db.sqlite”.MapHostAbsolutePath();
私有静态void CreateX(消息消息消息)
{
//使用sqlitedb-profected
var dbFactory=new-OrmLiteConnectionFactory(SqliteFileDb,sqlitedianal.Provider);
//将所有代码包装在using语句中,以免忘记使用db.Close()
使用(var db=dbFactory.Open())
{
db.CreateTableIfNotExists();
Message notex=新消息();
notex.Content=msg.Content;
notex.Datestamp=msg.Datestamp;
注x.Facility=msg.Facility;
notex.Hostname=msg.Hostname;
notex.LocalDate=msg.LocalDate;
notex.RemoteIP=msg.RemoteIP;
注x.严重性=消息严重性;
数据库保存(notex))
db.Close();
}
}
公共类消息
{
公共设施类型设施{get;set;}
公共服务器类型严重性{get;set;}
公共日期时间日期戳{get;set;}
公共字符串主机名{get;set;}
公共字符串内容{get;set;}
公共字符串RemoteIP{get;set;}
public DateTime LocalDate{get;set;}

}
如果您没有在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; }
}
另外,在using语句中,
db.Close()
是多余的,在这种情况下,您不希望在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);
}
使用(var db=dbFactory.Open())
{
db.CreateTableIfNotExists();
Message notex=新消息();
notex.Content=msg.Content;
notex.Datestamp=msg.Datestamp;
注x.Facility=msg.Facility;
notex.Hostname=msg.Hostname;
notex.LocalDate=msg.LocalDate;
notex.RemoteIP=msg.RemoteIP;
注x.严重性=消息严重性;
db.插入(注x);
}

谢谢您的回答。我认为这可能是问题所在,但我假设框架会处理这种情况。我将添加一个ID并尝试将其标记为答案。