错误消息:INSERT语句与FOREIGN KEY约束冲突 [英] Error Message: The INSERT statement conflicted with the FOREIGN KEY constraint

查看:157
本文介绍了错误消息:INSERT语句与FOREIGN KEY约束冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class DashboardWorkQueue : System.Web.Services.WebService
    {
        private IErrorHandling _IErrorHandling;
        private IServiceHandler.Notification.ISubscription _ISubscription;
        private INotificationLeadTime _INotificationLeadTime;
        private IServiceHandler.Audit.IAuditNote _IAuditNote;

        public DashboardWorkQueue()
        {
            _IErrorHandling = new ErrorHandling();

            var container = new StructureMap.Container(new Service.Infrastructure.StructureMapRegistry());
           
            _ISubscription = container.GetInstance<isubscription>();
            _INotificationLeadTime = new Service.Notification.NotificationLeadTime();
            _IAuditNote = container.GetInstance<iservicehandler.audit.iauditnote>();
        }

        [WebMethod]
        public int EnableDisable(Guid userID, int recordID, Model.SubscriptionSource.SubscriptionSourceEnum subscriptionSourceEnum, bool enabled)
        {
            int subscriptionID = 0;

            try
            {
                subscriptionID = Systrends.TMS.Business.ServiceHandler.DashboardWorkQueue.EnableOrDisable(userID, recordID, subscriptionSourceEnum, enabled);
            }
            catch (Exception ex)
            {
                _IErrorHandling.LogErrorNoRedirect(ex);
                throw;
            }

            return subscriptionID;
        }

        [WebMethod]
        public int Enabled(Guid userID, int recordID, Model.SubscriptionSource.SubscriptionSourceEnum subscriptionSourceEnum)
        {
            Model.Subscription subscriptionModel = new Model.Subscription();

            int subscriptionID = 0;

            try
            {
                subscriptionModel = _ISubscription.Get(userID, recordID, subscriptionSourceEnum, Model.SubscriptionOrigination.SubscriptionOriginationEnum.UserSubscription);

                if (subscriptionModel == null)
                    return 0;

                if (subscriptionModel.DesktopLink.HasValue)
                {
                    if (subscriptionModel.DesktopLink.Value == true)
                        subscriptionID = subscriptionModel.Id;
                }
            }
            catch (Exception ex)
            {
                _IErrorHandling.LogErrorNoRedirect(ex);
                throw;
            }

            return subscriptionID;
        }

        [WebMethod]
        public void Save(int taskID, int subscriptionID, string note, Guid userID, DateTime dateDue, int filingId)
        {
            Systrends.TMS.Business.ServiceHandler.Notification notification = new Notification();

            try
            {
                if (taskID == 0)
                {
                    taskID = Systrends.TMS.Business.ServiceHandler.Task.Insert(subscriptionID, note, userID, dateDue);
                    notification.CreateTaskNotification(subscriptionID, taskID, dateDue, userID);
                    AddAuditNote(filingId, userID, "", "Insert");
                }
                else
                {
                     Systrends.TMS.Business.ServiceHandler.Task.Update(taskID, note, userID, dateDue);
                     notification.CreateTaskNotification(subscriptionID, taskID, dateDue, userID);
                     AddAuditNote(filingId, userID, "", "Update");
                                         
                }
            }
            catch (Exception ex)
            {
                _IErrorHandling.LogErrorNoRedirect(ex);
                throw;
            }
        }

        [WebMethod]
        public List<Model.Task> GetList(int subscriptionID)
        {
            try
            {
                <Model.Task> list = Task.GetList(subscriptionID);

                return list;
            }
            catch (Exception ex)
            {
                _IErrorHandling.LogErrorNoRedirect(ex);
                throw;
            }
        }

        [WebMethod]
        public Model.Task Get(int taskID)
        {
            try
            {

                Model.Task model = Task.Get(taskID);

                return model;
            }
            catch (Exception ex)
            {
                _IErrorHandling.LogErrorNoRedirect(ex);
                throw;
            }
        }


        [WebMethod]
        public void DeleteTask(int taskID,int filingId,Guid userId)
        {
            try
            {
                Model.Users user = SessionWrapper.UserModel;

                Systrends.TMS.Business.ServiceHandler.Task.Delete_ById(taskID);
                if (Guid.Empty != userId)
                {
                    AddAuditNote(filingId, userId, "", "Delete");
                }
               
            }
            catch (Exception ex)
            {
                _IErrorHandling.LogErrorNoRedirect(ex);
                throw;
            }
        }

        private void AddAuditNote(int filingId, Guid userId, string createdBy, string changeBy)
        {
            string message = null;
            if (userId == Guid.Empty)
            {
                Systrends.TMS.Data.User_View userView = RepositoryEF.User.UserRepository.Get_ByUserName(createdBy);
                userId = userView.UserId;
            }
            switch (changeBy)
            {
                case "Insert":
                    message = "Inserted Work Queue note";
                    break;
                case "Update":
                    message = "Updated Work Queue note";
                    break;
                case "Delete":
                    message = "Deleted Work Queue note";
                    break;
                default:
                    break;
            }
            if (string.IsNullOrEmpty(message))
            {
                message = changeBy;
            }
            _IAuditNote.Insert(Model.AuditNote.EntityType.Filing, filingId, userId, message);
        }

    }
}

推荐答案

此错误告诉您何时在从另一个表引用的某个列中插入值,该值不包含在参考表中。



让我举个例子......



假设我有Project表,并且它的IndustryID栏引用了另一个表行业。



行业
ID名称

--------------------

1医疗保健

2自动手机

3房地产



项目

ID名称行业ID

-------------------------------

1 P1 2

2 P2 3



现在如果我做这样的事情......

插入项目值(3,P3,10) )



这会给我一个插入语句与外键冲突的错误。

因为没有重新在工业表中ID = 10的电源线,我试图将其插入到工业中引用的项目表中。



希望您了解实际问题,这可能会有所帮助你可以解决你的问题。



编辑:

要添加到包含外键的表中的值,参考表必须包含此值。(因此,首先在参考表中插入该值,然后在表中引用它的位置)。
This Error telling that When you are inserting value in some column which is referenced from another table, the value is not contained by Reference table.

Let me Give you an Example...

Say I have table "Project" and it have column IndustryID referenced to another table Industry.

Industry
ID Name
--------------------
1 Healthcare
2 Auto Mobile
3 Real Estate

Project
ID Name IndustryID
-------------------------------
1 P1 2
2 P2 3

Now if i do something like this...
insert into Project values(3,P3,10)

This will give me an error that insert statement conflicted with foreign key.
because there is no record having ID=10 in Industry table and i am trying to insert it in Project table which is referenced to Industry.

Hope you understand the actual problem and this might help you to solve your Problem.

EDIT :
the value you want to add into a table which consist of foreign key,the reference table must contain this value.(So insert that value first in Reference table and then in your table where it has been referenced).


这篇关于错误消息:INSERT语句与FOREIGN KEY约束冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆