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 LINQPa开发者_JAVA百科d and having problems with the identity problem of a table. 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
People person = new Person { Name = "Bob" };
People.InsertOnSubmit(person);
SubmitChanges();
But I get a SqlCeException stating
"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
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.
What do you get if you run this in 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.
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
This is probably the issue with dbml file. Check whether the column is marked as identity(or whatever this is called in L2S). The thing is that Id cannot appear in the insert query.
精彩评论