尝试通过EF Core在我的数据库上添加内容时出现此错误。
System.InvalidOperationException:'找不到实体类型'HealthCheck'的合适构造函数。以下构造函数具有无法绑定到实体类型属性的参数:无法在'HealthCheck(字符串标题,字符串hctype,字符串链接)'中绑定'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; }
}
}
我的RepositoryContext
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");
}
}
}
我的存储库
using Application.Models;
namespace Application.Repository
{
public class Repository
{
public void InsertHealthCheck(HealthCheck healthCheck)
{
using (var db = new RepositoryContext())
{
db.Add(healthCheck);
db.SaveChanges();
}
}
}
}
这就是调用“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);
}
}
如果你能帮助我,我会很感激,如果你需要我发布代码的任何其他部分只是问。
另外,我刚刚开始学习EF Core,所以如果我做了一些非常愚蠢的事情,请指出它
你缺少空构造函数:
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; }
}
试试吧
由于HealthCheck
类在数据库中表示一个表,因此您必须提供一个空构造函数。
尝试以下实现,如果您仍然收到相同的错误,请告诉我:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Application.Models
{
public class HealthCheck
{
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; }
}
}