Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.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/2/.net/25.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# Upsert在LiteDB中不起作用_C#_.net_Litedb - Fatal编程技术网

C# Upsert在LiteDB中不起作用

C# Upsert在LiteDB中不起作用,c#,.net,litedb,C#,.net,Litedb,我正在尝试这样做: db = new LiteDatabase(@"albumdata.db"); db_string = db.GetCollection<StringPair>("strings"); db.Engine.EnsureIndex("strings", "a", true); db_string.Upsert(new StringPair("a", "1")); // this line throws this exception : LiteDB.LiteExc

我正在尝试这样做:

db = new LiteDatabase(@"albumdata.db");
db_string = db.GetCollection<StringPair>("strings");
db.Engine.EnsureIndex("strings", "a", true);

db_string.Upsert(new StringPair("a", "1"));
// this line throws this exception : LiteDB.LiteException: 'Cannot insert duplicate key in unique index 'a'. The duplicate value is '"a"'.'
db_string.Upsert(new StringPair("a", "1"));
db=新的LiteDatabase(@“albumdata.db”);
db_string=db.GetCollection(“strings”);
db.Engine.EnsureIndex(“字符串”,“a”,true);
db_string.Upsert(新字符串对(“a”、“1”));
//此行引发此异常:LiteDB.LiteException:“无法在唯一索引“a”中插入重复键。”。重复值为“a”。'
db_string.Upsert(新字符串对(“a”、“1”));
但正如代码中提到的,我收到了以下错误:LiteDB.LiteException:“无法在唯一索引“a”中插入重复键。”。重复值为“a”。


是否为插入或更新而向上插入(如果存在)?

是否您的
StringPair
类包含唯一的Id属性(
\u Id
字段)。LiteDB使用PK索引(
\u id
字段)检查是否存在文档插入或更新。 尝试以下类结构:

public class StringPair
{
    public StringPair(string a, string b)
    {
        this.Id = a;
        this.OtherField = b;
    }

    public StringPair()
    {
        // don't forgot parameterless ctor
    }

    // Define "Id" or use [BsonId] in your property or use FluentApi mapper

    public string Id { get; set; }
    public string OtherField { get; set; }
}


db = new LiteDatabase(@"albumdata.db");

db_string = db.GetCollection<StringPair>("strings");

// PK already contains unique index
// db.Engine.EnsureIndex("strings", "a", true);

db_string.Upsert(new StringPair("a", "1")); // insert

db_string.Upsert(new StringPair("a", "2")); // update
公共类字符串对
{
公共字符串对(字符串a、字符串b)
{
这个.Id=a;
this.OtherField=b;
}
公共字符串对()
{
//别忘了无参数计算器
}
//在属性中定义“Id”或使用[BsonId],或使用FluentApi映射器
公共字符串Id{get;set;}
公共字符串OtherField{get;set;}
}
db=新的LiteDatabase(@“albumdata.db”);
db_string=db.GetCollection(“strings”);
//PK已包含唯一索引
//db.Engine.EnsureIndex(“字符串”,“a”,true);
db_string.Upsert(新字符串对(“a”、“1”);//插入
db_string.Upsert(新字符串对(“a”、“2”));//更新

是您的
StringPair
类包含唯一的Id属性(
\u Id
字段)。LiteDB使用PK索引(
\u id
字段)检查是否存在文档插入或更新。 尝试以下类结构:

public class StringPair
{
    public StringPair(string a, string b)
    {
        this.Id = a;
        this.OtherField = b;
    }

    public StringPair()
    {
        // don't forgot parameterless ctor
    }

    // Define "Id" or use [BsonId] in your property or use FluentApi mapper

    public string Id { get; set; }
    public string OtherField { get; set; }
}


db = new LiteDatabase(@"albumdata.db");

db_string = db.GetCollection<StringPair>("strings");

// PK already contains unique index
// db.Engine.EnsureIndex("strings", "a", true);

db_string.Upsert(new StringPair("a", "1")); // insert

db_string.Upsert(new StringPair("a", "2")); // update
公共类字符串对
{
公共字符串对(字符串a、字符串b)
{
这个.Id=a;
this.OtherField=b;
}
公共字符串对()
{
//别忘了无参数计算器
}
//在属性中定义“Id”或使用[BsonId],或使用FluentApi映射器
公共字符串Id{get;set;}
公共字符串OtherField{get;set;}
}
db=新的LiteDatabase(@“albumdata.db”);
db_string=db.GetCollection(“strings”);
//PK已包含唯一索引
//db.Engine.EnsureIndex(“字符串”,“a”,true);
db_string.Upsert(新字符串对(“a”、“1”);//插入
db_string.Upsert(新字符串对(“a”、“2”));//更新

如果使用类的属性
BsonIdAttribute
告诉LiteDb引擎类的哪些属性应被视为id,则可以轻松保留类结构:

public sealed class StringPair
{
    [BsonId]
    public string First { get; set; }
    public string Second { get; set; }
}

如果使用类的属性
BsonIdAttribute
告诉LiteDb引擎类的哪些属性应被视为id,则可以轻松地保留类结构:

public sealed class StringPair
{
    [BsonId]
    public string First { get; set; }
    public string Second { get; set; }
}