"无法确定序列化信息"使用Select创建索引时出错

时间:2015-06-10 22:06:09

标签: .net mongodb mongodb-.net-driver mongodb-csharp-2.0

使用MongoDB 2.0 .NET驱动程序时,我在尝试创建索引时收到以下错误:

  

无法确定user =>的序列化信息user.Logins.Select(lg => lg.LoginProvider)。

var loginKeyBuilder = Builders<TUser>.IndexKeys
    .Ascending(user => user.Logins.Select(lg => lg.LoginProvider))
    .Ascending(user => user.Logins.Select(lg => lg.ProviderKey));

_mongoCollection.Indexes.CreateOneAsync(loginKeyBuilder).Wait();

User类如下:

public class MongoIdentityUser : IUser<string>
{
    private List<MongoUserClaim> _claims;
    private List<MongoUserLogin> _logins;

    [BsonConstructor]
    private MongoIdentityUser()
    {
    }

    public MongoIdentityUser(string userName)
    {
        if (userName == null)
        {
            throw new ArgumentNullException(nameof(userName));
        }

        Id = GenerateId(userName);
        UserName = userName;
        _claims = new List<MongoUserClaim>();
        _logins = new List<MongoUserLogin>();
    }

    public MongoIdentityUser(string userName, string email)
        : this(userName)
    {
        if (email == null)
        {
            throw new ArgumentNullException(nameof(email));
        }

        Email = new MongoUserEmail(email);
    }

    [BsonId]
    public string Id { get; private set; }
    public string UserName { get; set; }
    public MongoUserEmail Email { get; private set; }

    public MongoUserPhoneNumber PhoneNumber { get; private set; }
    public string PasswordHash { get; private set; }
    public string SecurityStamp { get; private set; }
    public bool IsLockoutEnabled { get; private set; }
    public bool IsTwoFactorEnabled { get; private set; }

    public IEnumerable<MongoUserClaim> Claims
    {
        get
        {
            return _claims;
        }

        private set
        {
            if (_claims == null)
            {
                _claims = new List<MongoUserClaim>();
            }

            if (value != null)
            {
                _claims.AddRange(value);
            }
        }
    }
    public IEnumerable<MongoUserLogin> Logins
    {
        get
        {
            return _logins;
        }

        private set
        {
            if (_logins == null)
            {
                _logins = new List<MongoUserLogin>();
            }

            if (value != null)
            {
                _logins.AddRange(value);
            }
        }
    }
}

MongoUserLogin上课:

public class MongoUserLogin : IEquatable<MongoUserLogin>, IEquatable<UserLoginInfo>
{
    [BsonConstructor]
    private MongoUserLogin()
    {
    }

    public MongoUserLogin(UserLoginInfo loginInfo)
    {
        if (loginInfo == null)
        {
            throw new ArgumentNullException(nameof(loginInfo));
        }

        LoginProvider = loginInfo.LoginProvider;
        ProviderKey = loginInfo.ProviderKey;
    }

    public string LoginProvider { get; private set; }
    public string ProviderKey { get; private set; }

    public bool Equals(MongoUserLogin other)
    {
        return (other.LoginProvider.Equals(LoginProvider) && other.ProviderKey.Equals(ProviderKey));
    }

    public bool Equals(UserLoginInfo other)
    {
        return (other.LoginProvider.Equals(LoginProvider) && other.ProviderKey.Equals(ProviderKey));
    }
}

我还有以下全局注册的公约和序列化:

var pack = new ConventionPack
{
    new CamelCaseElementNameConvention(),
    new IgnoreIfNullConvention(false)
};

ConventionRegistry.Register("all", pack, type => true);

BsonSerializer.UseNullIdChecker = true;

知道可能出现什么问题吗?

0 个答案:

没有答案