我是MVC实体框架核心的新手,我一直在为这个异常而苦苦挣扎,我不知道是什么原因造成的。
我总是得到相同的错误:System.InvalidOperationException:'找不到适合实体类型'Rating'的合适的构造函数。以下参数无法绑定到实体的属性:“日期”,“消息”,“建议”。
在GetAll()方法中引发了错误:
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();
}
}
这是存储库实现的接口:
public interface ICodeRepository {
IEnumerable<string> GetAll();
void Remove(string code);
void SaveChanges();
}
这是我的评分等级:
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
}
这是我将其映射到数据库的方式:
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);
}
}
在我的ApplicationDb中,我预见了一个DbSet,在OnModelCreating中,我给出了RatingConfiguration。
我需要将其绑定到表单结果,所以这是我的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
}
现在,如果您能找到引发此异常的原因,请告诉我。如果您还需要我的代码中的其他内容,也请告诉我!
答案 0 :(得分:1)
实体框架将加载数据并尝试创建映射的对象。但是,如果指定构造函数,EF将尝试使用它,但它不知道如何处理参数。要解决此问题,只需添加一个空的构造函数。