.net core 我可以在RavenDB中使用构造函数重载吗?

.net core 我可以在RavenDB中使用构造函数重载吗?,.net-core,ravendb,.net Core,Ravendb,我一直试图编写一个智能解决方案来解决我的问题,但失败了。我需要在我的域中以我的身份进行构造函数重载,首先是一个对象实例,另一个用于RavenDB填充。这没什么大不了的,对吧?但是RavenDB不能使用多个构造函数,消息显示: Message=“找不到用于类型的构造函数 Project.Domain.Business.Users.User。类应具有 默认构造函数,一个带参数的构造函数或一个构造函数 用JsonConstructor属性标记。路径为“登录” 这样做的目的是在我的身份中保留一些域规则,

我一直试图编写一个智能解决方案来解决我的问题,但失败了。我需要在我的域中以我的身份进行构造函数重载,首先是一个对象实例,另一个用于RavenDB填充。这没什么大不了的,对吧?但是RavenDB不能使用多个构造函数,消息显示:

Message=“找不到用于类型的构造函数 Project.Domain.Business.Users.User。类应具有 默认构造函数,一个带参数的构造函数或一个构造函数 用JsonConstructor属性标记。路径为“登录”

这样做的目的是在我的身份中保留一些域规则,这在有人创建对象实例时是必要的。我们如何在以下示例中看到:

我的身份用户:

namespace Project.Domain.Business.Users
{
    public class User
    {
        public string Id { get; private set; }
        public string Login { get; private set; }
        public string Password { get; private set; }
        public string Role { get; private set; }
        public string Token { get; private set; }
        public string RecoveryToken { get; private set; }
        public string GoogleClientId { get; private set; }
        public string GoogleClientSecret { get; private set; }
        public string FacebookAppId { get; private set; }
        public string FacebookAppSecret { get; private set; }
        public Person Person { get; private set; }

        public User(string login, string password, string googleClientId, string googleClientSecret, string facebookAppId, string facebookAppSecret, Person person)
        {
            Id = Guid.NewGuid().ToString();
            Login = login;
            Password = SecurityHolder.EncryptPassword(password);
            Role = "Common";
            Token = SecurityHolder.GetHash_HMACSHA256(login + "_" + password);
            RecoveryToken = SecurityHolder.GetHash_HMACSHA256(login);
            GoogleClientId = googleClientId;
            GoogleClientSecret = googleClientSecret;
            FacebookAppId = facebookAppId;
            FacebookAppSecret = facebookAppSecret;
            Person = person;
        }

    }
}
在“我的服务”中创建的对象(例如,可以在控制器中创建):

我在考虑使用创造性设计模式来创建我的对象的实例,可能是工厂,并将标识设置为公共。因此,RavenDB也应该很好地工作。但是,我不知道这是不是正确的方法

编辑:

就像加雷问我的那样。创建方法在UserRepository中实现:

public class UserRepository : IUserRepository
{
    private readonly IUnitOfWork _unitOfWork;

    public UserRepository(IUnitOfWork unitOfWork)
    {
        _unitOfWork = unitOfWork;
    }

    public User GetById(string id)
    {
        var session = _unitOfWork.OpenSession();
        return session.Load<User>(id);
    }

    public void Create(User user)
    {
        var session = _unitOfWork.OpenSession();
        session.Store(user);
        session.SaveChanges();
    }

    public void Edit(User user)
    {
        var session = _unitOfWork.OpenSession();
        session.SaveChanges();
    }
}

经过研究,我找到了解决办法。可以在RavenDB的构造函数中使用标记
[Json构造函数]
,将其序列化

例如:

    [JsonConstructor]
    public User(string id, string login, string password, string role, string token, string recoveryToken, string googleClientId, string googleClientSecret, string facebookAppId, string facebookAppSecret, Person person)
    {
        Id = id;
        Login = login;
        Password = password;
        Role = role;
        Token = token;
        RecoveryToken = recoveryToken;
        GoogleClientId = googleClientId;
        GoogleClientSecret = googleClientSecret;
        FacebookAppId = facebookAppId;
        FacebookAppSecret = facebookAppSecret;
        Person = person;
    }
因此,它允许使用构造函数重载


请阅读文档中的更多内容

你说的例外不是来自RavenDB,我也没有看到任何RavenDB客户端代码。RavenDB接收初始化的实体,它不关心使用什么ctor来创建它。Create()方法中有什么?好的,我将编辑并显示Create()方法。
public class UnitOfWork : IUnitOfWork
{
    private readonly IDocumentStore _documentStore;
    private IDocumentSession _documentSession;

    public UnitOfWork(IDocumentStoreHolder documentStoreHolder)
    {
        _documentStore = documentStoreHolder.DocumentStore;
        _documentSession = null;
    }

    public IDocumentSession OpenSession()
    {
        return _documentSession ?? (_documentSession = _documentStore.OpenSession());
    }
}
    [JsonConstructor]
    public User(string id, string login, string password, string role, string token, string recoveryToken, string googleClientId, string googleClientSecret, string facebookAppId, string facebookAppSecret, Person person)
    {
        Id = id;
        Login = login;
        Password = password;
        Role = role;
        Token = token;
        RecoveryToken = recoveryToken;
        GoogleClientId = googleClientId;
        GoogleClientSecret = googleClientSecret;
        FacebookAppId = facebookAppId;
        FacebookAppSecret = facebookAppSecret;
        Person = person;
    }