当前位置: 首页 > 编程语言 > C#

SQLite的实体框架MigrationSqlGenerator分享

时间:2023-04-10 18:44:21 C#

SQLite的实体框架MigrationSqlGenerator实体框架有SQLite的MigrationSqlGenerator吗?我只找到了devart的一则广告。找不到提供程序“System.Data.SQLite”的MigrationSqlGenerator。在目标迁移配置类中使用SetSqlGenerator方法注册额外的SQL生成器。这是我所做的:http://msdn.microsoft.com/en-gb/data/jj591621虽然这篇文章已经开放了很长时间,但我会根据我们的朋友NSGaga发布我将要做的事情出现了问题。我设法根据为SQLCE开发的内容创建了一个类“MigrationSqLiteGenerator”。这个类的代码太大了,更多的可以去我的博客下载。在GoogleDrive上:https://drive.google.com/file/d/0B-NCqWnMQpBrQjkxMkxnVlRzb00/view?usp=sharing这门课只有基础知识,加上是改进的起点。22.Mar.2017由@shiva编辑:这是内联代码,因此如果GoogleDrive链接再次断开,您仍然可以访问代码。刚刚发现SO在回答帖子中有30,000个字符的限制,所以我不得不删除代码中的注释。这是超过32,000字符与评论:)//*--------------------------------------------------------------*//**//*CRIADOPOR...:JulioC.Borges。*//*数据…………:19/07/2013。*//*MOTIVO......:*//**//*--------------------------------------------------------------*使用系统;使用System.CodeDom.Compiler;使用System.Collections.Generic;使用System.Data.Common;使用System.Data.Entity.Migrations.Model;使用System.Data.Entity.Migrations.Sql;使用System.Data.Metadata.Edm;使用System.Data.SQLite;使用System.Globalization;使用System.IO;使用System.Linq;使用系统文本;命名空间ICM_Conexao.SqLite.Classes{//////MigrationSqLiteGenerator///publicclassMigrationSqLiteGenerator:MigrationSqlGenerator{#regionConstantesprivateconststringpstrDefaultDateTime="yyyy-MM-ddhh:mm:ss";私有常量intpintDefaultStringMaxLength=255;私有常量intpintDefaultPrecisaoNumerica=10;私有常量字节pbytDefaultTimePrecision=7;私人常量字节pintDefaultScale=0;//私有常量字符串pstrMigrationTableName="__MigrationHistory";#endregion#region实例privateDbProviderManifestpprovProviderManifest;私有列表plstCommands;私人布尔pblnGerouPrimaryKey;#endregion#regionMigratioSqlGenerator的覆盖生成方法publicoverrideIEnumerableGenerate(IEnumerablelstOperacoesMigrations,stringstrManifestoProvider){plstCommands=newList();InitializeServicosProvider(strManifestoProvider);生成命令(lstOperacoesMigrations);返回plst命令;}#endregion#region命令生成方法protectedvirtualvoidGenerate(CreateTableOperationopeCriacaoTabela){//最好不要生成迁移数据表if(opeCriacaoTablea.Name.Contains("MigrationHistory"))return;使用(varltextWriter=TextWriter()){GeraComandoCriacaoTable(opeCriacaoTable,ltextWriter);SQLCommand(ltextWriter);}}protectedvirtualvoidGenerate(AddForeignKeyOperationopeChaveEstrangeira){//Inicialmenten?ohaveraacria??odechaveestrangeira}protectedvirtualvoidGenerate(DropForeignKeyOperationdropForeignKeyOperation){//Inicalmenten?ohaveraacria??odechaveestrangeira}protectedvirtualvoidGenerate(CreateIndexOperationopeCriacaoIndex)复制代码{使用(varltextWriter=TextWriter()){ltextWriter.Write("CREATE");如果(opeCriacaoIndex.IsUnique)ltextWriter.Write("UNIQUE");ltextWriter.Write("索引");ltextWriter.Write(opeCriacaoIndex.Name);ltextWriter.Write("ON");ltextWriter.Write(RemoveDBO(opeCriacaoIndex.Table));ltextWriter.Write("(");for(intlintCount=0;lintCountlstOperacoesMigrations){foreach(lstOperacoesMigrations中的动态ldynOperacao)生成(ldynOperacao);}privatevoidInicializaServicosProvider(stringstrManifestoProvider){使用(varlconConexao=CreateConnection()){pprovProviderManifest=DbProviderServices.GetProviderServices(lconConexao).GetProviderManifest(strManifestoProvider);}}protectedvirtualDbConnectionCreateConnection(){returnnewSQLiteConnection();}privatevoidGeraComandoCriacaoTabela(CreateTableOperationopeCriacaoTabela,IndentedTextWritertextWriter){textWriter.WriteLine("CREATETABLE"+RemoveDBO(opeCriacaoTabela.Name)+"(");textWriter.Indent++;for(inti=0;i对于任何正在寻找处理迁移的生成器,我在https://sqliteef6migrations.codeplex.com上找到了一个在为“System.Data.SQLite.EF6.Migrations”的nuget包安装包后,您需要对迁移配置进行以下更改method.publicConfiguration(){AutomaticMigrationsEnabled=false;SetSqlGenerator("System.Data.SQLite",newSQLiteMigrationSqlGenerator());}完整的类应该如下所示。命名空间YourNamespace{使用System.Data.Entity.Migrations;使用System.Data.SQLite.EF6.Migrations;内部密封类配置:DbMigrationsConfiguration{publicConfiguration(){AutomaticMigrationsEnabled=false;SetSqlGenerator("System.Data.SQLite",newSQLiteMigrationSqlGenerator());}protectedoverridevoidSeed(YourContextcontext){//迁移到最新版本后将调用此方法。我认为我们可以使用相同的Android概念为SQLite创建一个简单的迁移引擎。这篇文章展示了这个概念。当然,我们没有任何可用的MigrationSqlGenerator,但这个概念是可靠且有用的。我不知道任何SQLite,但如果你想像其他人建议的那样创建一个,你可以使用这个开源项目开始。或者,也许您会发现这本身就足够了?http://www.codeproject.com/Articles/32977/Upgrade-framework-for-SQLite-databases以上是C#学习教程:SQLite的实体框架MigrationSqlGenerator分享的全部内容,如果对你有用需要了解一下更多关于C#学习教程,希望大家多多关注—本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: