Click here to Skip to main content
15,886,258 members
Home / Discussions / C#
   

C#

 
QuestionDifferent value from input Pin
chipp_zanuff14-Feb-20 6:15
chipp_zanuff14-Feb-20 6:15 
AnswerRe: Different value from input Pin
OriginalGriff14-Feb-20 6:32
mveOriginalGriff14-Feb-20 6:32 
GeneralRe: Different value from input Pin
chipp_zanuff14-Feb-20 17:48
chipp_zanuff14-Feb-20 17:48 
GeneralRe: Different value from input Pin
OriginalGriff14-Feb-20 20:21
mveOriginalGriff14-Feb-20 20:21 
AnswerRe: Different value from input Pin
Eddy Vluggen14-Feb-20 6:35
professionalEddy Vluggen14-Feb-20 6:35 
Questionauto fill data by using 3 layer in aso.net c# Pin
Member 1474357913-Feb-20 22:47
Member 1474357913-Feb-20 22:47 
AnswerRe: auto fill data by using 3 layer in aso.net c# Pin
OriginalGriff13-Feb-20 23:39
mveOriginalGriff13-Feb-20 23:39 
QuestionWhat is the correct way to do Business Validation on entities? Pin
Bastien Vandamme11-Feb-20 15:00
Bastien Vandamme11-Feb-20 15: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.
C#
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 19:48
mveGerry Schmitz11-Feb-20 19:48 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming11-Feb-20 23:47
mveRichard Deeming11-Feb-20 23:47 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Gerry Schmitz12-Feb-20 2:16
mveGerry Schmitz12-Feb-20 2:16 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming12-Feb-20 2:30
mveRichard Deeming12-Feb-20 2:30 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Gerry Schmitz12-Feb-20 2:42
mveGerry Schmitz12-Feb-20 2:42 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming12-Feb-20 2:52
mveRichard Deeming12-Feb-20 2:52 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
phil.o12-Feb-20 2:56
professionalphil.o12-Feb-20 2:56 
GeneralRe: What is the correct way to do Business Validation on entities? Pin
Gerry Schmitz12-Feb-20 3:06
mveGerry Schmitz12-Feb-20 3:06 
AnswerRe: What is the correct way to do Business Validation on entities? Pin
Richard Deeming12-Feb-20 0:03
mveRichard Deeming12-Feb-20 0:03 
QuestionHow do create a class property that is a collection of the same class? Pin
Member 1474182311-Feb-20 8:04
Member 1474182311-Feb-20 8:04 
AnswerRe: How do create a class property that is a collection of the same class? Pin
Richard Deeming11-Feb-20 8:46
mveRichard Deeming11-Feb-20 8:46 
GeneralRe: How do create a class property that is a collection of the same class? Pin
Dave Kreskowiak11-Feb-20 9:02
mveDave Kreskowiak11-Feb-20 9:02 
GeneralRe: How do create a class property that is a collection of the same class? Pin
Member 1474182312-Feb-20 0:04
Member 1474182312-Feb-20 0:04 
GeneralRe: How do create a class property that is a collection of the same class? Pin
Richard Deeming12-Feb-20 0:15
mveRichard Deeming12-Feb-20 0:15 
AnswerRe: How do create a class property that is a collection of the same class? Pin
Gerry Schmitz11-Feb-20 14:14
mveGerry Schmitz11-Feb-20 14:14 
AnswerRe: How do create a class property that is a collection of the same class? Pin
Member 1474182312-Feb-20 0:51
Member 1474182312-Feb-20 0:51 
QuestionHow to Pass the textbox value into Textbox1 in Web Browser in WPF Pin
Hernany Bondoc11-Feb-20 6:27
Hernany Bondoc11-Feb-20 6: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.