当我是唯一的用户时,为什么我会在Entity Framework中获得并发异常?

时间:2018-06-03 18:58:22

标签: .net sql-server entity-framework

我刚刚毕业,我正在研究我的第一个项目。

我搜索了很多并尝试了很多解决方案,但似乎没有一个解决这个问题。我是.NET和Entity Framework的新手,我试图通过POST调用EF将新行插入到现有表中,但即使我是唯一的例外,我仍然会遇到以下异常用户当前使用此数据库 (直接向DB插入行成功)

这是我得到的例外:

  

System.Data.Entity.Infrastructure.DbUpdateConcurrencyException
  的HResult = 0x80131501
  Message =存储更新,插入或删除语句影响了意外的行数(0)。自实体加载后,实体可能已被修改或删除。有关理解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=472540   Source = EntityFramework

     

StackTrace:

     

at System.Data.Entity.Internal.InternalContext.SaveChanges()
  在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()
  在System.Data.Entity.DbContext.SaveChanges()
  在Em:Habanim_Api.Controllers.TherapyController.PostTherapyMeetingSet(TherapyMeetingSet t)中的Q:\ a_Tech_App_2018 \ user \ Aim Habanim Server \ Em_Habanim_Api \ Controllers \ TherapyController.cs:第127行
  在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor。<> c__DisplayClassc.b__6(Object instance,Object [] methodParameters)
  at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance,Object [] arguments)
  在System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext,IDictionary`2 arguments,CancellationToken cancellationToken)

     

内部例外1:

     

OptimisticConcurrencyException:存储更新,插入或删除语句影响了意外的行数(0)。自实体加载后,实体可能已被修改或删除。有关理解和处理乐观并发异常的信息,请参阅http://go.microsoft.com/fwlink/?LinkId=472540

我尝试过以下两种方式发帖:

[Route("api/therapySets/{id}/func1")]
public void PostTherapyMeetingSet(TherapyViewModel t)
{
    using (Em_Habanim_Entities db = new Em_Habanim_Entities())
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        TherapyMeetingSet therapySet = new TherapyMeetingSet();
        therapySet.Id = t.Id;
        therapySet.TherapistId = t.TherapistID;
        therapySet.ClientId = t.ClientID;
        therapySet.Date = t.Date;
        therapySet.Comments = t.Comments;

        db.TherapyMeetingSets.Add(therapySet);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = therapySet.Id }, therapySet);
    }
}

以下是TherapyViewModel类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using BL.model;

namespace Em_Habanim_Api.Models
{
    public class TherapyViewModel
    {
        public TherapyViewModel(TherapyMeetingSet t)
        {
            this.Id = t.Id;
            this.TherapistID = t.TherapistId;
            this.ClientID = t.ClientId;
            this.Date = t.Date;
            this.Comments = t.Comments;
        }

        public TherapyViewModel()
        {
        }

        public int Id { get; set; }
        public int TherapistID { get; set; }
        public int ClientID { get; set; }
        public DateTime Date { get; set; } 
        public string Comments { get; set; }
    }
}

我也尝试过不使用TherapyViewModel类但我得到了同样的例外:

[Route("api/therapySets/{id}/func1")]
public void PostTherapyMeetingSet(TherapyMeetingSet t)
{
    using (Em_Habanim_Entities db = new Em_Habanim_Entities())
    {
        db.TherapyMeetingSets.Add(t);
        db.SaveChanges();
    }
}

以下是表格定义:

CREATE TABLE [dbo].[TherapyMeetingSet] 
(
    [Id]          INT IDENTITY(1,1) NOT NULL,
    [TherapistId] INT           NOT NULL,
    [ClientId]    INT           NOT NULL,
    [Date]        DATETIME      NOT NULL,
    [Comments]    NVARCHAR(100) NULL,

    CONSTRAINT [PK_MeetingSet] 
        PRIMARY KEY CLUSTERED ([Id] ASC),

    CONSTRAINT [FK_TherapistMeeting] 
        FOREIGN KEY ([TherapistId]) REFERENCES [dbo].[TherapistSet] ([Id]),
    CONSTRAINT [FK_PersonTherapyRequest] 
        FOREIGN KEY ([ClientId]) REFERENCES [dbo].[PersonSet] ([Id])
);
GO

CREATE TRIGGER ClientChildOrMotherTrigger 
ON therapyMeetingSet
INSTEAD OF INSERT
AS
    DECLARE @ClientId INT;
    DECLARE @therapistId INT;
    DECLARE @Date DATE;
    DECLARE @Comments NVARCHAR(100);

    SELECT @ClientId = i.ClientId 
    FROM inserted i;

    SELECT @therapistId = i.TherapistId 
    FROM inserted i;

    SELECT @Date = i.Date 
    FROM inserted i;

    SELECT @Comments = i.Comments 
    FROM inserted i;

    IF EXISTS (SELECT id FROM ChildSet WHERE id = @ClientId) OR
       EXISTS (SELECT id FROM MotherSet WHERE id = @ClientId)
       INSERT INTO TherapyMeetingSet 
       VALUES (@therapistId, @ClientId, @Date, @Comments)
    ELSE
       PRINT 'Client ID is invalid'

知道是什么导致了这个错误吗?

0 个答案:

没有答案