Don't get why it keeps throwing an System.InvalidOperationException

asp.net-mvc c# entity-framework-core

Question

I'm kinda new to MVC entity framework core and I've been struggeling with this exception and I don't know what's causing it.

I always get the same error: System.InvalidOperationException: 'No suitable constructor found for entity type 'Rating'. The following parameters could not be bound to properties of the entity: 'date', 'message', 'suggestion'.'

The error is thrown in the GetAll() method:

public class RatingRepository : IRatingRepository {
    private readonly ApplicationDbContext _context;

    public RatingRepository(ApplicationDbContext context) {
        _context = context;
    }

    public void Add(Rating rating) {
        var any = _context.Ratings.Any(r => r.RatingId == rating.RatingId);
        if (!any) {
            _context.Add(rating);
        }

    }

    public IEnumerable<Rating> GetAll() {
        return _context.Ratings.ToList();
    }

    public IEnumerable<Rating> GetFirst(int amount) {
        return GetAll().Take(amount).ToList();
    }

    public void Remove(Rating rating) {
        var any = _context.Ratings.Any(r => r.RatingId == rating.RatingId);
        if (any) {
            _context.Remove(rating);
        }

    }

    public void SaveChanges() {
        _context.SaveChanges();
    }
}

This is the interface the repository implements:

public interface ICodeRepository {
    IEnumerable<string> GetAll();
    void Remove(string code);
    void SaveChanges();
}

This is my Rating class:

 public class Rating {
    #region Fields
    private double _foodRating;
    private double _atmosphereRating;
    #endregion

    #region Properties
    public int RatingId { get; set; }
    public double FoodRating {
        get {
            return _foodRating;
        }
        private set {
            if (value < 0.0 || value > 5.0) {
                throw new ArgumentException("Give a score between 0 and 5 please.");
            }
            _foodRating = value;
        }
    }
    public double AtmosphereRating {
        get {
            return _atmosphereRating;
        }
        private set {
            if (value < 0.0 || value > 5.0) {
                throw new ArgumentException("Give a score between 0 and 5 please.");
            }
            _atmosphereRating = value;
        }
    }
    public string PersonalMessage { get; set; } //not mandatory
    public string Suggestions { get; set; } //not mandatory 
    #endregion

    #region Constructors
    public Rating(double foodRating, double atmosphereRating, DateTime date, string message = null, string suggestion = null) {
        FoodRating = foodRating;
        AtmosphereRating = atmosphereRating;
        PersonalMessage = message;
        Suggestions = suggestion;
    }
    #endregion

}

This is how I mapped it to the database:

 public class RatingConfiguration : IEntityTypeConfiguration<Rating> {
    public void Configure(EntityTypeBuilder<Rating> builder) {
        builder.ToTable("Rating");

        builder.HasKey(r => r.RatingId);

        builder.Property(r => r.PersonalMessage)
            .HasMaxLength(250)
            .IsRequired(false);

        builder.Property(r => r.Suggestions)
            .HasMaxLength(250)
            .IsRequired(false);
    }
}

In my ApplicationDb have I forseen a DbSet and in the OnModelCreating have I given the RatingConfiguration with.

I need to bind this to a form result so here is my RatingViewModel:

public class RatingViewModel {
    #region Properties
    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "How would you rate the food?")]
    public double FoodRating { get; set; }

    [Required]
    [DataType(DataType.Text)]
    [Display(Name = "How would you rate the atmosphere?")]
    public double AtmosphereRating { get; set; }

    [Display(Name = "Personal message")]
    [DataType(DataType.Text)]
    [StringLength(250, ErrorMessage = "Suggestion is needs to be between 250 and 0 characters")]
    public string PersonalMessage { get; set; }

    [Display(Name = "Any suggestions for next time?")]
    [DataType(DataType.Text)]
    [StringLength(250,ErrorMessage = "Suggestion is needs to be between 250 and 0 characters")]
    public string Suggestions { get; set; }
    #endregion

    #region Constructor
    public RatingViewModel() {

    }

    public RatingViewModel(Rating rating) : this() {
        FoodRating = rating.FoodRating;
        AtmosphereRating = rating.AtmosphereRating;
        PersonalMessage = rating.PersonalMessage;
        Suggestions = rating.Suggestions;
    }
    #endregion
}

Now if any of you can find why this exception is thrown please let me know. If you need anything else from my code, also let me know!

1
0
1/27/2019 3:48:08 PM

Accepted Answer

Entity framework will load data and tries to create the mapped objects. However if you specify a constructor, EF will try to use it, but it does not know how to handle your parameters. To fix this, simply add an empty constructor.

1
1/27/2019 3:48:22 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow