实体框架 - 如何在不保存相关对象的情况下保存实体

时间:2012-07-12 08:48:06

标签: entity-framework linq-to-entities

在我的实体框架中,我有三个相关实体“客户端”,“ ClientAddress ”和“ LookupAddressType ”。 “ LookupAddressType ”是指定可用地址类型类型的主类,如商家地址,住址等。 ClientAddress 依赖于 LookupAddresstype 客户端即可。使用相关ClientAddress数据保存 客户端实体时,我遇到以下错误。

  

“违反PRIMARY KEY约束'PK_LookupAddressType'。不能   在对象'dbo.LookupAddressType'中插入重复键。该声明   已被终止。

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

保存代码如下:

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);
    }

1 个答案:

答案 0 :(得分: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);

        }