在LINQtoSQL SQLite的自动增量列映射处理 [英] Dealing with SQLite Autoincrement column mapping in LINQtoSQL
问题描述
我已经如下因素实体
[Table(Name = "Users")]
public sealed class UserDB
{
private Int64 _id = -1;
private string _username = string.Empty;
public UserDB() { }
public UserDB(RepositoryInfo repoInfo)
{
UserName = repoInfo.Account;
}
[Column(Name = "ID", Storage = "_id", IsDbGenerated = true, IsPrimaryKey = true, UpdateCheck = UpdateCheck.Never)]
public Int64 ID { get { return _id; } set { _id = value; } }
[Column(Name = "UserName", DbType="nvarchar(50)", Storage = "_username")]
public string UserName { get { return _username; } set { _username = value; } }
}
ID 被映射为自动增量INTEGER类型列(实际上的唯一一种可以在SQLite的自动增量)
ID is mapped to Autoincrement INTEGER type column (actually the only type possible with autoincrement in SQLite)
当我尝试添加一个新用户到DB这样,我得到一个错误:
When I try to add a new user to DB like this, I get an error:
public static Int64 AddUser(DataContext context, RepositoryInfo repoInfo)
{
UserDB udb = new UserDB(repoInfo);
//an ID of udb is -1, but I tried different values too, doesn't change result
var userstable = context.GetTable<UserDB>();
userstable.InsertOnSubmit( udb );
context.SubmitChanges(); // here I get a error, see on screen shot
return udb.ID;
}
修改
A后,谷歌搜索并检查,似乎根本的SQLite没有提供任何 SCOPE_IDENTITY()
功能。但是,LINQ to SQL的注入呢!
EDIT
After a googling for and checking, seems that SQLite simply doesn't provide any SCOPE_IDENTITY()
function. But Linq To SQL injects it!
我怎样才能改变呢?
推荐答案
其实,有没有被我发现的解决方案来解决这个问题,如果没有一个建筑1 =>更改LINQ查询来改变输出SQL。
Actually there is no solution found by me to resolve this issue, if not that one architectural one => Change linq query to change outputted SQL.
这篇关于在LINQtoSQL SQLite的自动增量列映射处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!