0

我正在尝试更新数据库实体中的现有内容。

我想向它添加一个引用(导航)成员。

我正在从数据库中获取要更新的实体,

并使用相同的上下文 ( mMamDbEntities)

我添加了参考成员。

但是我得到一个数据库错误,违反唯一性限制

{"违反 UNIQUE KEY 约束 'UQ_MamConfigurations_V1'。无法在对象 'dbo.MamConfiguration_V1' 中插入重复键。重复键值为 (elad_14Apr_1315)。\r\n语句已终止。"}

怎么会这样?我认为双向参考可能是问题所在

所以我什至试图取消它(见注释代码)

    public void SaveCofiguration(MamConfiguration_V1Ui itemUi)
    {
        var itemEf = mMamConfiguration_V1UiToEfConvertor.ConvertToNewEf(itemUi);

        using (var maMDBEntities = new MaMDBEntities())
        {
            IDal<MamConfiguration_V1> mamConfigurationDal = mDalFactory.GetDal<MamConfiguration_V1>(maMDBEntities);

            mamConfigurationDal.Save(itemEf);
        }
    }

     public MamConfiguration_V1 GetById(object id)
    {           
            id.ThrowIfNull("id");

            int configurationId = Convert.ToInt32(id);

            var result =
                mMaMDBEntities.MamConfiguration_V1.SingleOrDefault(item => item.ConfigurationId == configurationId);

            return result;

    }

   public MamConfiguration_V1 Save(MamConfiguration_V1 item)
    {

            item.ThrowIfNull("item");

            var itemFromDB = GetById(item.ConfigurationId);

            if (itemFromDB != null)
            {
                UpdateEfItem(itemFromDB, item);

               // if (mMaMDBEntities.ObjectStateManager.GetObjectStateEntry(itemFromDB).State == EntityState.Detached)

// { // mmaMDBEntities.MamConfiguration_V1.AddObject(itemFromDB); // }

                // Attached object tracks modifications automatically
                mMaMDBEntities.SaveChanges();

                return item;
            }





   private void UpdateEfItem(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi)
        {
            itemFromDb.UpdatedDate = DateTime.Now;

            itemFromDb.Description = itemFromUi.Description;

            itemFromDb.StatusId = itemFromUi.StatusId;

            itemFromDb.Name = itemFromUi.Name;

            itemFromDb.NumericTraffic = itemFromUi.NumericTraffic;

            itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic;

            itemFromDb.Type = itemFromUi.NumericTraffic;

            foreach (var item in itemFromDb.MamConfigurationToBrowser_V1.ToList())
            {
                if (itemFromUi.MamConfigurationToBrowser_V1.All(b => b.BrowserVersionId != item.BrowserVersionId))
                {
                    mMaMDBEntities.MamConfigurationToBrowser_V1.DeleteObject(item);
                }
            }

            for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
            {
                var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i);
                var item = itemFromDb.MamConfigurationToBrowser_V1.SingleOrDefault(b => b.BrowserVersionId == element.BrowserVersionId);
                if (item != null)
                {
                    // copy properties from element to item
                }
                else
                {
                    element.Browser = mMaMDBEntities.Browsers.Single(browserItem =>
                        browserItem.BrowserID == element.BrowserID);

                    //element.MamConfiguration_V1 = itemFromDb;

                    //have also tried: element.MamConfiguration_V1 = null;

                    //element.MamConfiguration_V1Reference = null;

                    itemFromDb.MamConfigurationToBrowser_V1.Add(element);
                }
            }
        }

更新:

mMaMDBEntities.SaveChanges();抛出异常

这些是数据库表:

CREATE TABLE [dbo].[MamConfiguration_V1](
    [ConfigurationId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NOT NULL,
    [Description] [nvarchar](200) NOT NULL,
    [StatusId] [int] NOT NULL,
    [Type] [int] NOT NULL,
    [CreatedDate] [datetime2](7) NOT NULL,
    [UpdatedDate] [datetime2](7) NOT NULL,
    [PercentageTraffic] [int] NOT NULL,
    [NumericTraffic] [int] NOT NULL,
 CONSTRAINT [PK_MamConfigurations_V1] PRIMARY KEY CLUSTERED 
(
    [ConfigurationId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [SECONDARY],
 CONSTRAINT [UQ_MamConfigurations_V1] UNIQUE NONCLUSTERED 
(
    [Name] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [SECONDARY]
) ON [SECONDARY]

GO

ALTER TABLE [dbo].[MamConfiguration_V1]  WITH CHECK ADD  CONSTRAINT [FK_MamConfiguration_V1_ConfigurationType_V1] FOREIGN KEY([StatusId])
REFERENCES [dbo].[MamConfigurationStatuses] ([StatusId])
GO

ALTER TABLE [dbo].[MamConfiguration_V1] CHECK CONSTRAINT [FK_MamConfiguration_V1_ConfigurationType_V1]
GO

ALTER TABLE [dbo].[MamConfiguration_V1] ADD  CONSTRAINT [DF_MamConfigurations_V1_CreatedDate]  DEFAULT (getdate()) FOR [CreatedDate]
GO

ALTER TABLE [dbo].[MamConfiguration_V1] ADD  CONSTRAINT [DF_MamConfigurations_V1_UpdatedDate]  DEFAULT (getdate()) FOR [UpdatedDate]
GO




CREATE TABLE [dbo].[MamConfigurationToBrowser_V1](
    [MamConfigurationId] [int] NOT NULL,
    [BrowserVersionId] [uniqueidentifier] NOT NULL,
    [IsWhiteListed] [bit] NOT NULL,
    [BrowserID] [int] NOT NULL,
    [VersionNumberLowRange] [varchar](50) NOT NULL,
    [CreatedDate] [datetime] NOT NULL,
    [UpdatedDate] [datetime] NOT NULL,
    [VersionNumberUpperRange] [varchar](50) NULL,
 CONSTRAINT [PK_MamConfigurationToBrowser_V1_1] PRIMARY KEY CLUSTERED 
(
    [BrowserVersionId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [SECONDARY]
) ON [SECONDARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[MamConfigurationToBrowser_V1]  WITH CHECK ADD  CONSTRAINT [FK_MamConfigurationToBrowser_V1_Browsers] FOREIGN KEY([BrowserID])
REFERENCES [dbo].[Browsers] ([BrowserID])
GO

ALTER TABLE [dbo].[MamConfigurationToBrowser_V1] CHECK CONSTRAINT [FK_MamConfigurationToBrowser_V1_Browsers]
GO

ALTER TABLE [dbo].[MamConfigurationToBrowser_V1]  WITH CHECK ADD  CONSTRAINT [FK_MamConfigurationToBrowser_V1_BrowserVersion] FOREIGN KEY([MamConfigurationId])
REFERENCES [dbo].[MamConfiguration_V1] ([ConfigurationId])
GO

ALTER TABLE [dbo].[MamConfigurationToBrowser_V1] CHECK CONSTRAINT [FK_MamConfigurationToBrowser_V1_BrowserVersion]
GO

ALTER TABLE [dbo].[MamConfigurationToBrowser_V1] ADD  CONSTRAINT [DF_Browser_V1_CreatedDate]  DEFAULT (getdate()) FOR [CreatedDate]
GO

ALTER TABLE [dbo].[MamConfigurationToBrowser_V1] ADD  CONSTRAINT [DF_Browser_V1_UpdatedDate]  DEFAULT (getdate()) FOR [UpdatedDate]
GO

更新 2

我已经尝试过@AzharKhorasany 解决方案,但出现了同样的错误:

for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
            {
                var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i);
                var item = itemFromDb.MamConfigurationToBrowser_V1.SingleOrDefault(b => b.BrowserVersionId == element.BrowserVersionId);
                if (item != null)
                {
                    // copy properties from element to item
                }
                else
                {
                    element.Browser = mMaMDBEntities.Browsers.Single(browserItem =>
                        browserItem.BrowserID == element.BrowserID);

                    element.MamConfigurationId = itemFromDb.ConfigurationId;

                    //element.MamConfiguration_V1 = itemFromDb;

                    //have also tried: element.MamConfiguration_V1 = null;

                    //element.MamConfiguration_V1Reference = null;
                    //mMaMDBEntities.AddToMamConfigurationToBrowser_V1(itemFromUi.MamConfigurationToBrowser_V1.ElementAt(0)); // add as inserted

                    itemFromDb.MamConfigurationToBrowser_V1.Add(element);
                }
            }
4

0 回答 0