Mark a field as Required

15 posts, 0 answers
  1. Bilal Haider
    Bilal Haider avatar
    9 posts
    Member since:
    Aug 2012

    Posted 03 Jan 2013 Link to this post

    I m using OA ORM with MVC4

    I need to explicitly mark the fields as required for the validation in MVC to take place

    public partial class Person
    {
       [Required]
       int _PId;
     
       [Required]
       string _PName;

    in the MVC view, based on the default configuration, only int columns are being  verified as required. The non-null string columns are not marked as required ( i have not verified for other data types )

    Edited:
    I am using Database first approach
  2. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 08 Jan 2013 Link to this post

    Hello Bilal,

    Such an opportunity is possible with Telerik OpenAccess ORM, though it is not available out of the box. We offer a custom code generation text template (the file is attached at the end of the post) that allows data annotations. The annotation attributes it supports are:
    - KeyAttribute
    - RequiredAttribute
    - StringLenghtAttribute

    The template should be included in your domain model project and requires several simple steps in order to generate the necessary code:
    1) Add the OpenAccessMetadataTemplate.tt template to the project and set its Custom Tool to TextTemplatingFileGenerator if not set.
    2) Add a reference to the System.ComponentModel.DataAnnotations.dll assembly.
    3) Change line 11 as follows:
    For Visual Studio 2010:
    <#@ Assembly Name="C:\Program Files (x86)\Telerik\OpenAccess ORM\dsl2010\Telerik.OpenAccess.Common.UI.dll" #>
    For Visual Studio 2012:
    <#@ Assembly Name="C:\Program Files (x86)\Telerik\OpenAccess ORM\dsl2012\Telerik.OpenAccess.Common.UI.dll" #>
    Make sure to replace the value of the assembly name with the full path to the Telerik.OpenAccess.Common.UI assembly on YOUR machine (the assumption is that OpenAccess might be installed in a directory other than Program Files).
    4) Change line 12 as follows:
    <#@ Assembly Name="C:\Program Files (x86)\Telerik\OpenAccess ORM\bin\Telerik.OpenAccess.dll" #>
    Again make sure to replace the value of the assembly name with the full path to the Telerik.OpenAccess assembly on YOUR machine.
    5) Change line 43 as follows: replace "Path To Rlinq File Here" with the full path to your .rlinq file.
    6) Change line 42 as follows: replace the items of the string array with the names of the domain entities you want to extend (e.g. the Person class).
    7) Transform the OpenAccessMetadataTemplate.tt template either by saving it or by invoking the Transform All Templates command in Visual Studio.
    8) Open the generated OpenAccessMetadataTemplate.generated.cs file and add the attributes you need wherever you need them.
    9) Save the OpenAccessMetadataTemplate.generated.cs file.

    If you make any changes in the domain model that affect the data annotation (like addition of new column or change of the allowed string length) you should consider regenerating the data annotation code by invoking Transform All Templates.

    Keep in mind that changes made in the generated code file could easily get lost if the file is regenerated!
    If you need one-time code generation please delete/exclude from project the OpenAccessMetadataTemplate.tt in order to prevent any code loss in the generated file.

    If you want to add additional data annotation attributes you can do it in the generated file, but make sure that the text template won't overwrite your changes.

    If you experience any difficulties with the provided solution or have any other questions, do not hesitate to get back to us.

    All the best,
    Doroteya
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  3. DevCraft banner
  4. Bilal Haider
    Bilal Haider avatar
    9 posts
    Member since:
    Aug 2012

    Posted 08 Jan 2013 Link to this post

    Thanx for such a detailed and extensive reply.

    I will let u know once i hav tried this
  5. Bilal Haider
    Bilal Haider avatar
    9 posts
    Member since:
    Aug 2012

    Posted 09 Jan 2013 Link to this post

    I have got a workaround for this issue based on MetadataType as mentioned in a StackOverFlow question
    and it is working fine (uptill now ;) )

    Although it is simple and straight forward, the only issue is I have to create the MetadataType class for each of the required Model Class.

    I was wondering which of these two methods is helpful in the long run

  6. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 10 Jan 2013 Link to this post

    Hi Bilal,

    Generally, our solution and the one from Stack Overflow implement the same concept and produce the same outcome. In other words, in long term perspective it does not matter which one you will choose.

    Keep in mind, however, that the benefit from our solution is that with a few simple changes in the template you get the whole necessary code generated for you. That saves you the time and the effort you would invest in the other solution.

    In this message, I attach a second version of the template that currently generates not only the structure of the buddy classes but the attributes as well (you could compare the two files and find the differences between them from line 85 on).

    If you have any other questions, do not hesitate to get back to us. 


    Kind regards,
    Doroteya
    the Telerik team
    Q3'12 SP1 of OpenAccess ORM packs Multi-Table Entities mapping support. Check it out.
  7. Bilal Haider
    Bilal Haider avatar
    9 posts
    Member since:
    Aug 2012

    Posted 22 Jan 2013 Link to this post

    Its really nice to see such a detailed and code-containing reply from the support even for the second time.

    i have been working on other modules of the project.

    When the work related to this validation will resume, I will try to give the feedback.

    Thanx once again
  8. Mark
    Mark avatar
    20 posts
    Member since:
    Feb 2008

    Posted 11 Apr 2013 Link to this post

    How do you get your newly created partial class to work with OpenAccessModel.YourNameHere class?

    I'm trying to add

    [Required]
    [StringLength(50)]
    public System.String LastName { get; set; }

    attributes to my OpenAccessModel.Account class.
  9. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 16 Apr 2013 Link to this post

    Hi Mark,

    In order to generate the necessary attributes all you need to do is to download the OpenAccessMetadataTemplate - v2 attached to the previous post and to follow the instructions provided in the first reply of this thread.

    That way the properties of the classes will be decorated with the appropriate attributes and since the classes are declared partial you will be able to use them without any additional actions.

    I hope that helps. If you have other questions, do not hesitate to get back to us.

     

    Kind regards,
    Doroteya
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  10. Mark
    Mark avatar
    20 posts
    Member since:
    Feb 2008

    Posted 17 Apr 2013 Link to this post

    I have created the OpenAccessMetadataTemplate.generated.cs file. Do I just leave it where it is or put the code in the Models section?

    I played with the code in the Models section. My issue is, if I use this, then it effects using the EntitiesModel dbContext.

    If I add to the Models class
    [MetadataTypeAttribute(typeof(Account.AccountMetadata))]
      public partial class Account : OpenAccessModel.Account
      {

    I need to inherit from OpenAccessModel.Account right? If I do that, I get an error here because dbContext.Accounts is looking for OpenAccessModel.Account not my new object ProjectV1.Models.Account. Is there a way to cast dbContext.Accounts?
    ProjectV1.Models.Account obj;
     
         using (EntitiesModel dbContext = new EntitiesModel())
                {
                    // Use LINQ to retrieve Account with AccountID id.
                    obj = (from acc in dbContext.Accounts
                           where acc.UserId == (Guid)user.ProviderUserKey
                           select acc).FirstOrDefault();
                }

    Cannot implicitly convert type 'OpenAccessModel.Account' to 'ProjectV1.Models.Account'. An explicit conversion exists (are you missing a cast?)
  11. Bilal Haider
    Bilal Haider avatar
    9 posts
    Member since:
    Aug 2012

    Posted 17 Apr 2013 Link to this post

    Hi Mark,

    What i hav been able to understand from ur question is:

    to use the utilize the newly added attributes, just mark the existing class with the partial keyword and the attributes will be affective.

    [ i m not sure is this what u were asking ]

    also
    I hav been too occupied to the MVC Views related stuff that i hav not been able to proceed with the templates provided by support team.
  12. Mark
    Mark avatar
    20 posts
    Member since:
    Feb 2008

    Posted 17 Apr 2013 Link to this post

    I've gotten around the EntitiesModel dbContext issue by just matching manually one object to the other. Like this

    obj.Address1 = account.Address1;
    obj.Address2 = account.Address2;

    I feel there is a better way but this works.
  13. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 22 Apr 2013 Link to this post

    Hi Mark and Bilal,

    Billal, thank you for the answer.

    Mark, let me just add that Telerik OpenAccess ORM generates the domain classes as partial. That basically means that you could split the class definition between as many source files as needed (for example, as many as the members of a development team) and during compile time, the compiler will unite all the pieces into a single final type.

    In other words, if:
    1) the two parts of the class are in the same namespace and
    2) are in the same project and
    3) have the same signature (i.e. public partial class Account),
    You would not need to either move OpenAccessMetadataTemplate.generated.cs or inherit one of the parts of the class from the other in order to consume it.

    Could you please take a look at the following snippets:
    [MetadataTypeAttribute(typeof(Account.AccountMetadata))]
    public partial class Account
    {
       // The decorated properties of the class
    }
    Consuming the decorated class:
    using (EntitiesModel dbContext = new EntitiesModel())
    {
        // Use LINQ to retrieve Account with AccountID id.
        Account obj = (from acc in dbContext.Accounts
                where acc.UserId == (Guid)user.ProviderUserKey
                select acc).FirstOrDefault();
    }

    Here you can find more information about the partial class definition and here is a code library that can give you an overview of the end result.

    I hope that helps. If you need any further assistance, do not hesitate to get back to us.


    All the best,
    Doroteya
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  14. Mark
    Mark avatar
    20 posts
    Member since:
    Feb 2008

    Posted 22 Apr 2013 Link to this post

    But I want to add more to the code that is generated like

        [Required]
             [StringLength(50)]
    [DataType(DataType.PhoneNumber)]
    [Display(Name = "Label_Phone", ResourceType = typeof(ProjectV1.Properties.Resources))]
    [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid number")]
    public System.String Phone { get; set; }

    How do I add on top of the  OpenAccessMetadataTemplate.generated code?
  15. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 25 Apr 2013 Link to this post

    Hi,

    The best practice to additionally customize the generated buddy classes, would be to cut-and-paste the content of OpenAccessMetadataTemplate.generated.cs in a new class file and to apply the necessary custom attributes to the properties. Just like OpenAccessMetadataTemplate.generated.cs, the new class file should be in the project that holds the .rlinq file.

    Let's look at the example with Account class. The workflow would include the following steps:
    1) Add a new class file in the project that holds the .rlinq file
    2) Cut the content of OpenAccessMetadataTemplate.generated.cs and paste it in the newly created class file
    3) Add the neccessary attributes to the properties that need them

    The result should be similar to this one:
    public partial class Account
    {
        internal sealed class AccountMetadata
        {
            private AccountMetadata()
            {
            }
     
            [DataType(DataType.PhoneNumber)]
            [Display(Name = "Label_Phone", ResourceType = typeof(ProjectV1.Properties.Resources))]
            [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", ErrorMessage = "Not a valid number")]
            [Required]
            [StringLength(40)]
            public System.String Phone { get; set; }
        }
    }

    Note that if you perform changes in the domain model that require the modification of the data annotations, you will need to re-generate the OpenAccessMetadataTemplate.generated.cs file and repeat the process again.

    I hope that helps. If you have other questions, do not hesitate to get back to us.


    Regards,
    Doroteya
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  16. Doroteya
    Admin
    Doroteya avatar
    502 posts

    Posted 26 Apr 2013 Link to this post

    Hi,

    The previous post in this thread was modified due to the fact that the suggested workflow did not produce the required outcome. Currently, it contains the steps and the example that would help you to meet your goals.

    Please accept our apologies for the caused inconvenience.

    Greetings,
    Doroteya
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
Back to Top
DevCraft banner