简体   繁体   English

Rest API 数据不删除

[英]Rest API data doesn't delete

I working on a simple crud operation on API.我在 API 上进行简单的 crud 操作。 My GET, POST, PUT all are working fine.我的 GET、POST、PUT 都运行良好。 But the DELETE method doesn't work.但是 DELETE 方法不起作用。 It show a error message:它显示一条错误消息:

The DELETE statement conflicted with the REFERENCE constraint "FK_Configurations_Mobiles_MobileId". DELETE 语句与 REFERENCE 约束“FK_Configurations_Mobiles_MobileId”冲突。 The conflict occurred in database "TestPrime2", table "dbo.Configurations", column 'MobileId'.冲突发生在数据库“TestPrime2”、表“dbo.Configurations”、“MobileId”列中。

Here Mobile class and Configurations class their relation one-to-many.这里 Mobile class 和 Configurations class 它们是一对多的关系。

[Route("api/[controller]")]
[ApiController]
public class MobilesController : ControllerBase
{
    private readonly TestContext _context;

    public MobilesController(TestContext context)
    {
        _context = context;
    }

    [HttpGet]
    public async Task<ActionResult<IEnumerable<Mobile>>> GetMobiles()
    {
        return await _context.Mobiles.Include(p => p.Configuration).ToListAsync();
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<Mobile>> GetMobile(int id)
    {
        if(id <= 0)
        {
            return BadRequest();
        }

        var mobile = await _context.Mobiles
            .Include(p => p.Configuration)
            .Where( x => x.Id == id).FirstOrDefaultAsync();

        if (mobile == null)
        {
            return NotFound();
        }

        return mobile;
    }

    [HttpPut("{id}")]
    public async Task<IActionResult> PutMobile(int id, Mobile mobile)
    {
        if (id != mobile.Id)
        {
            return BadRequest();
        }

        _context.Entry(mobile).State = EntityState.Modified;

        try
        {
            await _context.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!MobileExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return NoContent();
    }

    [HttpPost]
    public async Task<ActionResult<Mobile>> PostMobile(Mobile mobile)
    {
        _context.Mobiles.Add(mobile);
        await _context.SaveChangesAsync();

        return CreatedAtAction("GetMobile", new { id = mobile.Id }, mobile);
    }

    [HttpDelete("{id}")]
    public async Task<IActionResult> DeleteMobile(int id)
    {
        if(id <= 0)
        {
            return BadRequest();
        }

        var mobile = await _context.Mobiles.FindAsync(id);

        if (mobile == null)
        {
            return NotFound();
        }

        _context.Mobiles.Remove(mobile);
        await _context.SaveChangesAsync();

        return NoContent();
    }

    private bool MobileExists(int id)
    {
        return _context.Mobiles.Any(e => e.Id == id);
    }
}

All methods work fine without the DELETE method没有 DELETE 方法,所有方法都可以正常工作

You need to remove all entries with the MobileId from Configurations table first.您需要先从 Configurations 表中删除所有带有 MobileId 的条目。

It would be something like this:它会是这样的:

 _context.RemoveRange(_context.Configurations.Where(x => x.MobileId == id).ToList());

And then you can delete the Mobile from the Mobile table.然后您可以从 Mobile 表中删除 Mobile。

Its because the Foreign key MobileId in your table named TestPrime2 has a reference to the row you're trying to delete.这是因为您的表中名为 TestPrime2 的外键 MobileId 引用了您尝试删除的行。

If you delete that row in the TestPrime2 table which has the MobileId you're trying to delete it will work.如果您删除 TestPrime2 表中具有您尝试删除的 MobileId 的那一行,它将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM