This is a migrated thread and some comments may be shown as answers.

modifying generated model

3 Answers 65 Views
Data Access Free Edition
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Samuel
Top achievements
Rank 1
Samuel asked on 02 Sep 2013, 12:44 PM
I've generated a domain model rlinq file using the wizard. The problem is that I need to add the [ForeignKey] attribute to some of the properties. 

However it does not seem possible to modify the generated classes. 

How can I add this attribute without loosing it each time I compile the project or a modification is made through the wizard ? 


Thanks 

3 Answers, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 05 Sep 2013, 12:51 PM
Hi Samuel,

I am sorry for the inconvenience you are experiencing.

Generally, the described behaviour is the designed one of Telerik OpenAccess ORM when you create and edit your model through Visual Designer.

On the other hand, OpenAccess ORM allows you to automatically apply the ForeignKey attribute by slightly modifying our code generation templates. The workflow is as follows:
1. Open the generated .rlinq file in Visual Designer
2. Run the Model Settings dialogue and navigate to the Code Generation tab
3. In the Code Generation Template section, select Custom Defined and click on the Copy Default To... button in order to specify a folder in your project that will hold the modified templates
4. After the folder is provided and the Select Folder dialogue is closed, OpenAccess ORM will take care to copy the default code generation templates locally, so that you can modify the easily
5. Make sure that the Generate DataAnnotation Attributes check-box is checked and click OK to close the dialogue
6. Save the domain model so that the new settings are applied
7. Open the DefaultTemplateCS.tt file and paste the following after the body of the RunCodeGeneration() method:
public class MyAttributesExtender: AttributesTranslator
{
    public override void SetDataAnnotationAttributes(Telerik.OpenAccess.Metadata.MetaNavigationMember navigationMember, Telerik.OpenAccess.CodeGeneration.CodeProperty codeProperty)
    {
        base.SetDataAnnotationAttributes(navigationMember, codeProperty);
            if(navigationMember.Master)
        {
            var ids = navigationMember.Association.GetSourceConceptualKeys();
            var param = new Telerik.OpenAccess.CodeGeneration.CodeConstructorParameter("ForeignKey", "\"" + ids[0].PropertyName + "\"");
            codeProperty.AddAttribute(new Telerik.OpenAccess.CodeGeneration.CodeAttribute(param) { Name = "ForeignKey", ImportNamespace = "System.ComponentModel.DataAnnotations.Schema" });
        }
     }
 
    public override void SetDataAnnotationAttributes(Telerik.OpenAccess.Metadata.MetaPrimitiveMember primitiveMember, Telerik.OpenAccess.CodeGeneration.CodeProperty codeProperty)
    {
        base.SetDataAnnotationAttributes(primitiveMember, codeProperty);
    }
}
8. On line 51 replace:
new AttributesTranslator()
with:
new MyAttributesExtender()
9. Save the .tt file and explicitly save the domain model

At this point, the reference navigation properties for all classes will be decorated with the attribute. Note that after this modification you need to treat the folder specified in step 3 in the workflow as part of your project.

I hope this helps. If you experience difficulties or have additional questions, do not hesitate to get back to us.



Regards,
Doroteya
Telerik
OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvementsshipped with this release.

0
Samuel
Top achievements
Rank 1
answered on 05 Sep 2013, 12:55 PM
Hello,

Thanks for helping. Actually this issue is totally related to this one I've reported: http://www.telerik.com/community/forums/orm/dsw/wcf-dataservice-incomplete-metadata.aspx

So in order to workaround, I've used Fluent instead of Database First, so I could specicfy the Foreign Key. But like I've explained in the other question, the real problem is that the ReferentialConstraint node is NOT generated in the metadata, despite the fact that the foreign key constraint does exist.

I hope this makes sense.
0
Doroteya
Telerik team
answered on 10 Sep 2013, 07:40 AM
Hello Samuel,

I am glad you have found a workaround that is feasible to you.

Let me just add that the suggested modification of the code generation templates could be applied to Fluent too. That way you will not have to add the attributes manually. The workflow is as follows:
1. Create a new Class Library project (you can use an existing project if you already have one dedicated to the model)
2. Create a folder in the project called OpenAccessTemplates
3. Copy the Includes_ver.2 folder from <install_dir>\Telerik\OpenAccess ORM\dsl20XX\CodeGenerationTemplates\CSharp to OpenAccessTemplates, where dsl20XX would be dsl2012 if you are using Visual Studio 2010 (respectively dsl2010 corresponds to Visual Studio 2010)
4. Copy FluentModel.tt from <install_dir>\Telerik\OpenAccess ORM\dsl20XX\CodeGenerationTemplates\FluentMapping\CSharp to OpenAccessTemplates
5. Delete the content of the Custom Tool property for the FluentModel.tt file
6. Open the FluentModel.tt file and paste the code from step 7 of my previous post after the body of the RunCodeGeneration() method
7. Apply step 8 from the previously suggested workflow and save the .tt file
8. Right-click on the project name, select Add\New Item... and run the Telerik OpenAccess Fluent Model item template (optionally, you can add a folder in the project that will hold the generated fluent model)
9. Navigate through the pages of the wizard and on Code Generation Settings make sure to apply the following settings:
- Code Generation Template: Custom Defined - click the Select Existing... button, navigate to the OpenAccessTemplates folder and select the modified FluentModel.tt file
- Check the Generate DataAnnotation Attributes checkbox
10. Click the Finish button

I hope this works for you.

If you need further assistance, do not hesitate to get back to us.


Regards,
Doroteya
Telerik
OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvementsshipped with this release.

Tags
Data Access Free Edition
Asked by
Samuel
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Samuel
Top achievements
Rank 1
Share this question
or