MVC3代码优先-EF4.1不';t自动创建表(使用MySQL和Connector/NET6.3.6。)
EF4.1以及MySQL和Connector/NET6.3.6不会基于我的POCO对象自动创建表。我不确定配置是否错误,或者Connector/.Net 6.3.6是否不支持此功能。我已经在互联网上爬了一个星期没有找到答案,我相信我已经在stackoverflow上浏览了所有的EF和MySQL问题。我注意到很多帖子都提到dotConnect(例如),但我需要找到一个免费的解决方案。我已经设置了表单身份验证,这在MySQL提供程序中运行良好,但我必须手动创建数据库模式和表MVC3代码优先-EF4.1不';t自动创建表(使用MySQL和Connector/NET6.3.6。),mysql,asp.net-mvc-3,entity-framework-4.1,mysql-connector,Mysql,Asp.net Mvc 3,Entity Framework 4.1,Mysql Connector,EF4.1以及MySQL和Connector/NET6.3.6不会基于我的POCO对象自动创建表。我不确定配置是否错误,或者Connector/.Net 6.3.6是否不支持此功能。我已经在互联网上爬了一个星期没有找到答案,我相信我已经在stackoverflow上浏览了所有的EF和MySQL问题。我注意到很多帖子都提到dotConnect(例如),但我需要找到一个免费的解决方案。我已经设置了表单身份验证,这在MySQL提供程序中运行良好,但我必须手动创建数据库模式和表 <prof
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
我已经按照以下方式设置了我的项目。我的web.config文件的一部分
<connectionStrings>
<add name="MySQLConn" connectionString="Server=localhost;Database=XxX;Uid=xXx;Pwd=xXx;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
我的POCO对象:
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
[XmlRoot(ElementName = "Shop", IsNullable = false)]
[Table("shops")]
public class ShopModel
{
[Key]
public int Id { get; set; }
// must be unique
[Required]
public string Name { get; set; }
public string SiteUrl { get; set; }
public string LogoUrl { get; set; }
public string AffiliateSuffix { get; set; }
public virtual ICollection<ProductModel> ProductModels { get; set; }
}
[XmlRoot(ElementName = "Product", IsNullable = false)]
[Table("products")]
public class ProductModel
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
public decimal Price { get; set; }
public string ImageUrl { get; set; }
public string ProductUrl { get; set; }
[Required]
public virtual ShopModel ShopModel { get; set; }
}
[XmlRoot(ElementName=“Shop”,IsNullable=false)]
[表(“店铺”)]
公共类商店模式
{
[关键]
公共int Id{get;set;}
//必须是唯一的
[必需]
公共字符串名称{get;set;}
公共字符串SiteUrl{get;set;}
公共字符串LogoUrl{get;set;}
公共字符串AffiliateSuffix{get;set;}
公共虚拟ICollection ProductModels{get;set;}
}
[XmlRoot(ElementName=“Product”,IsNullable=false)]
[表(“产品”)]
公共类产品模型
{
[关键]
公共int Id{get;set;}
[必需]
公共字符串标题{get;set;}
[必需]
公共十进制价格{get;set;}
公共字符串ImageUrl{get;set;}
公共字符串ProductUrl{get;set;}
[必需]
公共虚拟商店模型商店模型{get;set;}
}
我的数据库上下文
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
public class MySQLConn : DbContext
{
public DbSet<ShopModel> Shops { get; set; }
public DbSet<ProductModel> Products { get; set; }
}
公共类MySQLConn:DbContext
{
公共数据库集商店{get;set;}
公共数据库集产品{get;set;}
}
我创建了一个模拟类,它创建了一个新的商店和一个新的产品对象,如下所示
添加(新的ShopModel{…});
ctx.Savechanges();//代码到此为止
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
当我运行代码时,出现以下错误:
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
表“schemeName.shops”不存在
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
描述:执行当前web请求期间发生未处理的异常。请查看堆栈跟踪以了解有关错误的更多信息以及错误在代码中的起源
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
异常详细信息:MySql.Data.MySqlClient.MySqlException:表“schemeName.shops”不存在
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
您是否尝试过使用SQL Express来查看是否出现相同的错误
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
您是否尝试过创建数据库初始值设定项?该类将像下面的代码一样声明。让它在一些表中添加值
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
public class MySQLConnInitializer : DropCreateDatabaseIfModelChanges<MySQLConn>
公共类MySQLConnInitializer:DropCreateDatabaseIfModelChanges
然后在Global.asax.cs中添加以下行:
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
Database.SetInitializer<MySQLConn>(new MySQLConnInitializer ());
Database.SetInitializer(新的MySQLConnInitializer());
我找到了这个问题的解决方案。您必须获得Connector/NET 6.4.4的最新版本。添加数据库初始化器。将Persist Security Info=true添加到连接字符串中。我做了所有这些,我的是完美的作品
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
还注意到,如果您已有一个现有数据库,EF可能会抛出EntityDataModel异常。在EF自动创建数据库和表之前,您可能必须手动删除以前的数据库。从那时起,您对模型和DBContext所做的任何更改都会自动反映在数据库中。MySql Connector/NET直到6.6版才首先支持实体框架代码
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
(目前6.6.6、6.7.4或6.8.3中的任何一个都可以)。我在本地尝试过SQL Express,并且正确创建了包含列的表。我放弃了代码优先的方法。现在,我正在手动创建表,并为数据库中的每个更改更新EF模型。这是有效的,所以我想我会坚持下去。我现在也遇到了同样的问题。起初,我的项目正在删除并重新创建数据库。然后我不得不手动删除数据库。现在,除非我让它这么做,否则它什么都不会做。尝试添加行“Database.CreateIfNotExists();”我在使用EF4.1+MySQL时也遇到了一些问题。您在POCO类中尝试过任何上下文环境继承吗?我建议您重构类“ProductModel”;将其转换为一个抽象类,创建一个继承“ProductModel”的类“ProductWithImageModel”和一个继承“ProductModel”的“ProductWithoutImageModel”。在第一个中,放置“ImageUrl”属性;最后,你不能空着(这只是我们的控制)。你能设法让它工作吗?这就是我用更多细节描述我的问题的地方
<profile defaultProvider="MySqlProfileProvider" enabled="true">
<providers>
<clear />
<add name="MySqlProfileProvider" type="MySql.Web.Profile.MySQLProfileProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>
</profile>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider">
<providers>
<clear />
<add name="MySqlRoleProvider" type="MySql.Web.Security.MySQLRoleProvider,MySql.Web,Version=6.3.6.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d" connectionStringName="MySQLConn" applicationName="/" />
</providers>