开发者

sqlite Exception Identity Issue

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜