sqlite异常身份问题

发布于 2024-09-19 12:28:47 字数 2765 浏览 3 评论 0原文

当程序执行 mainDb.SubmitChanges() 时出现此错误:

INSERT INTO 项目 值 (@p0)

选择 CONVERT(Int, SCOPE_IDENTITY()) AS [值] -- @p0:输入字符串(大小 = 0;Prec = 0;比例 = 0)[test2] -- 上下文:SqlProvider(Sql2008) 模型:AttributeMetaModel 版本: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 中,有什么想法吗?

I'm getting this error when the program executes mainDb.SubmitChanges():

INSERT INTO projects
VALUES (@p0)

SELECT CONVERT(Int, SCOPE_IDENTITY()) AS [value]
-- @p0: Input String (Size = 0; Prec = 0; Scale = 0) [test2]
-- Context: SqlProvider(Sql2008) Model: AttributeMetaModel Build: 3.5.30729.4926

My code has the following files:
Program.cs [this is the main program file]

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();
        }
    }
}

the other file is:
mainDb.cs [this file has the class that is inherited from 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) { }
    }
}

the last file is
Project.cs [this is the class for mapping it to the sqlite database]:

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;}
        }
    }
}

the code is working well, except when it executes mainDb.SubmitChanges(); in the Program.cs, any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文