Mat*_*ira 7 mysql ef-code-first entity-framework-5
我试图为我的问题找到解决办法,但到目前为止我的努力都是徒劳的.:-(
我使用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; }
}
Run Code Online (Sandbox Code Playgroud)
由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);
}
}
Run Code Online (Sandbox Code Playgroud)
首先,我必须更改名为Versao(version)的属性
[Timestamp]
public byte[] Versao { get; set; }
Run Code Online (Sandbox Code Playgroud)
至
[ConcurrencyCheck]
public int Versao { get; set; }
Run Code Online (Sandbox Code Playgroud)
因为在更改之前发生了错误(关于类型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)
Run Code Online (Sandbox Code Playgroud)
(输入字符串的格式不正确.)
我试图使用MySQL版本5.5和5.1; 6.5.4,6.4.5和6.3.9版本的连接器无法解决问题.
是否有可能使用MySQL,实体框架和代码第一种方法?如果没有,切换到ODBC连接器而不是.NET连接器有什么后果?
在此先感谢并抱歉这个大问题.
Kir*_*oll 21
我创建了一个 MySQL数据连接器v.6.6.4的分支,它支持最新版本的Entity Framework(v.5).
要使用它,你可以下载的二进制文件,这对于更换组件MySql.Data
和MySql.Data.Entity
.还要确保您的项目依赖于EF5而不是4.3.
在您Enable-Migrations
第一次修改Configuration
类的构造函数以包含该行之后:
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
Run Code Online (Sandbox Code Playgroud)
此时,您应该可以运行Add-Migration
而Update-Database
不会出现问题.
我叉子的主要变化如下:
二进制文件依赖于EF5而不是EF4.3.
EF5预装dbo.
到MySQL无法处理的所有表名.因此,我的版本破解了SQL迁移生成器,dbo.
从表名中去掉了前缀.这一切都假设您没有通过TableAttribute
实体类重写模式.
它消除了CreatedOn
Jimi在答案中提到的用法.
您还应该尝试.NET Connector 6.6,因为它是第一个声称支持 EF 4.3 的版本(第一个带有迁移的版本)。如果它没有帮助,您应该尝试dotConnect for MySql(至少试用)以查找问题是否出在 .NET Connector 或 EF 中。ODBC 连接器不适用于 EF。