sql insert is ignoring sql server default value for the primary key

I have a table that the primary key field has a default value of newid() --> that generates a unique id every time a record gets inserted. when I try to insert a record directly in the database from any sql management tool, it works. but when I try to use C# linq-sql insert it gives my the below error:

Cannot insert the value NULL into column '', table ''; column does not allow nulls. INSERT fails. The statement has been terminated.

why is linq ignoring the default value of the field, and is there any alternative way of doing this, given that I don't want the normal sequential int id in the primary key below is the c# code for the insert:

 databaseDataContext db = new databaseDataContext();
        order c = new order();

        c.orderName = "Untitled";
        c.orderStatus = "New";
        db.orders.InsertOnSubmit(c);
        db.SubmitChanges();

The suggestion provide by @dbugger is good enough. However, in case you still need it to be generated in SQL, make sure you specify that the ID column (primary key field) has the IsDbGenerated attribute along with the IsPrimaryKey:

[Column(IsDbGenerated = true, IsPrimaryKey = true)]
链接地址: http://www.djcxy.com/p/40814.html

上一篇: 如何在Java中做一个2d数组的深层副本?

下一篇: SQL插入忽略主键的SQL Server默认值