Insert Linq into the primary key field, which is auto-synchronizing

I more or less want to do what this question offers. How can I set the Identity field in LINQ-To-SQL (IDENTITY INSERT)

However, I want to explain. I have a client db. I load Linq objects and submit them through WCF. On the other hand, I attach them to the data context and send them to the table. The problem is that they will have their own set of Guid columns. This column is marked as AutoSync in DBML. Therefore, in the case of insertion, Linq forces me to use the new value. I would like it to preserve my value when I need it.

Update 1

Basically, I want to programmatically change the settings for AutoSync and IsDbGenerated.

[Column(Storage="_cName", AutoSync=AutoSync.OnInsert, DbType="UniqueIdentifier NOT NULL", IsPrimaryKey=true, IsDbGenerated=true)] 
+4
source share
3 answers

Let me rephrase the question: do you want to use the same object definitions on both sides, but on the one hand do you want the identifier to be generated, and on the other hand do you want it to be inserted? The only way I know this is to use XML-based metadata instead of attributes. Unfortunately, as far as I know, DBML visual designer does not support XML metadata. However, you can use the SqlMetal tool to generate XML, and then modify the file for use on the other side of the web service.

You can start using SqlMetal here: http://msdn.microsoft.com/en-us/library/bb386987.aspx

+1
source

You cannot change attributes on the fly without using reflection. You might be able to do it another way using the manual encoding of your linq. I would not recommend changing attributes on the fly, they are design attributes and there may be unforeseen flaws for changing this.

0
source

This is, of course, the IsDbGenerated property that you need to look at, not AutoSync.

I see you have described one scenario in which you want to set the identifier in the application. Is there any other scenario when you need it to be automatically generated by the database? If not, you can simply set IsDbGenerated = false and populate Guid.NewGuid () in your WCF service if / when you don't already have an identifier from the client.

One of the advantages of using rowguid instead of the identity column is that you are allowed to insert certain values ​​into the rowguid column, but you do not need this. You can save the column as rowguid with DEFAULT NEWID (), but set IsDbGenerated = false in your service, and it will work until you remember the actual filling of the identifier.

0
source

All Articles