如何将此代码从使用 EF db 上下文更新到通用存储库?

How to update this code from using the EF db context to a generic repository?

重新审视使​​用 MembershipProvider 创建用户时的一些旧代码,我发现使用以下代码无法创建新用户:

// the call
Membership.CreateUser(user.Username, user.Password, user.EmailAddress, null, null, false, null, out createStatus);


    public override MembershipUser CreateUser(string username,
       string password, string email, string passwordQuestion,
       string passwordAnswer, bool isApproved,
       object providerUserKey, out MembershipCreateStatus status)
    {

        using (var db = new DbV001Context())
        {
            var crypto = new SimpleCrypto.PBKDF2();
            var encryptedPw = crypto.Compute(password);
            var user = db.Users.Create();

            user.MembershipTypeId = 7; 
            user.UserName = username;
            user.Email = email;
            user.Password = encryptedPw;
            user.PasswordSalt = crypto.Salt;
            //user.CreateDate = DateTime.Now; // set automatically in the db

            //user.CreateDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastLoginDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastActivityDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastPasswordChangedDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastLockoutDate = new DateTime(1980, 01, 01, 0, 0, 0, 0);

            db.Users.Add(user);
            try
            {
                db.SaveChanges();
            } ...

我在实现 repository and UoW pattern 之前编写了这部分代码,我在其他地方都使用它:

    private readonly IUnitOfWork _unitOfWork;
    private readonly IRepository<User> _repository;

    public AccountController() { }

    public AccountController(IUnitOfWork unitOfWorkAsync,
        IRepository<User> repository)
    {
        _unitOfWork = unitOfWorkAsync;
        _repository = repository;
    }

   .
   .
   .
                var userEntity = _repository
                    .ODataQueryable()
                    .Where(u => (u.Email == user.EmailAddress | u.UserName == user.Username)
                                && u.Email != null).ToList();

我尝试了以下操作,但是当我尝试 Insert():

时总是出现异常
public class CustomMembershipProvider : MembershipProvider
{
    //private DbV001Context _db = new DbV001Context();
    private readonly IUnitOfWork _unitOfWork;
    private readonly IRepository<User> _repository;

    public CustomMembershipProvider() { }

    public CustomMembershipProvider(IUnitOfWork unitOfWorkAsync,
        IRepository<User> repository)
    {
        _unitOfWork = unitOfWorkAsync;
        _repository = repository;
    }
    public override MembershipUser CreateUser(string username,
       string password, string email, string passwordQuestion,
       string passwordAnswer, bool isApproved,
       object providerUserKey, out MembershipCreateStatus status)
    {

        //using (var db = new DbContext())
        //{
            var crypto = new SimpleCrypto.PBKDF2();
            var encryptedPw = crypto.Compute(password);
            //var user = db.Users.Create();
            var user = new User();

            user.MembershipTypeId = 7; 
            user.UserName = username;
            user.Email = email;
            user.Password = encryptedPw;
            user.PasswordSalt = crypto.Salt;
            //user.CreateDate = DateTime.Now; // set automatically in the db

            //user.CreateDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastLoginDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastActivityDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastPasswordChangedDate = new DateTime(1980,01,01,0,0,0,0);
            user.LastLockoutDate = new DateTime(1980, 01, 01, 0, 0, 0, 0);


            //db.Users.Add(user);
            try
            {
                //db.SaveChanges();
                _repository.InsertGraph(user);  //Object reference not set to an instance of an object.
            }

我已经有一段时间没看过这些东西了,我该如何更新上面的代码以使用存储库,而不是数据库上下文?另外,由于我使用的是 DI,我在设置 CustomMembershipProvider 时是否遗漏了什么?

当我单步执行代码时,CustomMembershipProvider 构造函数没有被执行。将 Autofac 用于 DI。

我认为成员资格提供程序不允许您在构造函数中提供参数...在您的无参数构造函数中,您可以将当前实例分配给 UOW 和存储库,或者如果尚未完成则激活它们。 .

但这取决于您使用的 DI 框架。