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

C#

 
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 
In this case, you could probably implement the validation by adding a navigation property:
public class Order : IValidateObject
{
    public int Id { get; set; }
    public int ItemId { get; set; }
    public int Quantity { get; set; }
    
    public virtual Item Item { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (Quantity % Item.Multiple != 0)
        {
            yield return new ValidationResult("Quantity not multiple of item", new[] { Quantity });
        }
    }
}
However, you'd need to test to make sure the property was always set.

Alternatively, you could override the ValidateEntity method on your DbContext:
DbContext.ValidateEntity(DbEntityEntry, IDictionary<Object,Object>) Method (System.Data.Entity) | Microsoft Docs[^]

NB: This wouldn't work with EF Core - it no longer tries to validate the entities before saving them. If you wanted to validate the entities before trying to save them, you would have to write your own validation logic - for example:
Improving Model Validation for Entity Framework Core 2.0 | Ballard Software[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

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 
Questioncontinuous PING application ? Pin
auting8210-Feb-20 10:27
Memberauting8210-Feb-20 10:27 
AnswerRe: continuous PING application ? Pin
k505410-Feb-20 11:48
Memberk505410-Feb-20 11:48 
GeneralRe: continuous PING application ? Pin
auting8211-Feb-20 10:31
Memberauting8211-Feb-20 10:31 
GeneralRe: continuous PING application ? Pin
k505411-Feb-20 11:27
Memberk505411-Feb-20 11:27 
GeneralRe: continuous PING application ? Pin
auting8211-Feb-20 11:48
Memberauting8211-Feb-20 11:48 
AnswerRe: continuous PING application ? Pin
Gerry Schmitz10-Feb-20 14:56
mveGerry Schmitz10-Feb-20 14:56 
QuestionModifying multiple fields based on a condition Pin
Bice908-Feb-20 2:07
MemberBice908-Feb-20 2:07 
AnswerRe: Modifying multiple fields based on a condition Pin
Richard MacCutchan8-Feb-20 2:41
communityengineerRichard MacCutchan8-Feb-20 2:41 
GeneralRe: Modifying multiple fields based on a condition Pin
Bice908-Feb-20 5:10
MemberBice908-Feb-20 5:10 

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.