我试图为我的问题找到一个解决方案,但直到现在我的努力都是徒劳的。 :-(
我使用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 Usuarios { get; set; }
public virtual ICollection 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 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(版本)的属性
[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连接器的后果是什么?
提前致谢,并对这个重大问题感到抱歉。