Asp.net core Cosmos DB更新失败无法将Guid强制转换为字符串

Asp.net core Cosmos DB更新失败无法将Guid强制转换为字符串,asp.net-core,entity-framework-core,azure-cosmosdb,Asp.net Core,Entity Framework Core,Azure Cosmosdb,试图更新ASP.NET core 3.1中的Cosmos DB记录。但更新失败,并显示以下消息:“无法将类型为的System.Func'2[Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry,System.Guid]的对象强制转换为类型为'System.Func'2[Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntity

试图更新ASP.NET core 3.1中的Cosmos DB记录。但更新失败,并显示以下消息:“无法将类型为
的System.Func'2[Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry,System.Guid]的对象强制转换为类型为'System.Func'2[Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry,System.String]”

该错误发生在saveCangesAsync()上; 简化后,代码如下所示:

// The service
public async Task<Todo> UpdateAsync(Todo entity)
{
  var response = ctx.Todos.Update(entity);
  await ctx.SaveChangesAsync(); // Error here
  return response.Entity;
}

// The entity Todo
public class Todo
{
  public Guid id { get; set; }
  [Required(ErrorMessage = "Description is required")]
  public string description { get; set; }
  ...
}

// The context
public class TodoDbContext : DbContext
{
    public DbSet<Todo> Todos { get; set; }
    public TodoDbContext(DbContextOptions<TodoDbContext> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.HasDefaultContainer("Todos");
    }
}

// The controller
[HttpPut]
public async Task<IActionResult> Put(Todo todo)
{
    try
    {
        if (ModelState.IsValid)
        {
            Todo td = await service.GetAsync(todo.id.ToString());

            if (td != null)
            {
                td.description = todo.description;
                var response = await service.UpdateAsync(td);
                return Ok(response);
            }

            return BadRequest("Not found.");
        }
        else
        {
            return BadRequest(ModelState);
        }
    }
    catch (Exception ex)
    {
        return BadRequest(ex.Message);  // Exception here
    }
}
//服务
公共异步任务UpdateAsync(Todo实体)
{
var响应=ctx.Todos.Update(实体);
等待ctx.saveChangesSync();//此处出错
返回响应。实体;
}
//实体待办事项
公共课待办事项
{
公共Guid id{get;set;}
[必需(ErrorMessage=“需要说明”)]
公共字符串说明{get;set;}
...
}
//上下文
公共类TodoDbContext:DbContext
{
公共DbSet Todos{get;set;}
public-TodoDbContext(DbContextOptions选项):基本(选项)
{
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.hasdaultcontainer(“Todos”);
}
}
//控制器
[HttpPut]
公共异步任务Put(Todo Todo)
{
尝试
{
if(ModelState.IsValid)
{
Todo td=await service.GetAsync(Todo.id.ToString());
如果(td!=null)
{
td.description=todo.description;
var response=wait service.UpdateAsync(td);
返回Ok(响应);
}
返回错误请求(“未找到”);
}
其他的
{
返回请求(ModelState);
}
}
捕获(例外情况除外)
{
return BadRequest(ex.Message);//此处异常
}
}
我可以插入、读取但不能更新,所以下面的代码运行良好(也有Guid)

公共异步任务CreateAsync(Todo实体)
{
entity.id=Guid.NewGuid();
var response=await ctx.Todos.AddAsync(实体);
等待ctx.saveChangesSync();
返回响应。实体;
}

感谢您的帮助!

不要使用“id”,这会与自动生成的id冲突。请使用其他属性或使用“id”(大写字母I)来解决此问题。

是否有任何理由使用Guid作为id类型?在Todo类中,您可以这样编写:

public class Todo
{
    [JsonProperty("id")
    public string Id { get; set; }

    [JsonProperty("description")
    [Required(ErrorMessage = "Description is required")]
    public string description { get; set; }
}
我假设您使用的是Cosmos DB SDK的v3,然后使用Guid.NewGuid().ToString()将id设置为Guid(但作为字符串类型)

public class Todo
{
    [JsonProperty("id")
    public string Id { get; set; }

    [JsonProperty("description")
    [Required(ErrorMessage = "Description is required")]
    public string description { get; set; }
}