0

I use Entity Code First (EF core ver.6) in a project. The migration work correctly on the sql server, but it doesn't work on the MariaDb.

I used the Pomelo.EntityFrameworkCore.MySql provider by these instructions ^,^ and also used the Devart.Data.MySql.EFCore provider by this guide So I could connect to the MariaDb, However, I got this error when the app reach to migration line( context.Database.Migrate();):

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

CREATE TABLE Users ( 
  Id int AUTO_INCREMENT UNIQUE NOT NULL,
  Username nvarchar(450) NOT NULL,
  `Password` nvarchar(max) NOT NULL,
  DisplayName nvarchar(max) NOT NULL,
  IsActive bit NOT NULL,
  LastLoggedIn datetime NULL,      
  PRIMARY KEY (Id)
)

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'max) NOT NULL, DisplayName nvarchar(max) NOT NULL, IsActive bit NOT NUL...' at line 4

My Entities (User, Role and UserRole) are as follows:

User.cs

public class User

{
public User()
{
    UserRoles = new HashSet<UserRole>();
}

public int Id { get; set; }
[MaxLength(50, ErrorMessage = "")]
public string Username { get; set; }
[MaxLength(50, ErrorMessage = "")]
public string Password { get; set; }
[MaxLength(50, ErrorMessage = "")]
public string DisplayName { get; set; }
public bool IsActive { get; set; }
public DateTime? LastLoggedIn { get; set; }  
public ICollection<UserRole> UserRoles { get; set; }
}

Role.cs

public class Role
    {
        public Role()
        {
            UserRoles = new HashSet<UserRole>();
        }

        public int Id { get; set; }
        [MaxLength(50, ErrorMessage = "")]
        public string Name { get; set; }

        public ICollection<UserRole> UserRoles { get; set; }
    }

UserRole.cs:

public class UserRole
    {
        public int UserId { get; set; }
        public int RoleId { get; set; }

        public User User { get; set; }
        public Role Role { get; set; }
    }

Interestingly, the database is created and the role and __efmigrationshistory tables are also created, but the user table is not created and the above error occurs.

What did I do wrong?

I saw this answer and also this, However, Shouldn't the conversion of the models to the desired syntax bank be done by the providers? Also, why does this error not appear when creating the role table?

Arani
  • 891
  • 7
  • 18
  • NEVER store passwords in plain in a database. Use salted hashes instead. – Klaus Gütter Mar 07 '23 at 12:30
  • Thanks @KlausGütter. Please don't worry about it, I use the salted hash mechanism to hashing the password, and then I store it in the db as string format. – Arani Mar 07 '23 at 12:33
  • why do you need ` before and after password? – Stalidald Mar 07 '23 at 13:40
  • Migrations are not database agnostic. You cannot use SQL Server migrations on MariaDb. – Svyatoslav Danyliv Mar 07 '23 at 18:59
  • What is unclear with the error message? `check the manual that corresponds to your MariaDB server version for the right syntax to use` -> https://mariadb.com/kb/en/varchar/ – Georg Richter Mar 07 '23 at 19:35
  • @GeorgRichter, [Polite] The issue is that I do not have access to the provider. A series of classes and models have been written, and according to the capabilities of the ORM (EntityFrameWork), these classes are expected to be converted into the syntax of the MariaDb by the provider. Therefore, I am not involved in the production of the syntax of MariaDb that I'd like to modify to prevent this error from occurring. – Arani Mar 11 '23 at 05:06
  • @SvyatoslavDanyliv, [Polite] I don't use SQL Server migration. I use the MySql Migration with MariaDB Provider. – Arani Mar 11 '23 at 05:07
  • @Stalidald, That script is generated by the MySQL provider itself and appears as such in the error text. – Arani Mar 11 '23 at 05:09
  • 1
    Delete all of the previous migrations and create a new migration: https://github.com/PomeloFoundation/Pomelo.EntityFrameworkCore.MySql/issues/865 – VahidN Mar 11 '23 at 06:35
  • @VahidN, That's great. It worked for me too. Thank you very much. It is exactly my answer. Please give this as the answer if you'd like. – Arani Mar 11 '23 at 07:03

1 Answers1

2

Probably nvarchar(max) comes form the previous existing migrations. You should delete all of the previous migrations and create a new migration based on this new provider. More info

VahidN
  • 18,457
  • 8
  • 73
  • 117