Insert data in Value Object Table

Asked by At

Inserting Data By POSTMAN, but some error occurred.

My Code is given below:

public async Task<BusinessProfile> Save(string ID,bool Status,string Logo,string TaxNumber,string StateRegisterNumber,string StreetName,string Number
            ,string Complement,string PostalCode,string Neighborhood,string City,string State,string AreaCode,string Department
            ,string PhoneNo,string Type,string Website,string EDepartment,string EmailAddress)
        {

            try
            {
                int EnumPhoneNumber = 0;
                string[] values = Enum.GetNames(typeof(PhoneNumberEnum.PhoneNumber));

                if (values[0] == Type)
                {
                    EnumPhoneNumber = (int)PhoneNumber.FixedPhone;
                }
                else if (values[1] == Type)
                {
                    EnumPhoneNumber = (int)PhoneNumber.MobilePhone;
                }

                var businessProfile = new BusinessProfile() { ID = ID, Status = Status, Logo = Logo, TaxNumber = TaxNumber, StateRegisterNumber = StateRegisterNumber, Website = Website };
                businessProfile.AssignAddress(new Address(StreetName, Number, Complement, PostalCode, Neighborhood, City, State));
                businessProfile.AssignPhones(new Phones(Convert.ToString(EnumPhoneNumber), AreaCode, PhoneNo, Department));
                businessProfile.AssignEmail(new Emails(EmailAddress, EDepartment));

                //_db.Add(businessProfile);


                Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<BusinessProfile> x = await _db.AddAsync(businessProfile);

                // _db.SaveChanges();


                //await _db.BusinessProfiles.AddAsync(businessProfile);
                await _db.SaveChangesAsync();
                return businessProfile;
            }
            catch(Exception ex)
            {
                throw ex;
            }


        }

Output (Error:):

An unhandled exception occurred while processing the request.

InvalidOperationException: The entity of type BusinessProfile is sharing the table BusinessProfiles with entities of type Address, but there is no entity of this type with the same key value that has been marked as Added. Consider using DbContextOptionsBuilder.EnableSensitiveDataLogging to see the key values.

1 Answers

0
Community On Best Solutions

public async Task Save(BusinessProfile bp) { try { int EnumPhoneNumber = 0; string[] values = Enum.GetNames(typeof(PhoneNumberEnum.PhoneNumber));

            if (values[0] == bp.Phones.Type)
            {
                EnumPhoneNumber = (int)PhoneNumber.FixedPhone;
            }
            else if (values[1] == bp.Phones.Type)
            {
                EnumPhoneNumber = (int)PhoneNumber.MobilePhone;
            }

            var businessProfile = new BusinessProfile() { ID = bp.ID, IsActive = bp.IsActive, Logo = bp.Logo, TaxNumber = bp.TaxNumber, StateRegisterNumber = bp.StateRegisterNumber, Website = bp.Website };
            businessProfile.Address = new Address(bp.Address.StreetName, bp.Address.Number, bp.Address.Complement, bp.Address.PostalCode, bp.Address.Neighborhood, bp.Address.City, bp.Address.State);
            businessProfile.Phones = new Phones(Convert.ToString(EnumPhoneNumber), bp.Phones.AreaCode, bp.Phones.PhoneNumber, bp.Phones.Department);
            businessProfile.Emails = new Emails(bp.Emails.EmailAddress, bp.Emails.Department);

            Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry<BusinessProfile> x = await _db.AddAsync(businessProfile);
            await _db.SaveChangesAsync();
            return businessProfile;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }