views:

94

answers:

2

Hello, is it possible to decorate a field of a LINQ generated class with [Column(IsDbGenerated=true)] using a buddy class (which is linked to the LINQ class via [MetadataType(typeof(BuddyMetadata))]) ?

My goal is to be able to clear and repopulate the LINQ ORM designer without having to set the "Auto Generate Value" property manually every time to re-establish the fact that certain columns are autogenerated.

Thanks!

A: 

You can use a partial class - use your own partial class and decorate it with the attribute. It will be safe from the code generation.

If you need to decorate a method, you may be able to use partial methods as well.

As far as I know, fields cannot be decorated this way without the code generator destroying them on re-generation.

Oded
But I can't have the same field declared twice (once in my partial class and once from the ORM)?!? I'm not sure how the partial class would solve my issue.
Alex
+1 @Alex as long as you only declare it you can do it, which allows you to add the attribute. What you can't is have 2 implementations.
eglasius
@Freddy, no this doesn't compile. As expected I get the two errors 'The type 'XYZ' already contains a definition for 'CreationDate'', and 'Ambiguity between 'Domain.XYZ.CreationDate' and 'Domain.XYZ.CreationDate'. This is when I have a partial class XYZ with the field CreationDate, and also the regular class XYZ that was generated by the ORM with the same field.
Alex
@Alex are both declared as partial?
eglasius
@Freddy, yes they are.
Alex
@Oded: No, my question specified in the beginning of the first sentence: "decorate a field of a LINQ generated class". That's clearly not a class...
Alex
@Alex sry, my memory failed, for some reason I thought I had something like that.
eglasius
@Freddy: You can do such a thing with buddy classes (which I'm trying) but somehow even though I set the Column attribute IsDbGenerated to true in the buddy class and attach it via metadatatype it won't work.
Alex
A: 

LINQ to SQL does not recognize buddy classes.

You can't just add the attribute to the partial either as the property is already defined in the other partial (this is what buddy classes were invented to solve).

One option would be to use my T4 template for VS that replicates the code-generation functionality of LINQ to SQL (I used to work on the product team) and you could put some logic in there to detect your auto-generated columns and emit the right attribute every time.

http://l2st4.codeplex.com

DamienG