Get data length using Entity Framework (for checking data length in WCF service)

Let's say I have a table that looks like this:

create table #Employee ( EmployeeId int identity(1,1) not null, FirstName varchar(100) not null, LastName varchar(100) not null, Suffix varchar(10) not null, Prefix varchar(10) not null, Address varchar(500) null) 

And I populate this table with WCF (Soap) service. My data contract is as follows:

 [DataContract] public class Employee { [DataMember] public virtual string FirstName { get; set; } [DataMember] public virtual string LastName { get; set; } [DataMember] public virtual string Suffix { get; set; } [DataMember] public virtual string Prefix { get; set; } [DataMember] public virtual string Address { get; set; } } 

The contract has a String for all of these fields. A string can be no more than 1,073,741,823 characters . This is far beyond the limits that the database will allow. When a client sends data, they can insert really very long values ​​into my contract.

I would like to find this before trying (and failing) to insert.

I have a very good internal validation structure that will send an error to the client. And I can go and check the correctness of each element. But if I rigidly set the lengths of the fields, I see that he gets out of control very quickly. (If the length of the column must be changed at two points, then I open the way for errors when only one or the other changes.)

Is there a way (with entity infrastructure) to get the length of a data item? So can I check it before trying to save it?

Or there is another way to do this check (other than just insert failure).

+6
source share
1 answer

You can use MetadataWorkspace and handle the SavingChanges event in an ObjectContext.

Maximum Field Length in Entity Infrastructure

0
source

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


All Articles