Duplicate Username - Entity Framework

Asked

Viewed 31 times

0

I am in an ongoing project, today has already been installed the Entity framework. But what happened, it doesn’t let add users with the same name, which makes no sense, since they can have equal names.

To insert is called this code:

  var resultado = _userManager.CreateAsync(user, usuario.Password).Result;

And if there is already a user with this name, it returns an error:

user name 'name' is already Taken.

Is there any way around that, or remove that from letting only once include the same name? If I add a character just to insert, and then give an update, it may cause some problem ??

  • This user and the "class" login in EF, you can not have 2 equal logins in the system, if what you want and the user’s Fullname why not create a class for it?

  • @João ah yes, the project that was done this way, in this case just change, at first, I do a check, saved, and then update to let include with the same name, was a way I found, I do not know if it is the best, but it was the way I found quickly.

1 answer

0

The ideal is for you to make a getByEmail(email) and validate if it already exists. Example:

    public Task<bool> CheckEmailInUse(string email, int? ignoreId)
    {
        return dbSet.Where(w => w.Email == email)
                    .Where(w => (!ignoreId.HasValue || ignoreId.Value != w.Id))
                    .AnyAsync();
    }

with this you can ignore the id of the same user.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.