【发布时间】:2025-12-29 19:10:16
【问题描述】:
我的域类如下:
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Post> Posts { get; set; }
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public Author Author { get; set; }
public Blog Blog { get; set; }
}
如您所见,我绝对没有任何实体框架注释的数据注释或属性,并且我使用实体框架流利的 api 在另一个类中为每个类配置实体框架相关的注释。 现在我想用 MangoDb 替换实体框架。
但在 mongo db 中,我需要在 Id 的列表中放置一个属性,如下所示:
public class Author
{
[BsonElement("_id")]
[BsonRepresentation(BsonType.ObjectId)]
public int Id { get; set; }
public string Name { get; set; }
public IList<Post> Posts { get; set; }
}
我的问题是有什么办法可以在另一个类之外进行此配置,并且不要像我们在实体框架的流利的 api 中使用的那样触摸我的 poco 类。
【问题讨论】:
-
好问题。如果我必须在代码中混合实现特定的属性,我总是觉得很烦人。
标签: c# entity-framework mongodb ef-fluent-api