0

在我的实体框架中,我有三个相关的实体“客户端”、“客户端地址”和“查找地址类型”。“ LookupAddressType ”是一个主类,指定可用地址类型的类型,如商业地址、住宅地址等。ClientAddress取决于LookupAddresstypeClient。在保存具有相关 ClientAddress 数据的Client实体时,我收到以下错误。

“违反主键约束‘PK_LookupAddressType’。无法在对象‘dbo.LookupAddressType’中插入重复键。语句已终止。

我不需要插入 LookupAddressType。在这里,我只需要将相关的lookupAddressTypeId 插入到clientAddress 实体中。

保存代码是这样的:

Add(Client);
_objectContext.SaveChanges();

我怎样才能做到这一点?

加载代码如下:

private void LoadClientDetails(EFEntities.Client _Client)
    {

        EFEntities.LookupClientStatu clientStatus;
        var clientAddressList = new List<ClientAddress>();

        if (_Client == null)
        {
            return;
        }

        //Assign data to client object
        _Client.ClientName = rtxtName.Text;
        _Client.Alias = rtxtAlias.Text;
        _Client.ClientCode =Int32.Parse(rtxtClientCode.Text); 
        _Client.TaxPayerID = rtxtTaxPayerId.Text;

        if (rcboStatus.SelectedIndex != 0)
        {
            clientStatus = new EFEntities.LookupClientStatu
                               {
                                   ClientStatusID = (Guid) (rcboStatus.SelectedValue),
                                   ClientStatusDescription = rcboStatus.Text
                               };
            _Client.LookupClientStatu = clientStatus;
        }


        //_Client.Modified = EnvironmentClass.ModifiedUserInstance.Id;

        _Client.EffectiveDate = rdtEffectiveDate.Value;

        if (rdtExpDate.Value != rdtExpDate.MinDate)
        {
            _Client.ExpirationDate = rdtExpDate.Value;
        }
        else
        {
            _Client.ExpirationDate = null;
        }

        _Client.StartDate = DateTime.Now;


        EFEntities.ClientAddress clientAddress = null;
        // Iesi.Collections.Generic.ISet<ClientAddress> clientAddress = new HashedSet<ClientAddress>();
        foreach (var cAddress in _clientController.client.ClientAddresses)
        {
            clientAddress = cAddress;
            break;
        }

        if (clientAddress == null)
        {
            clientAddress = new EFEntities.ClientAddress();
        }

        clientAddress.Address1 = rtxtClientAdd1.Text;
        clientAddress.Address2 = rtxtClientAdd2.Text;
        clientAddress.Address3 = rtxtClientAdd3.Text;

        // Address type details
        if (rcboClientAddType.SelectedIndex != -1)
        {
            clientAddress.LookupAddressType = new EFEntities.LookupAddressType
                                            {
                                                AddressTypeID = (Guid) (rcboClientAddType.SelectedValue),
                                                AddressTypeDescription = rcboClientAddType.Text
                                            };

            //clientAddress.AddressType.Id = Convert.ToByte(rcboClientAddType.SelectedValue);
        }


        clientAddress.City = rtxtClientCity.Text;
        clientAddress.Client = _Client;

\

        _Client.ClientAddresses.Add(clientAddress);
    }
4

1 回答 1

0

好吧,我对以下代码行进行了此操作以使其正常工作。

if (rcboClientAddType.SelectedIndex != -1)
    {
        clientAddress.LookupAddressType = new EFEntities.LookupAddressType
                                        {
                                            AddressTypeID = (Guid) (rcboClientAddType.SelectedValue),
                                            AddressTypeDescription = rcboClientAddType.Text
                                        };

        //clientAddress.AddressType.Id = Convert.ToByte(rcboClientAddType.SelectedValue);
    }

我把上面的代码改成了这个

if (rcboClientAddType.SelectedIndex != -1)
        {
            //clientAddress.LookupAddressType = new EFEntities.LookupAddressType
            //                                {
            //                                    AddressTypeID = (Guid) (rcboClientAddType.SelectedValue),
            //                                    AddressTypeDescription = rcboClientAddType.Text
            //                                };
            clientAddress.AddressTypeID = (Guid)(rcboClientAddType.SelectedValue);

        }
于 2012-07-12T12:13:31.197 回答