none
Can I use data annotation validation for database first application?

    Soru

  • I have seen examples of data annotation validation (Entity Framework 4.1) only for code first senario.  Can I use data annotation validation for database first application?  Is there a sample app that I can use as a reference if the answer is yes?  Thanks.
    30 Ocak 2012 Pazartesi 15:31

Tüm Yanıtlar

  • Hi mystique99;

    I have not done this myself but there is a post in the Microsoft ASP.Net web site that show how this can be done. Please see link below.

    Validation with the Data Annotation Validators (C#)
    in the section called "Using Data Annotation Validators with the Entity Framework" near the bottom of the page.

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    30 Ocak 2012 Pazartesi 18:03
  • Thanks for your reply.  I tried the [MetadataType] approach in a prototype.  It doesn't seem to work for me.  I am not sure what am I missing.

    30 Ocak 2012 Pazartesi 21:35
  • Hi mystique99,

    Welcome!

    You can use "Buddy Classes" to validate database first, you can refer this link here: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Yanıt Olarak İşaretleyen Alan_chenModerator 09 Şubat 2012 Perşembe 07:08
    • Yanıt İşaretini Geri Alan mystique99 07 Mart 2012 Çarşamba 14:55
    31 Ocak 2012 Salı 07:01
  • Thanks for reply again.  Examples are for MVC project.  Anything non MVC samples?
    31 Ocak 2012 Salı 15:50
  • Hi mystique99,

    =============================

    One way you can apply additional attribute-based meta-data (like validation attributes) to a class that is auto-generated/maintained by a VS designer is to employ a technique we call “buddy classes”. Basically you create a separate class that contains your validation attributes and meta-data, and then link it to the class generated by the designer by applying a “MetadataType” attribute to a partial class that is compiled with the tool-generated class. 

    =============================

    You can use Buddy classes in any applications.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Yanıt Olarak İşaretleyen Alan_chenModerator 09 Şubat 2012 Perşembe 07:08
    • Yanıt İşaretini Geri Alan mystique99 07 Mart 2012 Çarşamba 19:26
    01 Şubat 2012 Çarşamba 01:10
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    06 Şubat 2012 Pazartesi 02:08
  • Thanks for checking back with me.  I understand the idea of "buddy class" and have tried it in a prototype.  But somehow it didn't work for me so I am back to where I started.  Would you be able to post a simple sample program to demonstrate?  That will be very helpful.
    10 Şubat 2012 Cuma 13:28
  • Hi Alan,

    I am not sure those examples help me.  My application does not have an UI component, it is a web service.   I would like to use data annotation to do entity validation.  I did create a buddy class but it didn't seem to flag anything when I call saveChanges on the entity.

    Where can I get an example?  I can send you mine but I am not sure that I can upload my project here.

    Thanks.

    Mystique99 

    07 Mart 2012 Çarşamba 15:01
  • I think the following article does exactly what you need.

    http://blogs.infosupport.com/sharing-validation-code-between-a-wpf-client-a-wcf-service-and-entity-framework-4-1-using-data-annotations/

    I created a sample based on what it says (did not use all the steps, but the most important ones and it worked. )

    If you run into any problems (or if you are not familiar with .tt templates) please let me know. You can email me at juliako@microsoft.com and I can send you a project that I created with more explanations.

    Thank you,

    Julia


    This posting is provided "AS IS" with no warranties, and confers no rights.



    07 Mart 2012 Çarşamba 22:49
  • Julia,

    Thank you so much for your reply.  I haven't got  a chance to look at it yet and I will let you know if I have any questions.

    Mystique99

    26 Mart 2012 Pazartesi 13:56
  • Hi,

    If I remember a common issue is that the member in the buddy class must be also a property with the same name. Could it be that you created fields rather than properties in the buddy class ?

    Else try to show the shortest example that doesn't work. It will be easier to see your code and find out what doesn't work rather than to imagine where you could have missed something. BTW you told you wanted a non MVC sample but we are still not sure what is your context (Silverlight with WCF or WebForms maybe ? If the later this is not yet available (it works now with DynamicData but if I remember it should come in 4.5))


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    26 Mart 2012 Pazartesi 16:26
  • My project is a WCF service with no UI.  I am using entity framework for the data access layer.  I was hoping to use data annotation in Metatdata class to define validation rules.  When calling SaveChanges on the entity (derived from ObjectContext) object, I was hoping to catch a specific exception other than Exception to handle validation violation.  But it didn't work for me.

    Here is my metadata class for the Product entity using AdventureWorks database.  This follows the examples Julia pointed out above.

    namespace EntityDAL { [MetadataType(typeof(ProductMetadata))] public partial class Product { private class ProductMetadata { [Range(0, Double.MaxValue, ErrorMessage = "ListPrice can't be smaller than zero!")] public decimal ListPrice { get; set; } [MaxLength(7)] public global::System.String Name { get; set; } } } }

     

     

    See any problems?

    26 Mart 2012 Pazartesi 20:18