C# DB将影响性能和存储如果您想使用字符串而不是ObjectId,您也需要在Mongo中将类型从ObjectId直接更改为string。如果您以MongoVue为例研究数据,您将看到数据类型是ObjectId。ID可以是任何东西(甚至是复杂的对象),但在DB和
C# DB将影响性能和存储如果您想使用字符串而不是ObjectId,您也需要在Mongo中将类型从ObjectId直接更改为string。如果您以MongoVue为例研究数据,您将看到数据类型是ObjectId。ID可以是任何东西(甚至是复杂的对象),但在DB和,c#,mongodb,C#,Mongodb,DB将影响性能和存储如果您想使用字符串而不是ObjectId,您也需要在Mongo中将类型从ObjectId直接更改为string。如果您以MongoVue为例研究数据,您将看到数据类型是ObjectId。ID可以是任何东西(甚至是复杂的对象),但在DB和类定义中需要相同的类型。@Pierre Luccinoult:我不同意。您可以注册自定义序列化程序,这是很常见的。此外,C#驱动程序还带有大量类型的内置序列化程序,其中许多类型mongo不知道,包括十进制、堆栈、字典、IP地址,甚至文化信息,请
DB将影响性能和存储如果您想使用字符串而不是ObjectId,您也需要在Mongo中将类型从ObjectId直接更改为string。如果您以MongoVue为例研究数据,您将看到数据类型是ObjectId。ID可以是任何东西(甚至是复杂的对象),但在DB和类定义中需要相同的类型。@Pierre Luccinoult:我不同意。您可以注册自定义序列化程序,这是很常见的。此外,C#驱动程序还带有大量类型的内置序列化程序,其中许多类型mongo不知道,包括
十进制
、堆栈
、字典
、IP地址
,甚至文化信息
,请参见在数据库中使用字符串将影响性能和存储
public class Student
{
public ObjectId Id { get; set; }
public string Name { get; set; }
}
public IEnumerable<Student> GetAll()
{
MongoCollection<Student> collection = Db.GetCollection<Student>("students");
List<Student> students = collection.AsQueryable().ToList();
return students;
}
public IEnumerable<StudentReadDTO> GetAll()
{
MongoCollection<Student> collection = Db.GetCollection<Student>("students");
List<Student> students = collection.AsQueryable().ToList();
return students.Select(p => AutoMapper.Mapper.DynamicMap<StudentReadDTO>(p));
}
// this DTO should be used for POST and PUT (i.e where there is no id or the id
// is part of the URL) - after all, it doesn't make sense to send the id from the
// client to the server
public class StudentDTO
{
public string Name { get; set; }
}
// this should be used by reading operations, where the id is read from the server
// inheritance in DTOs is a source of confusion and can be painful, but this trivial
// type of inheritance will be fine
public class StudentReadDTO : StudentDTO
{
public string Id { get; set; }
}
public class Student
{
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public string Name { get; set; }
}