MariaDb错误 – 在'max) NOT NULL'附近使用的正确语法

huangapple go评论76阅读模式
英文:

EF code first with MariaDb Error - the right syntax to use near 'max) NOT NULL

问题

我在项目中使用了 Entity Code First(EF Core 版本6)。迁移在 SQL Server 上可以正常工作,但在 MariaDb 上不起作用。

我按照这些说明使用了 Pomelo.EntityFrameworkCore.MySql 提供程序^,^,并且还使用了 Devart.Data.MySql.EFCore 提供程序,参照了这个 指南。我成功连接到了 MariaDb,但是当应用程序达到迁移行(context.Database.Migrate();)时,我遇到了这个错误:

> 执行 DbCommand 失败(2毫秒)[参数= [],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)
)

> 你的 SQL 语法有错误;请检查与你的 MariaDB 服务器版本相对应的手册,了解使用 'max' 的正确语法。

我的实体(User、Role 和 UserRole)如下:

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; }
}

有趣的是,数据库和 role 和 __efmigrationshistory 表都已经创建,但 user 表没有创建,而且出现了上述错误。

我做错了什么?

我看到了这个答案这个,但是模型到所需语法的转换不应该由提供程序来完成吗?
而且,为什么创建 role 表时没有出现这个错误?

英文:

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&lt;UserRole&gt;();
}

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

Role.cs

public class Role
    {
        public Role()
        {
            UserRoles = new HashSet&lt;UserRole&gt;();
        }

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

        public ICollection&lt;UserRole&gt; 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?

答案1

得分: 2

可能nvarchar(max)是来自先前的现有迁移。您应该删除所有先前的迁移,并根据这个新提供程序创建一个新的迁移。更多信息

英文:

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

huangapple
  • 本文由 发表于 2023年3月7日 20:08:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661791.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定