C# 外键不工作的自动验证核心2.1
我正在尝试在.NETCore2.1中设置一个API。我一直在关注这一点,并遇到了模型不能自动验证的问题(根据)。具体地说,我想确保,如果发送POST请求时带有不存在的ForeignKey,那么我应该得到BadRequest响应。如果发送了正确的外键,则正确处理请求 行动:C# 外键不工作的自动验证核心2.1,c#,mysql,.net-core,C#,Mysql,.net Core,我正在尝试在.NETCore2.1中设置一个API。我一直在关注这一点,并遇到了模型不能自动验证的问题(根据)。具体地说,我想确保,如果发送POST请求时带有不存在的ForeignKey,那么我应该得到BadRequest响应。如果发送了正确的外键,则正确处理请求 行动: 发送POST请求以添加不存在CustomerId的活动 预期的: 接收请求响应 实际值: 由于未处理的DbUpdateException,API崩溃 我的文件如下所示: Context.cs public class
发送POST请求以添加不存在CustomerId的活动 预期的:
接收请求响应 实际值:
由于未处理的DbUpdateException,API崩溃 我的文件如下所示: Context.cs
public class Context : DbContext
{
public Context(DbContextOptions<Context> options)
: base(options)
{ }
public DbSet<Customer> Customers { get; set; }
public DbSet<Campaign> Campaigns { get; set; }
}
{
public class Customer
{
public long Id { get; set; }
public string Name { get; set; }
public List<Campaign> Campaigns { get; set; }
}
}
[Route("api/[controller]")]
[ApiController]
public class CampaignController : ControllerBase
{
private readonly Context _context;
public CampaignController(Context context)
{
_context = context;
}
//Get Methods
[HttpPost]
public IActionResult Create(Campaign campaign)
{
_context.Campaigns.Add(campaign);
_context.SaveChanges();
return CreatedAtRoute("GetCustomer", new { id = campaign.Id }, campaign);
}
}
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var connection = @"Server=(localdb)\mssqllocaldb;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<Context>(options => options.UseSqlServer(connection));
}
Customer.cs
public class Context : DbContext
{
public Context(DbContextOptions<Context> options)
: base(options)
{ }
public DbSet<Customer> Customers { get; set; }
public DbSet<Campaign> Campaigns { get; set; }
}
{
public class Customer
{
public long Id { get; set; }
public string Name { get; set; }
public List<Campaign> Campaigns { get; set; }
}
}
[Route("api/[controller]")]
[ApiController]
public class CampaignController : ControllerBase
{
private readonly Context _context;
public CampaignController(Context context)
{
_context = context;
}
//Get Methods
[HttpPost]
public IActionResult Create(Campaign campaign)
{
_context.Campaigns.Add(campaign);
_context.SaveChanges();
return CreatedAtRoute("GetCustomer", new { id = campaign.Id }, campaign);
}
}
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var connection = @"Server=(localdb)\mssqllocaldb;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<Context>(options => options.UseSqlServer(connection));
}
Startup.cs
public class Context : DbContext
{
public Context(DbContextOptions<Context> options)
: base(options)
{ }
public DbSet<Customer> Customers { get; set; }
public DbSet<Campaign> Campaigns { get; set; }
}
{
public class Customer
{
public long Id { get; set; }
public string Name { get; set; }
public List<Campaign> Campaigns { get; set; }
}
}
[Route("api/[controller]")]
[ApiController]
public class CampaignController : ControllerBase
{
private readonly Context _context;
public CampaignController(Context context)
{
_context = context;
}
//Get Methods
[HttpPost]
public IActionResult Create(Campaign campaign)
{
_context.Campaigns.Add(campaign);
_context.SaveChanges();
return CreatedAtRoute("GetCustomer", new { id = campaign.Id }, campaign);
}
}
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var connection = @"Server=(localdb)\mssqllocaldb;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<Context>(options => options.UseSqlServer(connection));
}
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
public void配置服务(IServiceCollection服务)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
var connection=@“服务器=(localdb)\mssqllocaldb;可信连接=True;ConnectRetryCount=0”;
services.AddDbContext(options=>options.UseSqlServer(connection));
}
它不应该那样工作。控制器不会验证您的DB外键。验证用于验证ViewModel约束,如[必需]
文件: