简体   繁体   English

获取动态 object 而不是从 .net 核心 Z240AA2CEC4B29C56F3BEE520A8DCEE7 中的 MongoDB 强类型

[英]Getting dynamic object instead of strong typed from MongoDB in .net core c#

I try to use MongoDB in combination with .net core (c#) to save some survey results.我尝试将 MongoDB 与 .net 内核(c#)结合使用来保存一些调查结果。

The challenge is that I plan to make it as generic as possible to be able to add other rating controls later.挑战在于我计划使其尽可能通用,以便以后能够添加其他评级控件。

I am able to save different result types in one table.我能够在一张表中保存不同的结果类型。

public class VoteBase
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }
    public User User { get; set; }
    public Control Control { get; set; }
    public string ControlType { get; set; }
}



public class VoteStarRatingControl : VoteBase
{
    public int? Rating { get; set; }
}



public class VoteStarRatingWithComment : VoteStarRatingControl
{
    public string Comment { get; set; }
}

I created a table of base type as MongoCollection:我创建了一个基本类型为 MongoCollection 的表:

        private readonly IMongoCollection<VoteBase> _voteBases;

To save it to that collection I used this code (where the DTO is same content datatransfer object to decouple the REST service from DB structure):为了将其保存到该集合,我使用了以下代码(其中 DTO 是相同的内容数据传输 object 以将 REST 服务与 DB 结构分离):

List<VoteBase> dbVotes = new List<VoteBase>();

foreach (VoteBaseDTO v in votes)
{
    switch (v) {
        case VoteStarRatingWithCommentDTO src:
            dbVotes.Add(new VoteStarRatingWithComment() { User = new User() { Id = UserId }, Control = new Control() { Id = src.ControlId }, Rating = src.Rating, Comment = src.Comment });
            break;
        case VoteStarRatingControlDTO sr:
            dbVotes.Add(new VoteStarRatingControl() { User = new User() { Id = UserId }, Control = new Control() { Id = sr.ControlId }, Rating = sr.Rating });
            break;
    }

}

_voteBases.InsertMany(dbVotes);

return dbVotes;

Until here all works fine.直到这里一切正常。

Now I try to get the votes back for a specific list of controls (for one survey).现在,我尝试为特定的控件列表(针对一项调查)收回选票。

The following command fails with以下命令失败

'Element 'Rating' does not match any field or property of class SurveyToolRestAPI.Models.VoteBase.' '元素'评级'与 class SurveyToolRestAPI.Models.VoteBase 的任何字段或属性都不匹配。

 object obj = _voteBases.Find(vb => vb.Control.Id == "5e9c24c50a099728b027e176").SingleOrDefault();

This is because it is of Type StarRatingControl instead of type VoteBase.这是因为它是 StarRatingControl 类型而不是 VoteBase 类型。

Is there a way to get the list as dynamic instead of strong typed from a MongoDB collection?有没有办法从 MongoDB 集合中获取动态列表而不是强类型列表?

Thanks Eldho, you pointed me to the right direction.谢谢Eldho,您为我指明了正确的方向。

Doings some more research with the Bson... attributes I found the solution finally in this post Mongodb collection as dynamic使用 Bson 进行更多研究...属性我最终在这篇文章Mongodb 集合中找到了解决方案作为动态

The key is to add a BsonKnownTypes attribute.关键是添加一个 BsonKnownTypes 属性。 The it can be used as strong typed collection and the list will contain the appropriate subtypes.它可以用作强类型集合,并且列表将包含适当的子类型。

    [BsonKnownTypes(typeof(VoteBase), typeof(VoteStarRatingControl), typeof(VoteStarRatingWithComment))]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM