.nettiers和SQLite搅合到一块之后遇到的问题

第一步

用SQLiteStudio生成一个新的数据库,sqlitetest,新建一张表test,建立一个主键字段ID,一个字符字段Name,建立完成,留待后用。

第二步

用VS2010建立一个sqlitetest的控制台程序,添加System.Data.SQLite和System.Data.SQLite.Linq引用(.Net版本的SQLite在http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki下载,目前版本1.0.87.0对应SQLite 3.7.17

 第三步

用CodeSmith正常生成项目,添加sqlitetest.Data、sqlitetest.Data.GenericClient和sqlitetest.Entities三个项目到程序中,并添加引用

问题1

这时编译会提示“命名空间“sqlitetest”中不存在类型或命名空间名称“Data”。是否缺少程序集引用?”,需要修改程序的目标框架从.NET Framework 4 Client Profile改成.NET Framework 4。

问题2

这时运行会抛出异常“Unable to load NetTiersServiceSection”,需要添加app.config文件,添加下面的内容到config文件的最前面

<configSections>
<section name="sqlitetest.Data"
type="sqlitetest.Data.Bases.NetTiersServiceSection, sqlitetest.Data"
allowDefinition="MachineToApplication"
restartOnExternalChanges="true" />
</configSections>

还有两段内容也需要复制到config文件中,具体见生成的report.html

问题3

这时运行会抛出异常“未能加载文件或程序集“sqlitetest.Data.SqlClient”或它的某一个依赖项。系统找不到指定的文件。”,因为之前生成的代码还是使用SqlClient,而不是产生的GenericClient

type="sqlitetest.Data.SqlClient.SqlNetTiersProvider, sqlitetest.Data.SqlClient"

上面这行改成

type="sqlitetest.Data.GenericClient.GenericNetTiersProvider, sqlitetest.Data.GenericClient"

——————————

providerInvariantName="System.Data.SqlClient"

上面这行改成

providerInvariantName="System.Data.SQLite"

问题4

这时运行会抛出异常“SQL logic error or missing database near ".": syntax error”,原因是生成的dbCommand类似这样:


INSERT INTO .[test]
(
[Name]
)
VALUES
(
@Name
);
注意表名前面有个".",费了点功夫去跟踪了下这个东东的来历,长话短说,这个东东来自于System.Data.SQLite.Procedures.cst,第70行: 

string owner = GetOwnerName(SourceTable, true);
改成这样:
string owner = GetOwnerName(SourceTable);

重新生成即可

问题5

GenericTestProviderBase.generated.cs,第646行注释掉

entity.Id = (System.Int64) reader["test.ID"];

换用644行

entity.Id = (long)reader["Id"];

修改GenericEntityProviderBase.generated.cst,第1024行,去掉注释

entity.Id = (long)reader["Id"];
注释掉1038行

//entity.<%=GetPropertyName(cols[i])%> = (<%=castTo%>) reader["<%=cols[i]%>"];

问题6

System.Data.SQLite.Procedures.cst,第288行

tempParams += string.Format("AND [{1}] = @{0} {2}", prefix + GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]), SourceTable.PrimaryKey.MemberColumns[i], Environment.NewLine) ;

改为

tempParams += string.Format("AND [{1}] = @{0} {2}", prefix + GetPropertyName(SourceTable.PrimaryKey.MemberColumns[i]), SourceTable.PrimaryKey.MemberColumns[i].Name, Environment.NewLine) ;

解决UPDATE时的异常

posted on 2013-08-01 11:47  sPhinX  阅读(567)  评论(0编辑  收藏  举报

导航