0

I have a DB that I need all data to be able to load into. Lets say Table name is Person, and person has a column SSID (Don't worry this isn't the real table).

I bulk copy the data into this table, and don't want any issues, so SSID is a nvarchar (8000). Then with EF I want to load this data into memory and validate that it's string lengths and such are correct, and fix them before pushing them to a final table (this part is not trivial, a transform is needed).

So my EF model:

public class Person
{
     [MinLength(9)]
     [MxnLength(9)]
     public string SSID { get; set;}

     //... more code
}

So since we're using code first, the MaxLength attribute will be used to determine the varchar length in the table, which we don't want to happen. I just want to be able to do Validator.Validate(person) and get each validation issue.

Whats the best way to go about this?

One thought is 2 models, one for the actual DB, and another for validation. Seems like data binding from one to another might be expensive/tedious.

Another thought is to re-implement MaxLength and other attributes we need to do the same thing, but EF probably wouldn't know what to do with it so it wouldn't attempt to make a schema change.

Another thought is maybe there is a setting in EF to ignore certain data annotations?

Thanks for any help.

Dave
  • 1,645
  • 2
  • 23
  • 39
  • possible duplicate of [StringLength vs MaxLength attributes ASP.NET MVC with Entity Framework EF Code First](http://stackoverflow.com/questions/5717033/stringlength-vs-maxlength-attributes-asp-net-mvc-with-entity-framework-ef-code-f) – Erik Philips May 22 '15 at 16:30
  • In a test project, if I use `MaxLength` or `StringLength` and scaffold a model, it will use that length for the varchar length. – Dave May 22 '15 at 16:31
  • What about [that](http://stackoverflow.com/questions/10572442/how-to-ignore-some-property-when-use-entity-framework-code-first) SO thread? – sszarek May 22 '15 at 17:11
  • Correct me if I'm wrong, but that looks like it's for not mapping the property. I want to mark the dataannotation itself to be ignored. – Dave May 22 '15 at 17:26

0 Answers0