我试图为我的问题找到解决方案,但到目前为止我的努力都是徒劳的。:-(
我使用 Visual Studio 2010、.NET Framework 4、C#、Entity Framework 5.0、MySQL 5.5 及其对应的 .NET 连接器(版本 6.5.4)创建了一个 Web 项目。我对实体和 O/R 映射使用代码优先方法。
我面临的问题是我无法执行看似简单的迁移。这是我的实体类:
public class Usuario
{
public int Id { get; set; }
[Required]
[StringLength(100)]
public string NomeCompleto { get; set; }
[Required]
[StringLength(100)]
[DataType(DataType.EmailAddress)]
public string Email { get; set; }
[Required]
[StringLength(30)]
public string Login { get; set; }
[Required]
[StringLength(64)]
public string Senha { get; set; }
[Required]
public bool Ativo { get; set; }
//[Timestamp]
[ConcurrencyCheck]
public int Versao { get; set; }
}
public class Perfil
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Nome { get; set; }
[StringLength(100)]
public string Descricao { get; set; }
//[Timestamp]
[ConcurrencyCheck]
public int Versao { get; set; }
public virtual ICollection<Usuario> Usuarios { get; set; }
public virtual ICollection<Permissao> Permissoes { get; set; }
}
public class Permissao
{
public int Id { get; set; }
[Required]
[StringLength(50)]
public string Nome { get; set; }
[StringLength(100)]
public string Descricao { get; set; }
//[Timestamp]
[ConcurrencyCheck]
public int Versao { get; set; }
public virtual ICollection<Perfil> Perfis { get; set; }
}
Add-Migration Acesso
由(唯一Up()
方法)生成的代码:
public partial class Acesso : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Usuario",
c => new
{
Id = c.Int(nullable: false, identity: true),
NomeCompleto = c.String(nullable: false, storeType: "mediumtext"),
Email = c.String(nullable: false, storeType: "mediumtext"),
Login = c.String(nullable: false, storeType: "mediumtext"),
Senha = c.String(nullable: false, storeType: "mediumtext"),
Ativo = c.Boolean(nullable: false),
Versao = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.Perfil",
c => new
{
Id = c.Int(nullable: false, identity: true),
Nome = c.String(nullable: false, storeType: "mediumtext"),
Descricao = c.String(storeType: "mediumtext"),
Versao = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.Permissao",
c => new
{
Id = c.Int(nullable: false, identity: true),
Nome = c.String(nullable: false, storeType: "mediumtext"),
Descricao = c.String(storeType: "mediumtext"),
Versao = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id);
CreateTable(
"dbo.PerfilPermissao",
c => new
{
PerfilId = c.Int(nullable: false),
PermissaoId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.PerfilId, t.PermissaoId })
.ForeignKey("dbo.Perfil", t => t.PerfilId, cascadeDelete: true)
.ForeignKey("dbo.Permissao", t => t.PermissaoId, cascadeDelete: true)
.Index(t => t.PerfilId)
.Index(t => t.PermissaoId);
CreateTable(
"dbo.UsuarioPerfil",
c => new
{
UsuarioId = c.Int(nullable: false),
PerfilId = c.Int(nullable: false),
})
.PrimaryKey(t => new { t.UsuarioId, t.PerfilId })
.ForeignKey("dbo.Usuario", t => t.UsuarioId, cascadeDelete: true)
.ForeignKey("dbo.Perfil", t => t.PerfilId, cascadeDelete: true)
.Index(t => t.UsuarioId)
.Index(t => t.PerfilId);
}
}
首先,我必须将名为 Versao (version) 的属性从
[Timestamp]
public byte[] Versao { get; set; }
到
[ConcurrencyCheck]
public int Versao { get; set; }
因为在更改之前发生了错误(关于类型 rowversion 的某些事情没有被命名空间或别名限定)。进行此更改后,我能够生成迁移,但Update-Database
命令失败,控制台中显示以下错误:
System.FormatException: Cadeia de entrada não estava em um formato incorreto.
em System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
(输入字符串的格式不正确。)
我尝试使用 MySQL 5.5 和 5.1 版本;连接器的6.5.4、6.4.5和6.3.9版本并不能解决问题。
是否可以使用 MySQL、实体框架和代码优先方法?如果不是,那么切换到 ODBC 连接器而不是 .NET 的后果是什么?
在此先感谢,并对这个大问题感到抱歉。