开发者

Entity Framework 4.1 Server-side validation

The data I work with can be added from different interfaces. So in my MVC application I need to validate that the data read from the database is correct according to the rules/attribute I sat in my model. Depending on the validation send t开发者_开发百科he user to different Views.

I need to do this validation on the SERVER-SIDE

var myModelClassObj = myDbContextInheritedModelClass.theDbSetClassMapToModel.Find(123);
// How do i validat this


The DbContext class has a protected ValidateEntity method. You can call that method to validate your entity.

public class myDbContextInheritedModelClass :  DbContext
{
     public DbEntityValidationResult Validate(object entity)
     {
         return ValidateEntity(Entry(entity));
     }
}

Then

var myModelClassObj = myDbContextInheritedModelClass
                         .theDbSetClassMapToModel.Find(123);
var validationResult = myDbContextInheritedModelClass.Validate(myModelClassObj);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜