当程序执行mainDb.SubmitChanges()时,我收到此错误:
INSERT INTO项目 价值观(@ p0)
SELECT CONVERT(Int,SCOPE_IDENTITY())AS [value] - @ p0:输入字符串(大小= 0; Prec = 0; Scale = 0)[test2] - 上下文:SqlProvider(Sql2008)模型:AttributeMetaModel Build:3.5.30729.4926
我的代码包含以下文件: Program.cs [这是主程序文件]
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using blg.db;
namespace blg
{
class Program
{
static void Main(string[] args)
{
SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder();
csb.Add("data source", "blg.sqlite");
using (SQLiteConnection connection = new SQLiteConnection(csb.ConnectionString))
{
connection.Open();
MainDb mainDb = new MainDb(connection);
mainDb.Log = Console.Out;
Project project = new Project();
project.projectName = "test2";
mainDb.projects.InsertOnSubmit(project);
mainDb.SubmitChanges();
//var query = from p in mainDb.projects
// select p;
//foreach (var row in query)
//{
// Console.WriteLine(row.projectName);
//}
Console.WriteLine(mainDb.Connection.State);
}
Console.ReadKey();
}
}
}
另一个文件是: mainDb.cs [此文件具有从DataContext继承的类]
using System;
using System.Collections.Generic;
using System.Data.Linq;
using System.Data.SQLite;
using System.Text;
namespace blg.db
{
class MainDb : DataContext
{
public Table<Project> projects;
public MainDb(SQLiteConnection connection) : base(connection) { }
}
}
最后一个文件是 Project.cs [这是将其映射到sqlite数据库的类]:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq.Mapping;
using System.Text;
using System.Data.SQLite;
namespace blg.db
{
[Table(Name = "projects")]
class Project
{
private int _id;
private string _projectName;
private string _another;
[Column(Name = "id", Storage = "_id", DbType = "int",
IsPrimaryKey = true, IsDbGenerated = true)]
public int id
{
get {return _id;}
set {_id = value;}
}
[Column(Name = "project_name", Storage = "_projectName", DbType = "text")]
public string projectName
{
get {return _projectName;}
set {_projectName = value;}
}
}
}
代码运行良好,除非它执行mainDb.SubmitChanges();在Program.cs中,有什么想法吗?