使用 entity framework 6 访问 DB2-LUW 10
Accessing DB2-LUW 10 with entity framework 6
我正在开发一个应用程序,其中的数据库由最终用户在 运行 时选择。数据库可以位于 MS SQL 服务器或 IBM DB2 服务器上。我目前正在 windows 服务器上使用 IBM DB2 10 Express-c 进行测试。我正在使用 Visual Studio 2013 C# 和 Entity Framework 6 进行开发。我已经为 DB2 支持安装了 EntityFramework.IBM.DB2 Nuget 包。我正在对现有 SQL 服务器数据库使用代码优先逆向工程来生成我的基本代码。该应用程序适用于 SQL 服务器数据库。
我正在使用 System.Data.Common.DbProviderFactories.GetFactory 生成提供程序。
System.Data.EntityClient.EntityConnectionStringBuilder connectString = new System.Data.EntityClient.EntityConnectionStringBuilder(a_Connection);
System.Data.Common.DbConnection conn = System.Data.Common.DbProviderFactories.GetFactory(connectString.Provider).CreateConnection();
conn.ConnectionString = connectString.ProviderConnectionString;
LB500Database = new LB402_TestContext(conn, true);
a_Connection 是提供者=IBM.Data.DB2;提供者连接字符串="Database=LISTBILL;User ID=xxxx;Password=yyyy;Server=db210:50000"
并且正在被 EntityConnectionStringBuilder 正确解析。
然后我尝试使用
访问数据库中的 table
LBData500.LB_System oneSystem;
System.Linq.IQueryable<LB_System> allSystem = LB500Database.LB_System.Where(g => g.DatabaseVersion == databaseVersion && g.CompanyID == companyID);
我收到无效操作异常"Sequence contains no matching element",这意味着没有返回任何元素。如果我删除 Where 以便返回所有行(table 中有一个行)并尝试使用 VS 调试器枚举结果集,我会看到消息:
"The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe."
我没有使用多线程。我不在 OnModelCreating 中。
只需将连接字符串更改为指向 SQL 服务器就可以正常工作,所以我认为我的基本方法是合理的。如果我从服务器返回某种错误,我会继续做一些事情。我可以 运行 从 Visual Studio 内部查询,所以我有连接。
任何指点将不胜感激。
更新:
我发现 EF 对象是使用 EF5 生成的,并且正在使用 EF6 运行time。我首先使用 EF6 逆向工程代码重新生成了 EF 对象。我现在可以连接到数据库并收到一条错误消息:
"ERROR [42704] [IBM][DB2/NT64] SQL0204N \"DBO.LB_SYSTEM\" 是未定义的名称。"
DB2 数据库中的模式与我的用户标识相同(在本例中,并非总是如此)。我将 CurrentSchema=xxxx 添加到提供的连接字符串中,但 EF 仍将 dbo 作为模式名称传递。
现在我需要一种在 运行 时更改架构名称的方法。我看到一个 link 到 codeplex EFModelAdapter (http://efmodeladapter.codeplex.com)。所以我可以试试看。
Update2 看了一遍EFModelAdapter后,我决定走另一条路。因为我只需要数据库访问而不需要模式管理,所以我决定使用 Dapper (https://github.com/StackExchange/dapper-dot-net)。这非常适合我的需要,并允许我在访问 DB2 数据库时更改模式名称。
根据我的更新 2,Entity Framework 对我的需要来说有点矫枉过正。我切换到 dapper https://github.com/StackExchange/dapper-dot-net 并且我在多个 DBMS 上工作正常。
我正在开发一个应用程序,其中的数据库由最终用户在 运行 时选择。数据库可以位于 MS SQL 服务器或 IBM DB2 服务器上。我目前正在 windows 服务器上使用 IBM DB2 10 Express-c 进行测试。我正在使用 Visual Studio 2013 C# 和 Entity Framework 6 进行开发。我已经为 DB2 支持安装了 EntityFramework.IBM.DB2 Nuget 包。我正在对现有 SQL 服务器数据库使用代码优先逆向工程来生成我的基本代码。该应用程序适用于 SQL 服务器数据库。
我正在使用 System.Data.Common.DbProviderFactories.GetFactory 生成提供程序。
System.Data.EntityClient.EntityConnectionStringBuilder connectString = new System.Data.EntityClient.EntityConnectionStringBuilder(a_Connection);
System.Data.Common.DbConnection conn = System.Data.Common.DbProviderFactories.GetFactory(connectString.Provider).CreateConnection();
conn.ConnectionString = connectString.ProviderConnectionString;
LB500Database = new LB402_TestContext(conn, true);
a_Connection 是提供者=IBM.Data.DB2;提供者连接字符串="Database=LISTBILL;User ID=xxxx;Password=yyyy;Server=db210:50000" 并且正在被 EntityConnectionStringBuilder 正确解析。
然后我尝试使用
访问数据库中的 table LBData500.LB_System oneSystem;
System.Linq.IQueryable<LB_System> allSystem = LB500Database.LB_System.Where(g => g.DatabaseVersion == databaseVersion && g.CompanyID == companyID);
我收到无效操作异常"Sequence contains no matching element",这意味着没有返回任何元素。如果我删除 Where 以便返回所有行(table 中有一个行)并尝试使用 VS 调试器枚举结果集,我会看到消息: "The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe."
我没有使用多线程。我不在 OnModelCreating 中。
只需将连接字符串更改为指向 SQL 服务器就可以正常工作,所以我认为我的基本方法是合理的。如果我从服务器返回某种错误,我会继续做一些事情。我可以 运行 从 Visual Studio 内部查询,所以我有连接。
任何指点将不胜感激。
更新: 我发现 EF 对象是使用 EF5 生成的,并且正在使用 EF6 运行time。我首先使用 EF6 逆向工程代码重新生成了 EF 对象。我现在可以连接到数据库并收到一条错误消息: "ERROR [42704] [IBM][DB2/NT64] SQL0204N \"DBO.LB_SYSTEM\" 是未定义的名称。" DB2 数据库中的模式与我的用户标识相同(在本例中,并非总是如此)。我将 CurrentSchema=xxxx 添加到提供的连接字符串中,但 EF 仍将 dbo 作为模式名称传递。
现在我需要一种在 运行 时更改架构名称的方法。我看到一个 link 到 codeplex EFModelAdapter (http://efmodeladapter.codeplex.com)。所以我可以试试看。
Update2 看了一遍EFModelAdapter后,我决定走另一条路。因为我只需要数据库访问而不需要模式管理,所以我决定使用 Dapper (https://github.com/StackExchange/dapper-dot-net)。这非常适合我的需要,并允许我在访问 DB2 数据库时更改模式名称。
根据我的更新 2,Entity Framework 对我的需要来说有点矫枉过正。我切换到 dapper https://github.com/StackExchange/dapper-dot-net 并且我在多个 DBMS 上工作正常。