Description:
I'm trying to use migration on entity framework 5.0 with MySQL Connector/Net 6.7.0 Alpha! I tested, but now I receive the following error message:
Method not found: 'System.DateTime System.Data.Entity.Migrations.Model.InsertHistoryOperation.get_CreatedOn()'.
How to repeat:
I tried with both! Add-Migration with Update-Migration, and AutomaticMigrationsEnabled... Look the example below:
Models:
-------
public class Produto
{
public Produto()
{
Categoria = new Categoria();
}
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Nome { get; set; }
public Categoria Categoria { get; set; }
public int ColunaTeste { get; set; } // This is the column for migration
}
public class Categoria
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string Nome { get; set; }
}
Context:
--------
public class Contexto : DbContext
{
public Contexto()
: base("Contexto")
{
Database.SetInitializer(new
//DropCreateDatabaseAlways<Contexto>()
MigrateDatabaseToLatestVersion<Contexto, Configuration>()
);
}
public DbSet<Produto> Produto { get; set; }
public DbSet<Categoria> Categoria { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Configuration (Enable-Migrations):
---------------------------------
internal sealed class Configuration : DbMigrationsConfiguration<TesteEF5_Migration.Contexto>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
SetSqlGenerator("MySql.Data.MySqlClient", new MySql.Data.Entity.MySqlMigrationSqlGenerator());
}
protected override void Seed(TesteEF5_Migration.Contexto context)
{
}
}
InitialCreate(Enable-Migrations):
---------------------------------
public partial class InitialCreate : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Produto",
c => new
{
ID = c.Int(nullable: false, identity: true),
Nome = c.String(unicode: false),
Categoria_ID = c.Int(),
})
.PrimaryKey(t => t.ID)
.ForeignKey("dbo.Categoria", t => t.Categoria_ID)
.Index(t => t.Categoria_ID);
CreateTable(
"dbo.Categoria",
c => new
{
ID = c.Int(nullable: false, identity: true),
Nome = c.String(unicode: false),
})
.PrimaryKey(t => t.ID);
}
public override void Down()
{
DropIndex("dbo.Produto", new[] { "Categoria_ID" });
DropForeignKey("dbo.Produto", "Categoria_ID", "dbo.Categoria");
DropTable("dbo.Categoria");
DropTable("dbo.Produto");
}
}
Controller:
-----------
public class HomeController : Controller
{
public ActionResult Index()
{
List<Categoria> returnValue = null;
try
{
using (var ctx = new Contexto())
{
var query = ctx.Categoria.ToList();
returnValue = new List<Categoria>();
if (query.Count() > 0)
returnValue.AddRange(query);
}
}
catch (Exception ex)
{
throw ex;
}
return View();
}
}
Web.Config:
-----------
<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="Contexto" connectionString="Server=localhost; Database=testemigrations; Uid=root;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Version" value="2.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="PreserveLoginUrl" value="true" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Optimization" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
</system.web>
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="4.4.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
------
This the source code used in the example.
Thks,
Paulo