Click here to Skip to main content
15,887,812 members
Articles / Programming Languages / C#

Adding Metadata to Entities in The Data Model

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
20 Jan 2011CPOL2 min read 43.9K   1   1
Adding Metadata to Entities in The Data Model

Introduction

Sometimes I'm being asked how to add metadata to a generated entity in Entity Framework.

Adding Metadata to Entities in The Data Model

This metadata can be data annotation or other attributes which can help the developer during runtime. One answer that I give is to edit the T4 template in order to add the attributes. This solution can be combined with the building of an extension to Entity Framework designer which can add more details to the EDM. But it can take some time to develop. Another solution is to create a MetadataType for the entity and use the entity’s partial class behavior to add this type. This post will show you the second solution.

Adding Metadata to a Generated Entity

In the example, I'm going to use the following simple entity:

Entity Designer Diagram

This type is part of a Dynamic Data site and the requirements for it are not to show the TypeID and that the URL needs to be up to 100 characters. Since Dynamic Data works with Data Annotations, I want to add this metadata to the entity. But the problem is that the entity is generated by Entity Framework code generation. So how can I add the annotations? Using the MetadataType attribute. The MetadataType is an attribute that is part of the System.ComponentModel.DataAnnotations assembly. It indicates that a data model class has an associated metadata class. The MetadataType attribute gets a type parameter to specify which type is holding the metadata for the class. We can use the fact that the entity is generated as partial class and add the MetadataType attribute to it. Let's look at an example of how to use the MetadataType attribute:

C#
public class CRMTypeMetadata
{
    [ScaffoldColumn(false)]
    public int TypeID { get; set; }
 
    [StringLength(100)]
    public string Url { get; set; }
}
 
[MetadataType(typeof(CRMTypeMetadata))]
public partial class CRMType
{
}

The first thing to notice in the example is that I've created a public class by the name CRMTypeMetadata which holds the properties annotated with the relevant attributes. The Metadata postfix in the class name is a convention that I encourage you to use. After I create the metadata class, all I need to do is to create a partial class for the generated entity and annotate it with the MetadataType attribute. The MetadataType attribute will get as a parameter the type of the metadata type which is CRMTypeMetadata in the example. That is all.
Now the expected behavior was achieved.

Summary

One of the solutions to add metadata to entities in the Entity Data Model is by using the MetadataType attribute. It is very simple to use and can help you in frameworks like ASP.NET MVC, Dynamic Data, WCF Ria Services and more.


This article was originally posted at http://feeds.feedburner.com/GilFinkBlog

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead sparXys
Israel Israel
Gil Fink is a web development expert and ASP.Net/IIS Microsoft MVP. He is the founder and owner of sparXys. He is currently consulting for various enterprises and companies, where he helps to develop Web and RIA-based solutions. He conducts lectures and workshops for individuals and enterprises who want to specialize in infrastructure and web development. He is also co-author of several Microsoft Official Courses (MOCs) and training kits, co-author of "Pro Single Page Application Development" book (Apress) and the founder of Front-End.IL Meetup. You can read his publications at his website: http://www.gilfink.net

Comments and Discussions

 
QuestionProblem using MetadataType attribute on ADO .NET Entity data model in ASP.NET Pin
Patrik Fatoric23-Oct-11 23:53
Patrik Fatoric23-Oct-11 23:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.