How do I MANUALLY set an Identity field in LINQ-To

2020-02-09 11:17发布

问题:

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; }

...
}