无法确定i =>的序列化信息. i.Id使用接口 [英] Unable to determine the serialization information for i => i.Id using interfaces

查看:106
本文介绍了无法确定i =>的序列化信息. i.Id使用接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我知道已经有关于此错误消息的问题了,但是我还没有发现任何与在这种类型的查询中使用接口有关的问题.

First up, I know there is already questions out there with this error message, but I haven't found any that relate to the use of using interfaces with this type of query.

我目前正在尝试使用C#驱动程序2.0更新MongoDB实体.但是,在尝试构建查询时出现错误(我假设它是代码的Builders<T>.Filter.Eq(i => i.Id, entity.Id)位),并且出现以下错误:

I'm currently trying to update a MongoDB entity using the C# Driver 2.0. However, I'm getting an error when it's trying to build the query (I'm assuming it's the Builders<T>.Filter.Eq(i => i.Id, entity.Id) bit of code) and I'm getting the following error:

无法确定i => i.Id的序列化信息.

Unable to determine the serialization information for i => i.Id.

我正在尝试更新以下课​​程

I've got the following class that I'm trying to update

public interface IEntity {
    string Id { get; set; }
}

public interface ITrack : IEntity {
    string Name { get; set; }
}

public class TrackDTO : ITrack
{

    [BsonId]
    public string Id { get; set; }

    public string Name { get; set; }

}

然后,我在通用DAO类中使用以下方法使用该接口将对象保存到数据库中,以替换整个文档.请注意,在下面的示例中,T编码为ITrack(即TrackDao = new Dao<ITrack>),但是当对象在运行时传递时,它是一个TrackDTO对象(正确):

I'm then using the interface to save the object into the database using the following method in a generic DAO class to replace the entire document. Note, in the example below T is coded as ITrack (i.e. TrackDao = new Dao<ITrack>) but when the object is passed in at runtime it's a TrackDTO object (which is correct):

public async Task<T> Save(T entity)
{
    // Save the entity to the collection.
    ReplaceOneResult result = await _collection.ReplaceOneAsync(Builders<T>.Filter.Eq(i => i.Id, entity.Id), entity, new UpdateOptions() { IsUpsert = true });

    // return the saved user object.
    return entity;
}

我不知道我的IEntity类的Id属性是否也需要[BsonId]属性,但是我想尽可能避免这种情况,因为我想保留我的模型层(在IEntity驻留),没有任何特定于数据库平台的引用.

I don't know if the Id property of my IEntity class also requires the [BsonId] attribute, but I'd like to avoid this if possible, as I want to keep my model layer (where IEntity resides) free of any database platform specific references.

我还尝试添加以下也无效的类映射:

I've also tried adding the following class map which has had no effect either:

BsonClassMap.RegisterClassMap<TrackDTO>(cm =>
{
    cm.AutoMap();
    cm.MapMember(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId));
    cm.SetIdMember(cm.GetMemberMap(c => c.Id));
});

出于与在模型层中没有[BsonId]属性相同的原因,我不想使用引用DTO对象的[BsonKnownTypes]装饰的Model类,但是我不介意这是否需要作为类映射的一部分出现.

For the same reason as not having the [BsonId] attributes in the Model layer, I don't want to have the Model classes decorated with [BsonKnownTypes] that reference DTO objects, however I don't mind if this needs to occur as part of a class map.

推荐答案

  1. 对于

无法确定i => i.Id的序列化信息."

"Unable to determine the serialization information for i => i.Id."

尝试使用:nameof().

Builders<T>.Filter.Eq(nameof(IEntity.Id), entity.Id)

2.

...但是我想尽可能避免这种情况,因为我想保留我的模型 层(IEntity所在的地方)没有任何特定于数据库的平台 参考.

...but I'd like to avoid this if possible, as I want to keep my model layer (where IEntity resides) free of any database platform specific references.

我对类似问题的解决方案:

My solution for problem like your and similar problems:

public interface IEntity {
    [BsonId]
    string Id { get; set; }

    string IdEntity { get; set; }
}

[BsonIgnoreExtraElements(Inherited = true)]
public abstract class BaseEntity : IEntity 
{
    [BsonRepresentation(BsonType.ObjectId)]
    public virtual string Id { get; set; }

    [BsonIgnoreIfNull]
    public string IdEntity { get; set; }
}

这篇关于无法确定i =&gt;的序列化信息. i.Id使用接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆