Additional attributes
Additional attributes
Hi there,
I need to add [Key] property attribute to some of the LINQ to SQL classes (System.ComponentModel.DataAnnotations.KeyAttribute).
However MyModel.Designer.cs is getting overwritten every time I made changes to the model.
Is there some workarounds to inject custom property attributes or *.tt file to customize source code generator?
Thank you in advance,
Lex
I need to add [Key] property attribute to some of the LINQ to SQL classes (System.ComponentModel.DataAnnotations.KeyAttribute).
However MyModel.Designer.cs is getting overwritten every time I made changes to the model.
Is there some workarounds to inject custom property attributes or *.tt file to customize source code generator?
Thank you in advance,
Lex
The simplest way to add custom attribute is to add it to the DataContext.cs class. It is designed to contain custom attributes and properties which should not be affected by the rebuilding of the project.
Add the attribute and the partial class declaration to this file in the following way:
Add the attribute and the partial class declaration to this file in the following way:
Code: Select all
[System.ComponentModel.DataAnnotations.Key]
public partial class MyClass{
}
Re: Additional attributes
Your example shows how to add custom class attributes.
Alladin wrote:I need to add [Key] property attribute
In this case you will need a bit more complicated approach.
Create a new code generation template (Tools->Entity Developer->Templates->New...), and load the code from the LINQ C# template.
Add the following code
Create a new code generation template (Tools->Entity Developer->Templates->New...), and load the code from the LINQ C# template.
Add the following code
Code: Select all
if(property.Name == "MyProperty")#>
[System.ComponentModel.DataAnnotations.Key]
, UpdateCheck = UpdateCheck.)]
Template) and the code generation will generate the Key attibute for the MyProperty property.
Found a bug in LINQ 2 SQL template:
this code line is always generated:
However, if I choose to store mapping in file, mappingSource should be XmlMappingSource.
this code line is always generated:
Code: Select all
private static System.Data.Linq.Mapping.MappingSource mappingSource = new Devart.Data.Linq.Mapping.AttributeMappingSource();