System.InvalidOperationException: '找不到适合实体类型 'HealthCheck' 的构造函数

Fac*_*ero 3 c# asp.net model-view-controller entity-framework-core

尝试通过 EF Core 在我的数据库上添加内容时出现此错误。

System.InvalidOperationException: '找不到适合实体类型 'HealthCheck' 的构造函数。以下构造函数的参数无法绑定到实体类型的属性:无法绑定“HealthCheck(string title, string hctype, string link)”中的“hctype”。

这是我的 HealthCheck 课程:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Application.Models
{
    public class HealthCheck
    {
        public HealthCheck(string title, string hctype, string link)
        {
            Title = title;
            HCType = hctype;
            Link = link;
        }

        public int Id { get; set; }
        public string Title { get; set; }
        public string HCType { get; set; }
        public string Link { get; set; }
    }
}

Run Code Online (Sandbox Code Playgroud)

我的存储库上下文

using Microsoft.EntityFrameworkCore;
using Application.Models;

namespace Application.Repository
{
    public class RepositoryContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(
                @"Server=(localdb)\mssqllocaldb;Database=healthcheck;Integrated Security=True");
        }

        //public DbSet<HealthCheck> HealthChecks { get; set; }
        //public DbSet<UserHealthCheck> UserHealthChecks { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<HealthCheck>().ToTable("HealthCheck");
            modelBuilder.Entity<UserHealthCheck>().ToTable("UserHealthCheck");
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

我的仓库

using Application.Models;

namespace Application.Repository
{
    public class Repository
    {
        public void InsertHealthCheck(HealthCheck healthCheck)
        {
            using (var db = new RepositoryContext())
            {
                db.Add(healthCheck);
                db.SaveChanges();
            }
        }
    }
}

Run Code Online (Sandbox Code Playgroud)

这就是调用“InsertHealthCheck()”的地方

[Route("/api/HealthCheck/Website")]
        [HttpPost]
        public ActionResult WebsiteStatus([FromBody] WebsiteDataModel websiteData)
        {
            HealthCheck data = new HealthCheck(websiteData.Title, "Website", websiteData.Url);
            try
            {
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(websiteData.Url);
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                HttpStatusCode HealthCheckStatusCode = myHttpWebResponse.StatusCode;
                myHttpWebResponse.Close();
                return Ok(HealthCheckStatusCode);
            }
            catch(UriFormatException)
            {
                return Ok("Check url.");
            }
            catch (Exception)
            {
                return Ok("400");
            }
            finally
            {
                repository.InsertHealthCheck(data);
            }
        }

Run Code Online (Sandbox Code Playgroud)

如果您能帮我一把,我将不胜感激,如果您需要我发布代码的任何其他部分,请询问。

另外,我刚开始学习 EF Core,所以如果我做了一些非常愚蠢的事情,请指出

小智 7

提供无参数构造函数可以避免这个问题,但这并不是 OP 案例中错误的真正原因。 EF Core 2.1 及更高版本使用严格的约定将构造函数参数映射到实体的属性名称。 它期望构造函数参数的名称是 Pascal-case 中属性名称的真正驼峰式表示。如果您将参数名称从“hctype”更改为“hCType”,如果您的域驱动设计方法表明这样做会有问题,则不应出现错误并且不必提供无参数构造函数。

但是,如果您只是为了方便而提供参数化构造函数,但调用者能够使用“new”运算符实例化 HealthCheck 并没有错,那么简单地添加无参数构造函数是可以接受的。


Mil*_*vic 5

您缺少空构造函数:

public class HealthCheck
{
   // here
   public HealthCheck()
   {
   }

   public HealthCheck(string title, string hctype, string link)
   {
       Title = title;
       HCType = hctype;
       Link = link;
   }

   public int Id { get; set; }
   public string Title { get; set; }
   public string HCType { get; set; }
   public string Link { get; set; }


}
Run Code Online (Sandbox Code Playgroud)

试试看