gpt4 book ai didi

c# - MongoRepository 继承序列化错误

转载 作者:IT老高 更新时间:2023-10-28 13:11:51 26 4
gpt4 key购买 nike

在尝试将继承与 MongoRepository for C# 结合时,我遇到了序列化错误。

真正奇怪的是它可以在很短的时间内工作,但是在说重建或其他事情之后它就失败了。如果我删除该集合并创建一个新集合,它将一直有效,直到停止或重建。

我的代码如下:

public class Organization
{
// other attributes removed for demonstration simplicity

public List<Person> People { get;set; }
}

public abstract class Person
{
public string Id {get;set;}
public string Name {get;set;}
}

public class Employee : Person
{
public string Badge {get;set;}
}

public class Contractor : Person
{
public string Company {get;set;}
}

当我尝试得到它时:

static MongoRepository<Organization> apps = new MongoRepository<Organization>();
return apps.Single(c => c.Id == id);

我收到的错误是:

An exception of type 'System.IO.FileFormatException' occurred in MongoDB.Driver.dll but was not handled in user code

Additional information: An error occurred while deserializing the People property of class API.Models.Organization: Instances of abstract classes cannot be created.

最佳答案

添加装饰器属性:

[BsonKnownTypes(typeof(Contractor), typeof(Employee))]

类解决了这个问题。

关于c# - MongoRepository 继承序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20497165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com