none
How to add Validations after we create database model ? RRS feed

  • Question

  • Hi,

    I have created database first and then created model using "Add new item" -> "ADO.NET entity data model".

    Now I wish to add some validations rules on the fields, but i do not find any article or video talking about adding validations rules after you create database model.  all articles refer to 'Code First' approach which i am not using.

    Can anyone guide please.

    Regards

    Jiya.

    Monday, December 17, 2012 3:47 AM

All replies

  • Hi Jiya,

    Welcome to the MSDN forum.

    Do you use DbContext? If so, I recommend you try to implement Validation in the Context with ValidateEntity: http://msdn.microsoft.com/en-us/data/gg193959.aspx

    DbContext has an Overridable method called ValidateEntity. When you call SaveChanges, Entity Framework will call this method for each entity in its cache whose state is not Unchanged. You can put validation logic directly in here or even use this method to call.

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 18, 2012 6:09 AM
  • Hi,

    OK, I should have mentioned few things... I am using VS2010 with SP1. and .net 4.0

    and i observe the EF Designer model generates ObjectContext.  So i assume atleast i am not using EF 5.

    1. but otherwise is there anyway to know if i am using 4.0 or  4.3.1 ?

    2. I prefer Database First, so now since its ObjectContext, how to add Validation rules.

    Regards

    Tuesday, December 18, 2012 6:27 PM
  • Hi,

    just open your Tools tab in VS2010

    Tools=>LibraryPackageManager=>PackageManagerConsole=>

    in the console just update your package using Update-Package/Install-Package . 

    Type get-help NuGet   command to get the help.

    check your references now . Your dlls will be updated to latest version.

    Hope this helps.


    -Arun

    Wednesday, December 19, 2012 7:19 AM
  • Hi Jiya,

    The new features of Entity Framework 5 requires you to use Visual Studio 2012 (.NET Framework 4.5), so, for Visual Studio 2010, after you add EntityFramework.dll via Package Manager Console, its version is 4.4.0.0. In addition, for Visual Studio 2010, by default, the context is generated based on ObjectContext. If you want to generate DbContext, you can right click the empty area on model designer surface>>Add Code Generation Item…>>EF 4.x DbContext Generator (If you have not installed it, you can search and download it from Online Templates).

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, December 19, 2012 9:01 AM
  • Hi

    Thanks Alexander ,  I got the answer about "EF 4.x DbContext Generator" from one of my previous question post, which you answered. Thanks for that.

    This post was more about How to add Validation Rule in Database First Model after Model is created. 
    and in VS2010 with .NET 4.0 and EF 4.0

    Regards


    • Edited by JiyaDesai Wednesday, December 19, 2012 9:52 AM
    Wednesday, December 19, 2012 9:51 AM
  • Hi Jiya,

    Now you have made your context class derived from DbContext. Please try to implement Validation in the Context with ValidateEntity:http://msdn.microsoft.com/en-us/data/gg193959.aspx

    Merry Christmas!


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 24, 2012 9:21 AM