Click here to Skip to main content
14,456,102 members
   

C#

 
QuestionDifferent value from input Pin
chipp_zanuff14-Feb-20 7:15
Memberchipp_zanuff14-Feb-20 7:15 
AnswerRe: Different value from input Pin
OriginalGriff14-Feb-20 7:32
communityengineerOriginalGriff14-Feb-20 7:32 
GeneralRe: Different value from input Pin
chipp_zanuff14-Feb-20 18:48
Memberchipp_zanuff14-Feb-20 18:48 
GeneralRe: Different value from input Pin
OriginalGriff14-Feb-20 21:21
communityengineerOriginalGriff14-Feb-20 21:21 
AnswerRe: Different value from input Pin
Eddy Vluggen14-Feb-20 7:35
professionalEddy Vluggen14-Feb-20 7:35 
Questionauto fill data by using 3 layer in aso.net c# Pin
Member 1474357913-Feb-20 23:47
MemberMember 1474357913-Feb-20 23:47 
AnswerRe: auto fill data by using 3 layer in aso.net c# Pin
OriginalGriff14-Feb-20 0:39
communityengineerOriginalGriff14-Feb-20 0:39 
QuestionWhat is the correct way to do Business Validation on entities? Pin
Bastien Vandamme11-Feb-20 16:00
MemberBastien Vandamme11-Feb-20 16:00 
Hi,

What is the correct way to do Business Validation on entities with Entity Framework 6. By business Validation I mean validation that require more than one entity in process with, sometime, a little more complex rules that involve all these entities. A validation that require the system to call he database.

I already checked this post:
ScottGu's Blog - Class-Level Model Validation with EF Code First and ASP.NET MVC 3

But, like most example about EF and validation all validations are limited to simple check that consider only the current entity. I need to do some check based on other entities.

Example: I have a order entity that contain a quantity of product but this quantity must be a multiple of a variable stored in my item entity. This is what I call the business validation. To perform this validation on order I need to go in database and check the item selected for this order.
public class Order : IValidateObject
{
    public int Id { get; set; }
    public int ItemId { get; set; }
    public int Quantity { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        var item = DbContext.Items.Find(ItemId); // <-- What about this?
        if (Quantity % item.Multiple != 0)
            yield return new ValidationResult("Quantity not multiple of item", new[] {Quantity});
    }
}

public class Item
{
    public int Id { get; set; }
    public int Multiple { get; set; }
}

How should I implement this kind of business validation that require other entities? I'm more looking for a good tutorial about this subject. I search but each time I think I find the subject the tutorial is limited to classic validation with Data Annotation or other type of classic validation.

Maybe you will juste tell me there is no problem to class my context in the Validate method. It is just that most of tutorial separate the entity layer from the context layer suggesting entities must only be simple POCO with no behavior. I most and most disagree with this but I feel alone.
AnswerRe: What is the correct way to do Business Validation on entities? Pin
Gerry Schmitz11-Feb-20 20:48
mveGerry Schmitz11-Feb-20 20:48 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming12-Feb-20 0:47
communityengineerRichard Deeming12-Feb-20 0:47 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Gerry Schmitz12-Feb-20 3:16
mveGerry Schmitz12-Feb-20 3:16 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming12-Feb-20 3:30
communityengineerRichard Deeming12-Feb-20 3:30 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Gerry Schmitz12-Feb-20 3:42
mveGerry Schmitz12-Feb-20 3:42 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming12-Feb-20 3:52
communityengineerRichard Deeming12-Feb-20 3:52 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
phil.o12-Feb-20 3:56
mvephil.o12-Feb-20 3:56 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Gerry Schmitz12-Feb-20 4:06
mveGerry Schmitz12-Feb-20 4:06 
AnswerRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming12-Feb-20 1:03
communityengineerRichard Deeming12-Feb-20 1:03 
QuestionHow do create a class property that is a collection of the same class? Pin
Member 1474182311-Feb-20 9:04
MemberMember 1474182311-Feb-20 9:04 
AnswerRe: How do create a class property that is a collection of the same class? Pin
Richard Deeming11-Feb-20 9:46
communityengineerRichard Deeming11-Feb-20 9:46 
GeneralRe: How do create a class property that is a collection of the same class? Pin
Dave Kreskowiak11-Feb-20 10:02
mveDave Kreskowiak11-Feb-20 10:02 
GeneralRe: How do create a class property that is a collection of the same class? Pin
Member 1474182312-Feb-20 1:04
MemberMember 1474182312-Feb-20 1:04 
GeneralRe: How do create a class property that is a collection of the same class? Pin
Richard Deeming12-Feb-20 1:15
communityengineerRichard Deeming12-Feb-20 1:15 
AnswerRe: How do create a class property that is a collection of the same class? Pin
Gerry Schmitz11-Feb-20 15:14
mveGerry Schmitz11-Feb-20 15:14 
AnswerRe: How do create a class property that is a collection of the same class? Pin
Member 1474182312-Feb-20 1:51
MemberMember 1474182312-Feb-20 1:51 
QuestionHow to Pass the textbox value into Textbox1 in Web Browser in WPF Pin
Hernany Bondoc11-Feb-20 7:27
MemberHernany Bondoc11-Feb-20 7:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.