Page 1 of 1

Additional attributes

Posted: Wed 07 Apr 2010 17:05
by Alladin
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

Posted: Thu 08 Apr 2010 14:38
by AndreyR
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:

Code: Select all

[System.ComponentModel.DataAnnotations.Key]
  public partial class MyClass{
  }

Re: Additional attributes

Posted: Thu 08 Apr 2010 16:26
by Alladin
Your example shows how to add custom class attributes.
Alladin wrote:I need to add [Key] property attribute

Posted: Fri 09 Apr 2010 11:54
by AndreyR
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

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.

Posted: Fri 09 Apr 2010 14:25
by Alladin
Big thanks / Большое спасибо

:roll:

Posted: Fri 09 Apr 2010 14:46
by Alladin
Found a bug in LINQ 2 SQL template:

this code line is always generated:

Code: Select all

private static System.Data.Linq.Mapping.MappingSource mappingSource = new Devart.Data.Linq.Mapping.AttributeMappingSource();
However, if I choose to store mapping in file, mappingSource should be XmlMappingSource.

Posted: Tue 13 Apr 2010 11:36
by AndreyR
Thank you for the report, this issue is associated with the problems in MappingMode=File.
It will be fixed in one of the nearest builds.