我有一个使用EF6代码的ASP.NET MVC站点,它可以完美地对抗MS sql Server(在本地开发机器和Azure网站/ sql上).现在我将它转移到使用MySql 5.6 DBS的生产中,这给我带来了麻烦.
我的解决方案分为多个层(Web,视图模型,模型,数据层接口),这些层是持久性无知的(使用UnitOfWork和GenericRepository)和引用EF程序集的单个数据项目.
现在我想我会将我的解决方案交换机EF提供程序从sql Server分支到MysqL.
所以我添加了MysqL.Data.Entities NuGet包并对web.config进行了以下更改:
MysqL.Data.Entity.MysqLEFConfiguration,MysqL.Data.Entity.EF6">
sqlConnectionFactory,EntityFramework" />
MysqL.Data.MysqLClient" type="MysqL.Data.MysqLClient.MysqLProviderServices,MysqL.Data.Entity.EF6" />
MysqL Connector / Net版本是MysqL.Data 6.8.3.0
我还将[DbConfigurationType(typeof(DbContextConfiguration))]添加到我的DbContext类和SetExecutionStrategy(MysqLProviderInvariantName.ProviderName,()=> new MysqLExecutionStrategy());到我的DbContextConfiguration类继承自MysqLEFConfiguration
我能够运行我的代码优先迁移(借助SetHistoryContextFactory(MysqLProviderInvariantName.ProviderName,(conn,schema)=> new MysqLHistoryContext(conn,schema));)并创建数据库模式.
据我所知,数据库模式似乎是正确的. sql Server中nvarchar的列是longtext或varchar(取决于实体属性上指定的最大长度),MysqL和datetime2中的列是datetime.
现在,当我运行应用程序时,我收到以下异常:
System.Data.DataException was unhandled by user code
HResult=-2146233087
Message=An exception occurred while initializing the database. See the InnerException for details.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
at System.Data.Entity.Internal.LazyInternalContext.MetadataException
HResult=-2146232007
Message=Schema specified is not valid. Errors:
(0,0) : error 0040: The Type nvarchar(max) is not qualified with a namespace or alias. Only primitive types can be used without qualification.
(0,0) : error 0040: The Type datetime2 is not qualified with a namespace or alias. Only primitive types can be used without qualification.
(0,0) : error 0040: The Type nvarchar(max) is not qualified with a namespace or alias. Only primitive types can be used without qualification.
Source=EntityFramework
StackTrace:
at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader.ThrowOnNonWarningErrors()
at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader.LoadItems(IEnumerable`1 xmlReaders,IEnumerable`1 sourceFilePaths)
at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Loader..ctor(IEnumerable`1 xmlReaders,IEnumerable`1 sourceFilePaths,Boolean throwOnError,IDbDependencyResolver resolver)
at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection.Init(IEnumerable`1 xmlReaders,IEnumerable`1 filePaths,IDbDependencyResolver resolver,DbProviderManifest& providerManifest,DbProviderFactory& providerFactory,String& providerInvariantName,String& providerManifestToken,Memoizer`2& cachedCTypeFunction)
at System.Data.Entity.Core.Metadata.Edm.StoreItemCollection..ctor(IEnumerable`1 xmlReaders)
at System.Data.Entity.Utilities.XDocumentExtensions.GetStorageMappingItemCollection(XDocument model,DbProviderInfo& providerInfo)
at System.Data.Entity.Migrations.Infrastructure.EdmModelDiffer.Diff(XDocument sourceModel,XDocument targetModel,Lazy`1 modificationCommandTreeGenerator,MigrationsqlGenerator migrationsqlGenerator)
at System.Data.Entity.Internal.InternalContext.ModelMatches(XDocument model)
at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext,ModelHashCalculator modelHashCalculator,Boolean throwIfNoMetadata)
at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata)
at System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata)
at System.Data.Entity.CreateDatabaseIfNotExists`1.<>c__DisplayClass1.
请注意
Schema specified is not valid. Errors:
(0,0) : error 0040: The Type nvarchar(max) is not qualified with a namespace or alias. Only primitive types can be used without qualification.
我在我的智慧结束.无法弄清楚如何处理此问题或如何使EF与MysqL提供程序正常工作.在SO或Web上一般没有任何用处.我发现的所有内容都与http://forums.devart.com/viewtopic.php?t=24678上的Oracle DBS类似
我会感激任何想法.我开始认为移动到NHibernate会更好,如果我知道它会解决这个问题 – 我想知道问题是否与EF如何映射实体并生成sql语句或MysqL提供者/连接器中的一些错误有关.
>删除所有迁移类
>删除__migrationhistory MysqL表(如果存在)
>配置实体框架以使用MysqL
>创建新的初始迁移.
配置文件需要这个:
DbContext类使用名为PrimaryDatabase的连接字符串,该字符串在上面定义:
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext() : base("PrimaryDatabase")
{
}
}
最后在迁移配置文件中:
internal sealed class Configuration : DbMigrationsConfiguration
你可以找到详细的信息here.