Error in Validationresult when trying to create Migration

Asked

Viewed 51 times

-3

One or more validation errors were detected during model generation:

Sigma.Infra.Data.Context.ValidationResult: : EntityType 'ValidationResult' has no key defined. Define the key for this EntityType.
Sigma.Infra.Data.Context.ValidationFailure: : EntityType 'ValidationFailure' has no key defined. Define the key for this EntityType.
ValidationResult: EntityType: EntitySet 'ValidationResult' is based on type 'ValidationResult' that has no keys defined.
ValidationFailures: EntityType: EntitySet 'ValidationFailures' is based on type 'ValidationFailure' that has no keys defined.

Class where Validationresult is, but cannot be changed

 #region Assembly FluentValidation, Version=8.0.0.0, Culture=neutral, 
 PublicKeyToken=7de548da2fbae0f0
 // 
  #endregion


 using FluentValidation.Results;

 using System.Collections.Generic;


  
  namespace FluentValidation.Results
  {

    // Summary:
    //     The result of running a validator
    public class ValidationResult
    {
        //
        // Summary:
        //     Creates a new validationResult
        public ValidationResult();
        //
        // Summary:
        //     Creates a new ValidationResult from a collection of failures
        //
        // Parameters:
        //   failures:
        //     List of FluentValidation.Results.ValidationFailure which is later available through
        //     FluentValidation.Results.ValidationResult.Errors. This list get's copied.
        //
        // Remarks:
        //     Every caller is responsible for not adding null to the list.
        public ValidationResult(IEnumerable<ValidationFailure> failures);

        //
        // Summary:
        //     Whether validation succeeded
        public virtual bool IsValid { get; }
        //
        // Summary:
        //     A collection of errors
        public IList<ValidationFailure> Errors { get; }
        public string[] RuleSetsExecuted { get; }

        //
        // Summary:
        //     Generates a string representation of the error messages separated by new lines.
        public override string ToString();
        //
        // Summary:
        //     Generates a string representation of the error messages separated by the specified
        //     character.
        //
        // Parameters:
        //   separator:
        //     The character to separate the error messages.
        public string ToString(string separator);
    }
}

Class of Iselfvalidation

using FluentValidation.Results;

namespace Sigma.Domain.Interfaces{

    /// <summary>
    /// Api de validação
    /// https://fluentvalidation.net
    /// </summary>
    /// 
    public interface ISelfValidation
    {
    
        ValidationResult GetValidationResult();
    
        bool IsValid { get; }
    }
    
}

This is the entity and the method that appears in the error is inside Iselfvalidation

public class ParametroArea : ISelfValidation
{

    public ParametroArea(){

        objID=Guid.NewGuid();

    }
    public Guid objID { get; set; }

    public Guid IDAreaServico { get; set; }

    public Guid IDUltimaCultura { get; set; }

    public string areaArrendada { get; set; }

    public string tempoRestante { get; set; }

    public string condicaoAtual { get; set; }

    public string observacaoComplementar { get; set; }
    [JsonIgnore]
    public virtual AreaServico AreaServico { get; set; }
    [JsonIgnore]
    public virtual Cultura Cultura { get; set; }

        private ValidationResult validationResult;
        public ValidationResult GetValidationResult()
        {
            return validationResult;
        }

        private void SetValidationResult(ValidationResult value)
        {
            validationResult = value;
        }

        public bool IsValid
        {
            get
            {
                var validador = new ParametroAreaValidation();

                this.SetValidationResult(validador.Validate(this));
                return GetValidationResult().IsValid;
            }
        }
    }
}
  • And the entityValidationResult has a primary key?

  • Can put the code of your entity ?

  • @Leandroangelo No, she has no primary key

  • @Matheus I can do it

1 answer

1

1º - try to put it this way:

[Key]
[Column(Order = 0)]
public Guid objID { get; set; }

2º - Remove the constructor that generates the Guid:

public ParametroArea(){

    objID=Guid.NewGuid();

}
  • Error continues, when I put the [Key] Validation asks for an Annotations and Iselfvalidations asks to implement an interface

  • @Jcsmz, yes you need to reference the Dataannotations package

  • Yes, I still made the mistake persisted

  • When exactly this error happens ?

  • When I will run the Migration, add-Migration parameter

  • It is strange, because setting the [Key] should work, tries to make these two changes that I posted in my question

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.