How to get the string length for a server side field?

I have an edmx model created from a database and .cs metadata for it.

In the client, .g.cs includes the [StringLength(X)] attributes next to my attributes from my metadata.

I am doing some server-side validation to import a flat file, which is a separate client-side editor of these objects.

I can apply my range and regex checks, but I cannot find the StringLength attribute on the server. Does anyone know how to do this without duplicating the StringLength attributes manually in the metadata properties.

Edit:

Here is the code:

File on the side of the ProductService.metadata.cs server:

 internal sealed class PRODUCTMetadata { [Required] [RegularExpression("[AZ]+")] [Display(Name = "Product Code", Order = 10)] public string Product_code { get; set; } } 

Client side Generated_Code \ NameSpace.Web.g.cs:

 public sealed class PRODUCT { [DataMember()] [Display(Name="Product Code", Order=10)] [RegularExpression("[AZ]+")] [Required()] [StringLength(8)] //This is what I want to know, but server side public string Product_code {...etc } } 
+4
source share
1 answer

I studied this problem a bit and did not find any good information about the topic on the Internet. So what I will say here is just a guess .

As you have seen, automatically generated client proxy code is much more decorated with attributes than server-side code. For example, your objects have a nice attribute [StringLength(8)] , which comes from the Entity model. On the server side, the automatically generated .metadata.cs file does not have these attributes for entities. I think this is all about code generation patterns .

I suspect that the RIA Services code generation template (which creates the .g.cs file) is much more advanced than the template that creates the server-side .metadata.cs file.

The fact that the attribute that is missing in your case is 95% of the time used to validate the client-side UI, may explain why the template for the .metadata.cs file .metadata.cs not provide these validation attributes.

I see 2 problems for your problem:

1. Write your own server-side metadata class

Example:

 [MetadataTypeAttribute(typeof(PRODUCT.PRODUCTMetadata))] public partial class PRODUCT { internal sealed class PRODUCTMetadata { // Metadata classes are not meant to be instantiated. private PRODUCTMetadata() { } [StringLength(8)] public string Product_code { get; set; } } } 

You can manually add any attributes to the properties of your objects, since entities are partial classes.

Unfortunately, you will have to maintain this metadata every time you change your model: if (for example) the column of the database table changes from varchar(8) to varchar(10) , you can automatically update your EDMX model from your database, but you will have to manually verify that your metadata is still in order (in this example, you will have to manually replace [StringLength(8)] with [StringLength(9)] ).

Here's a good link about metadata.

2. Change T4 templates

The second option is probably the best, but I have not experienced a modification of the code generation template, so I do not know what can be done efficiently or not.

Code generation templates are known as T4 templates ( Text Template Conversion Tool ). These patterns can be changed to include anything in the code generation process. You can change the default EF template to generate missing attributes, as the RIA services template does.

Here are some good articles about generating T4 code:


I write this as an answer (it does not fit as a comment) , but remember all the assumptions .

+2
source

Source: https://habr.com/ru/post/1395235/


All Articles