使用ssh框架怎样连接两个数据库

使用ssh框架怎样连接两个数据库,第1张

中英文切换

我设计的时候把所有的中文英文都放在一个表了 设计了单独的字段

中文的是1英文的是2

然后在页面判断就行了 select xxxx from table where ziduan='1'这就是中文的如果数据量不大的文字网站 你甚至可以用JS控制显示和隐藏 把数据库里所有数据都查出来

具体情况看吧 用两个数据库有点麻烦吧 后期维护也是个问题

注意这里面每个net framework都有两个版本,一个带有bundle字眼,一个没有。一个安装的DLL里面包含SQLiteInteropdll,而另一个没有。如果你运行代码的时候报

逗无法加载SQLiteInteropdll地的错误,则将安装文件中的

SQLiteInteropdll拷贝到Bin文件中即可。或是在NuGet下载的

packages\SystemDataSQLiteCore10940\build中也有对应的程序

示例代码

Modelcs

public class Person

{

public Int64 Id { get; set; } //注意要用Int64

public string FirstName { get; set; }

public string LastName { get; set; }

}

public class MyContext : DbContext

{

public DbSet<Person> Persons { get; set; }

public MyContext()

: base("SqliteTest")

{

}

}

Programcs

static void Main(string[] args)

{

MyContext context = new MyContext();

var empList = contextPersonsOrderBy(c => cFirstName)ToList();

ConsoleWriteLine(empListCount);

Person people = new Person()

{

FirstName = "Hello",

LastName = "World"

};

contextPersonsAdd(people);

contextSaveChanges();

ConsoleReadLine();

}

示例代码很简单,就是用EF对Person表进行新增与查看。

配置config文件

如果你是用NuGet获取Sqlite,会自动在config中配置一些相关的信息。

<xml version="10" encoding="utf-8">

<configuration>

<configSections>

<!-- For more information on Entity Framework configuration, visit -->

<section name="entityFramework" type="SystemDataEntityInternalConfigFileEntityFrameworkSection, EntityFramework, Version=6000, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

</configSections>

<connectionStrings>

<add name="SqliteTest" connectionString="data source=SqliteTestdb" providerName="SystemDataSQLiteEF6" />

</connectionStrings>

<startup>

<supportedRuntime version="v40" sku="NETFramework,Version=v45" />

</startup>

<systemdata>

<DbProviderFactories>

<add name="SQLite Data Provider" invariant="SystemDataSQLite" description="NET Framework Data Provider for SQLite" type="SystemDataSQLiteSQLiteFactory, SystemDataSQLite" />

<remove invariant="SystemDataSQLite" />

<remove invariant="SystemDataSQLiteEF6" />

<add name="SQLite Data Provider (Entity Framework 6)" invariant="SystemDataSQLiteEF6" description="NET Framework Data Provider for SQLite (Entity Framework 6)" type="SystemDataSQLiteEF6SQLiteProviderFactory, SystemDataSQLiteEF6" />

</DbProviderFactories>

</systemdata>

<entityFramework>

<defaultConnectionFactory type="SystemDataEntityInfrastructureLocalDbConnectionFactory, EntityFramework">

<parameters>

<parameter value="v110" />

</parameters>

</defaultConnectionFactory>

<providers>

<provider invariantName="SystemDataSqlClient" type="SystemDataEntitySqlServerSqlProviderServices, EntityFrameworkSqlServer" />

<provider invariantName="SystemDataSQLiteEF6" type="SystemDataSQLiteEF6SQLiteProviderServices, SystemDataSQLiteEF6" />

</providers>

</entityFramework>

</configuration>

其中数据连接串是:

<add name="SqliteTest" connectionString="data source=SqliteTestdb" providerName="SystemDataSQLiteEF6" />

注意提供程序集是SystemDataSQLiteEF6。

但是这个配仍然是错误的。

如果此时运行程序,会报错:

Unable to determine the provider name for

provider factory of type 'SystemDataSQLiteSQLiteFactory' Make sure

that the ADONET provider is installed or registered in the application

config

或中文错误信息:

未找到具有固定名称逗SystemDataSQLite地的 ADONET 提供程序的实体框架提供程序。请确保在应用程序配置文件的逗entityFramework地节中注册了该提供程序。

意思是EF没有找到提供SystemDataSQLiteSQLiteFactory的dll,我们看看现在config中的entityFramework节点:

<entityFramework>

<defaultConnectionFactory type="SystemDataEntityInfrastructureLocalDbConnectionFactory, EntityFramework">

<parameters>

<parameter value="v110" />

</parameters>

</defaultConnectionFactory>

<providers>

<provider invariantName="SystemDataSqlClient" type="SystemDataEntitySqlServerSqlProviderServices, EntityFrameworkSqlServer" />

<provider invariantName="SystemDataSQLiteEF6" type="SystemDataSQLiteEF6SQLiteProviderServices, SystemDataSQLiteEF6" />

</providers>

</entityFramework>

有SystemDataSQLiteEF6与

SystemDataSqlClient,确实没有名称为SystemDataSQLite的提供程序。这里我一直不明白为什么sqlite会去

找名称为SystemDataSQLite的提供程序,因为我们在连接串中配置的provider也是

SystemDataSQLiteEF6。

那我们就在EF的配置节点中增加一个名为SystemDataSQLite的provider,但type仍然是SystemDataSQLiteEF6。最终的配置如图:

红色部分是配置有变化的地方。

这里再运行程序就可以了。

注意:

1连接串的配置。

数据连接串可以指定绝对地址,也可以指定相对地址。像我的data

source=SqliteTestdb,则SqliteTestdb要在Bin文件夹中,如果是web程序可以通过Data

Source=|DataDirectory|\SqliteTestdb来配置在App_Data文件平中。

2如果没有指定数据库中的表文件名,EF生成的SQL表都是用复数表示。就像我的程序中实体名是Person,但EF去查找的表名会是People。所以在数据库中定义的表名是People。

3不支持CodeFirst模式,您需要自己先设计好Sqlite的表结构。

以上就是关于使用ssh框架怎样连接两个数据库全部的内容,包括:使用ssh框架怎样连接两个数据库、如何用Entity Framework 6 连接Sqlite数据库、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10201633.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-06
下一篇2023-05-06

发表评论

登录后才能评论

评论列表(0条)

    保存