views:

924

answers:

3

I more or less want to do what this question suggests. http://stackoverflow.com/questions/507515/how-do-i-manually-set-an-identity-field-in-linq-to-sql-identity-insert

However, I want to explain. I have a client db. I load Linq objects from here and the send them across WCF. On the other side, I attach them to a data context and post them to the table. The issue is, they will have their Guid column set. This column is marked as AutoSync in the DBML. So in the case of an insert, Linq forces me to use the new value. I would like it to keep my value when I need it to.

Update 1

Basically I want to flip the AutoSync and IsDbGenerated setting on the fly programatically.

[Column(Storage="_cName", 
        AutoSync=AutoSync.OnInsert, 
        DbType="UniqueIdentifier NOT NULL", 
        IsPrimaryKey=true, IsDbGenerated=true)]
A: 

You can't change attributes on the fly without using reflection. You may be able to do this in another fashion by handcoding your linq. I wouldn't advise changing the attributes on the fly, they are attributes by design and there may be unforeseen conscquencies for altering that.

Martin Murphy
+1  A: 

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

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

A: 

It is definitely the IsDbGenerated property that you need to look at, and not AutoSync.

I see you've described one scenario where you want to set the ID in the application. Is there actually another scenario where you need it to be auto-generated by the database? If not, you can just set IsDbGenerated = false, and populate with Guid.NewGuid() in your WCF service if/when you don't already have an ID from the client.

One of the advantages of using a rowguid instead of an identity column is that you're allowed to insert specific values into the rowguid column, but you don't have to. You can keep the column as a rowguid with DEFAULT NEWID(), but set IsDbGenerated = false in your service, and it will still work as long as you remember to actually populate the ID.

Aaronaught