我在项目中使用 Entity Code First (EF core ver.6)。迁移在 sql server 上正常工作,但在 MariaDb 上不起作用。
我按照这些说明使用了 Pomelo.EntityFrameworkCore.MySql 提供程序 ^,^ 并且还通过这个 guide 使用了 Devart.Data.MySql.EFCore 提供程序所以我可以连接到 MariaDb,但是,当应用程序到达迁移线时出现此错误(
context.Database.Migrate();
):
执行 DbCommand 失败 (2ms) [Parameters=[], CommandType='Text', 命令超时='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) NOT NULL 附近使用的正确语法, 显示名称 nvarchar(max) NOT NULL, IsActive bit NOT NUL...' 在第 4 行
我的实体(用户、角色和用户角色)如下:
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; }
}
角色.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表,出现了上面的错误
我做错了什么?
我看到了这个答案还有这个,但是,模型到所需语法库的转换不应该由提供者完成吗? 还有,为什么创建角色表的时候不出现这个错误呢?