简体   繁体   English

使用 LINQPad 将实体插入 SQL Compact 4 Table with Identity 列

[英]Inserting Entity into SQL Compact 4 Table with Identity column using LINQPad

I'm trying to insert new records into a SQL CE 4 database with LINQPad and having problems with the identity problem of a table.我正在尝试使用 LINQPad 将新记录插入到 SQL CE 4 数据库中,并且遇到表的身份问题。 Let's say I have this simple table for instance:假设我有这个简单的表格,例如:

PEOPLE
Id int IDENTITY(1,1) NOT NULL,
Name nvarchar(100) NOT NULL

I may be doing things the wrong way but I tried this in LINQPad我可能以错误的方式做事,但我在 LINQPad 中尝试过

People person = new Person { Name = "Bob" };
People.InsertOnSubmit(person);
SubmitChanges();

But I get a SqlCeException stating但我得到一个 SqlCeException 说明

"The colum cannot be modified. [ Column name = Id ]"

I can insert a record with SQL just fine, that works with no errors from SQL CE or its data provider and SQL CE sets the Id column for me which is what I'm wanting我可以用 SQL 插入一条记录就好了,这在 SQL CE 或其数据提供者和 SQL CE 中没有错误的情况下工作,我想为我设置 Id 列是什么

INSERT INTO PEOPLE (Name) VALUES ('Bob');

Is there another step that I'm missing?我还缺少另一个步骤吗? I'm not even sure if its a LINQPad issue but thought I'd ask anyways since that's what I'm trying this code with right now.我什至不确定它是否是 LINQPad 问题,但我想我还是会问,因为这就是我现在正在尝试使用此代码的原因。

What do you get if you run this in LinqPad如果你在 LinqPad 中运行它,你会得到什么

(from dm in this.Mapping.GetTable(typeof(People)).RowType.DataMembers 
    select new  { dm.DbType, dm.Name, dm.IsPrimaryKey , dm.IsDbGenerated }
 ).Dump();

In particular, as I understand it, IsDbGenerated should be true for the id column.特别是,据我了解, IsDbGenerated 对于 id 列应该是正确的。

I have a SQL CE 3.5 data file that I have used previously in LinqPad and looking at the SQL generated for inserts it does not mention id我有一个 SQL CE 3.5 数据文件,我以前在 LinqPad 中使用过,并查看为插入生成的 SQL 它没有提到 id

This is probably the issue with dbml file.这可能是 dbml 文件的问题。 Check whether the column is marked as identity(or whatever this is called in L2S).检查该列是否标记为标识(或在 L2S 中调用的任何内容)。 The thing is that Id cannot appear in the insert query.问题是 Id 不能出现在插入查询中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM