c# - 当我在基类型中拥有另一个属性的 BsonId 属性时,有没有办法在派生类中使用“Id”属性?
问题描述
我可以在基类中使用 Id 属性,但是当我不想在派生类中执行此操作时,我会收到错误消息“'ConsoleApp1.DerivedWithId' 类型的属性 'Id' 不能使用元素名称 '_id'因为它已经被“ConsoleApp1.BaseWithoutId”类型的属性“IdForMongo”使用下面是一个控制台应用程序来说明
using System;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//This will work
BsonClassMap.LookupClassMap(typeof(DerivedWithoutId));
//But this will throw error: The property 'Id' of type 'ConsoleApp1.DerivedWithId' cannot use element name '_id' because it is already being used by property 'IdForMongo' of type 'ConsoleApp1.BaseWithoutId
BsonClassMap.LookupClassMap(typeof(DerivedWithId));
}
}
public class BaseWithoutId
{
[BsonId]
public Guid IdForMongo{ get; set; }
}
public class BaseWithId
{
[BsonId]
public Guid IdForMongo { get; set; }
public Guid Id { get; set; }
}
public class DerivedWithId : BaseWithoutId
{
public Guid Id { get; set; }
public string Description { get; set; }
}
public class DerivedWithoutId : BaseWithId
{
public string Description { get; set; }
}
}
解决方案
我认为您可以在派生类中使用 [BsonNoId] 属性。
[BsonNoId]
public class DerivedWithId : BaseWithoutId
{
public Guid Id { get; set; }
public string Description { get; set; }
}
推荐阅读
- python - 如何从 Open3d 中提取图片?
- typescript - 在 typescript 中引用 VSS.SDK。错误:未定义 VSS
- flutter - 如何在 ShowDialog 上制作样式小部件?
- typescript - 必须在 className 中使用解构道具分配
- ios - Firebase:Flutter 电话身份验证不起作用并且应用程序崩溃
- python - 将数据框写入 txt 文件
- javascript - 如何防止从“私有”模块导入?
- java - Java:将日期拆分为 n 部分
- ios - UITableView 在设备旋转后保持滚动位置
- amazon-s3 - 将 RDS 数据增量传输到 Parquet 中的 s3