Adding attributes to properties in the Linq2Sql DBML

1.4k Views Asked by At

We are working with a database containing metadata on all database tables and fields used in our applications. From this metadata, we generate code that contains partial classes, extending our Linq2Sql entities.

We had the idea of adding an "obsolete" flag in our metadata, which should in turn add the Obsolete-attribute to the flagged properies in the Linq object. (generating warnings in our code using old fields)

Is this type of extension possible in a partial class? To just add an attribute to a property in a partial class file? This sounds a lot like a "partial property", something I tought didn't exist in .NET.

2

There are 2 best solutions below

0
On

Yeah, check out the MetadataType attribute.

Good example here. In this context, they want to add attributes for the purposes of validation, but no reason you couldn't do the same to indicate obsolescence.

1
On

Yes it is possible to use partial classes to add attributes to classes autogenerated through the DBML.

I use this myself to add the CompilerGenerated attribute to the classes that Linq2SQL generates. For instance:

[System.Runtime.CompilerServices.CompilerGenerated()]
public partial class MyDataContext : System.Data.Linq.DataContext {}

[System.Runtime.CompilerServices.CompilerGenerated()]
public partial class tblInsurance { }