How do I MANUALLY set an Identity field in LINQ-To-SQL (IDENTITY INSERT)

丶灬走出姿态 提交于 2019-12-18 03:37:05

问题


I have a table that normally, upon insert, the auto-key will increment. But, there are some instances when we want to set the ID (as would easily be done with "IDENTITY INSERT" in SQL).

Is there a way to accomplish this with LINQ to SQL?

Thanks,


回答1:


Take a look here: http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/566e9540-911e-48b4-ac31-f69c0ab9f7fb/

Last reply here: http://forums.asp.net/t/1208607.aspx




回答2:


If you want to make IDENTITY increment always OFF

Edit the Model

public class TableName
{

    [Key,DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ID { get; set; }

...
}


来源:https://stackoverflow.com/questions/507515/how-do-i-manually-set-an-identity-field-in-linq-to-sql-identity-insert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!