5

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.

Joachim VR
  • 2,320
  • 1
  • 15
  • 24
  • 1
    Did you find a solution for this yet? I'm trying to do the exact same thing. http://stackoverflow.com/questions/4651843/where-to-put-data-annotations-tags This post looks along the right lines, but I can't get it to work. – Banford Apr 18 '11 at 10:29
  • Sorry, lost track of this question. I did some research, and this seems to be an unfortunate shortcoming of the partial class system in .NET. Hopefully they will someday add this to an otherwise awesome language feature. – Joachim VR Jun 13 '12 at 20:21
  • check this this out , i already answered this question here http://stackoverflow.com/a/24757520/3050647 – elia07 Jul 15 '14 at 12:10

2 Answers2

1

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.

Taryn
  • 242,637
  • 56
  • 362
  • 405
Tom Lianza
  • 4,012
  • 4
  • 41
  • 50
0

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 { }
aanund
  • 1,483
  • 11
  • 18