0

Problem with EF Core database:

Failed executing DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30']

DECLARE @var0 sysname;
SELECT @var0 = [d].[name]
FROM [sys].[default_constraints] [d]
INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
WHERE ([d].[parent_object_id] = OBJECT_ID(N'[User]') AND [c].[name] = N'PasswordSalt');
IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [User] DROP CONSTRAINT [' + @var0 + '];');
ALTER TABLE [User] ALTER COLUMN [PasswordSalt] varbinary(16) NULL;

If I have string password on user model everything working fine, but if I want to change it to byte[], I see upper error.

public class User
{
    [Key]
    public Guid UserId { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int RoleId { get; set; } //admin czy nie
    public string PhoneNo { get; set; }
    public string Login { get; set; }
    [MaxLength(16)]
    public byte[] PasswordHash { get; set; }
    [MaxLength(16)]
    public byte[] PasswordSalt { get; set; }
}

I tried just convert to byte in this method

public Models.User.User Authenticate(string email, string password)
{
        if (string.IsNullOrEmpty(email) || (string.IsNullOrEmpty(password)))
        {
            return null;
        }

        throw new NotImplementedException();

        var user = _dbContext.Set<Models.User.User>().SingleOrDefault(x => x.Email == email);

        // check if customer with email exist
        if (user == null)
        {
            return null;
        }

        byte[] array = Encoding.ASCII.GetBytes(user.PasswordHash);
        byte[] array2 = Encoding.ASCII.GetBytes(user.PasswordSalt);

        // check if password is correct
        if (!VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
        {
            return null;
        }

        return user;
}

But it's not working and I need byte[] data. Can you help?

1
  • Can you tell us when and which line of code will show this error? According to your code, I tried to create sample, it seems that we could store the password via the byte array, so, I'm not sure which line of the above code will cause this issue. Commented Apr 19, 2021 at 7:44

1 Answer 1

0

You have a throw exception it this cause your error

You see this image of your code.

image where you'll see the error

If you don't erase this line your code throw exception

Sign up to request clarification or add additional context in comments.

2 Comments

It's not this, I debug program and it failing during migrate function private static async Task Migrate(IApplicationBuilder app) { using var serviceScope = app.ApplicationServices.GetService<IServiceScopeFactory>().CreateScope(); var context = serviceScope.ServiceProvider.GetRequiredService<DbContext>(); await context.Database.MigrateAsync(); } with this One or more errors occurred. (Implicit conversion from data type nvarchar(max) to varbinary is not allowed. Use the CONVERT function to run this query.)'
how are you db table definition and you are migration definition? you should write me your code here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.