小编SPS*_*101的帖子

当我使用 ReplaceOneAsync 和 IsUpsert = true mongodb 添加一个空 ID 时。我该如何阻止?

如果文档存在,我可以使用以下方法更新文档

var filter = Builders<Neighborhood>.Filter.Eq(x => x.Id, neighborhood.Id);

var result = await collection.ReplaceOneAsync(filter,
             neighborhood,new UpdateOptions { IsUpsert = true });


[CollectionName("neighborhoods")]
[BsonIgnoreExtraElements(true)]
public class Neighborhood : IEntity<string>
{
 [BsonId(IdGenerator = typeof(GuidGenerator))]
 [BsonRepresentation(BsonType.ObjectId)]
 public string Id { get; set; }

 [BsonElement("name")]
 public string  Name    { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

如果 Id = NULL 并且我想返回更新的结果,如何插入文档。

插入新文档NULLID 时,会创建一个带有 ID 的记录NULL,我添加时 [BsonId(IdGenerator = typeof(GuidGenerator))]没有任何运气。

我做错了什么,所以ObjectId可以为新记录生成一个。

c# mongodb mongodb-csharp-2.0 mongodb-.net-driver

8
推荐指数
1
解决办法
3740
查看次数

自动映射将多个属性映射到单个属性

我需要帮助将我的域对象映射到ViewModel以与我的C#/ MVC应用程序一起使用

在FormAnswer类中,只能有1种答案类型(AnswerCurrency,AnswerDateTime,AnswerBool等),这在数据库和应用程序逻辑中强制执行.

如果存在Answer,则如果所有值都为null,则需要将其映射到FormAnswerModel中的Answer属性,Answer是一个空字符串.

public class FormQuestion
{
   public int Id {get; set;)
   public string DataType {get; set;} 
   public string Question {get; set;} 
}

public class FormAnswer
{
   public int Id {get; set;)
   public int QuestionId {get; set;)
   public double? AnswerCurrency {get;set}
   public dateTime? AnswerDataTime {get;set}
   public bool? AnswerBool {get;set}
   public string AnswerString{get;set}
   public string AnswerText{get;set}
}

public class FormAnswerModel
{
   public int Id {get; set;)
   public int QuestionId {get; set;)
   public string Answer{get;set}
}
Run Code Online (Sandbox Code Playgroud)

c# automapper

5
推荐指数
1
解决办法
8188
查看次数