6

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 fill this table with a WCF (Soap) service. My data contract looks like this:

[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 String for all of these fields. A string can be a max of about 1,073,741,823 chars. That is well over the limits that the database will allow. When a client sends up data they can put in really really long values into my contract.

I would like to find this out before I attempt (and fail) an insert.

I have a very nice in-house made validation framework that will send a Fault down to the client. And I could go and put in a validation for each item. But if I hardcode the lengths of the fields then I see it spiraling out of control very very fast. (If the length of a column has to be changed in two spots, then I am opening up the way for bugs when only one or the other gets changed.)

Is there a way (with entity framework) to get the lengths of a data item? So I can validate it prior to trying to persist it?

Or is there another way to do this validation (except by just failing the insert).

Community
  • 1
  • 1
Vaccano
  • 78,325
  • 149
  • 468
  • 850

1 Answers1

0

You can use the MetadataWorkspace and handle the SavingChanges event on your ObjectContext.

Field max length in Entity framework

Community
  • 1
  • 1
Jeff
  • 35,755
  • 15
  • 108
  • 220