This is common problem when using Entity Framework, If we use Data Annotations directly in classes generated by Entity Framework this Annotations are removed when we recreate EDMX file. In this article I am going to tell you how to preserve these Data Annotations even EDMX file is recreated.
Many programmers including Me write Data Annotations directly in classes generated by Entity Framework. This works fine but will creates problem when you need to modify table schema. I suffered from this problem when I modified my table schema and recreate my EDMX file to find all my Data Annotations are removed.
To deal with this problem I keep my automatically generated classes by Entity Framework as it is and added a class in Models folder to contain the partial class and namespace declaration same as the partial class and namespace declaration of the data model that I am using like this
This is TblUser class in Entity folder
This is TblUser class in my Models folder with Data Annotations applied
Here notice that I keep namespace and partial class declaration of class in Models Folder same as that of in my Entity Folder. Also associated the new class(TblUserMD) with the table class(TblUser) by using the MetadataTypeAttribute attribute found in System.ComponentModel.DataAnnotations. The associated class can have any name, a convention is to append "MD" or "MetaData" to the table class name. The associated class must be used with EDM or LINQ-to-SQL models because CLR types cannot mark existing properties with new attributes.
Now when I use this model with view like this
All Data Annotations used in model get applied to my view like this
So next time I recreate EDMX file my Data Annotations keep safe in class in Models folder.
I hope this article will help you. Please leave comments to improve this article, any suggestion also welcomed.
Many programmers including Me write Data Annotations directly in classes generated by Entity Framework. This works fine but will creates problem when you need to modify table schema. I suffered from this problem when I modified my table schema and recreate my EDMX file to find all my Data Annotations are removed.
To deal with this problem I keep my automatically generated classes by Entity Framework as it is and added a class in Models folder to contain the partial class and namespace declaration same as the partial class and namespace declaration of the data model that I am using like this
This is TblUser class in Entity folder
This is TblUser class in my Models folder with Data Annotations applied
Here notice that I keep namespace and partial class declaration of class in Models Folder same as that of in my Entity Folder. Also associated the new class(TblUserMD) with the table class(TblUser) by using the MetadataTypeAttribute attribute found in System.ComponentModel.DataAnnotations. The associated class can have any name, a convention is to append "MD" or "MetaData" to the table class name. The associated class must be used with EDM or LINQ-to-SQL models because CLR types cannot mark existing properties with new attributes.
Now when I use this model with view like this
@model PreservingDataAnnotation.Entity.TblUser
All Data Annotations used in model get applied to my view like this
So next time I recreate EDMX file my Data Annotations keep safe in class in Models folder.
I hope this article will help you. Please leave comments to improve this article, any suggestion also welcomed.




No comments:
Post a Comment