The INSERT statement conflicted with the foreign KEY constraint " FK dbo.Bod counters.Enderecos EnderecoId"

I have two problems. I have the entity Counter, address and City . I'm going to sign up for a new - count - , I got a dropdowlist to load cities - , so that I can choose from, the problem is that, as opposed to just select city for your address) in question, he was copying the city the bank is, or is to be, where I want the city, and he ends up saving the same city, with a different Guid. the other problem is that whenever I have save the counter, it gives the following error message:

INSERT statement conflicted with foreign KEY constraint "FK_dbo.Counters_dbo.Enderecos_EnderecoId". The conflict occurred in the database "Smc_DDD_data_guid", table " bod.Righties", column 'EnderecoId'. The instruction has been finalized.

My counter model:

  public class Contador: Pessoa
    {
        public Contador()
        {
            ClienteId = Guid.NewGuid();
        }
        public bool Status { get; set; }
        public virtual EnderecoTerceiros Endereco { get; set; }
        public Guid EnderecoId { get; set; }
    }

My Model Address:

public class EnderecoTerceiros
    {
        public EnderecoTerceiros()
        {
            EnderecoTerceirosId = Guid.NewGuid();
        }
        public Guid EnderecoTerceirosId { get; set; }
        public string Rua { get; set; }
        public string Bairro { get; set; }
        public string Cep { get; set; }
        public string Complemento { get; set; }
        public string Numero { get; set; }
        public Zona Zona { get; set; }
        public TipoDeEndereco TipoDeEndereco { get; set; }
        public DateTime DataCadastro { get; set; }

        public virtual Cidade Cidade { get; set; }
        public Guid CidadeId { get; set; }

    }

My Cities Model:

public class Cidade
    {
        public Cidade()
        {
            CidadeId = Guid.NewGuid();
        }
        public Guid CidadeId { get; set; }
        public string Nome { get; set; }
        public string Codigo { get; set; }
        public Estado Estado { get; set; }
        public Regioes Regioes { get; set; }
        public DateTime DataCadastro { get; set; }
    }

In my controller it looks like this:

public ActionResult Create(ContadorViewModel contadorViewModel)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.CidadeId = new SelectList(_cidadeAppService.ObterTodos(), "CidadeId", "Nome");
                return View(contadorViewModel);
            }

            var cidade = _cidadeAppService.ObterPorId(contadorViewModel.Endereco.Cidade.CidadeId); // busca a cidade pois ela ta vinda com a propriedade "Nome" vazia, isso gera um erro de validação, pois como eu falei, ele ta salvando a cidade de novo
            contadorViewModel.Endereco.Cidade = cidade;
            contadorViewModel.ClienteId = Guid.NewGuid();
            contadorViewModel.Endereco.EnderecoTerceirosId = Guid.NewGuid();
            _enderecoAppService.Adicionar(contadorViewModel.Endereco);
            _contadorAppService.Adicionar(contadorViewModel);
            return RedirectToAction("Index");


        }

Counter Service:

 public class ContadorAppServico : AppServiceBase, IContadorAppService
    {
        private readonly IContadorRepositorio _repositorio;

        public ContadorAppServico()
        {
            _repositorio = new ContadorRepositorio();
        }
        public ContadorViewModel Adicionar(ContadorViewModel contadorViewModel)
        {
            var contador = Mapper.Map<Contador>(contadorViewModel);
            _repositorio.Adicionar(contador);
            return contadorViewModel;
        }
}

Service Interface:

Public interface IContadorAppService:IDisposable
    {
        ContadorViewModel ObterPorId(Guid id);
        IEnumerable<ContadorViewModel> ObterTodos();
        ContadorViewModel ObterPorCnpj(string cnpj);
        ContadorViewModel Atualizar(ContadorViewModel contadorViewModel);
        ContadorViewModel Adicionar(ContadorViewModel contadorViewModel);
        void Remover(Guid id);
    }

Address number:

     public class EnderecoTerceirosAppService : AppServiceBase, IEnderecoTerceirosAppService
        {
            private readonly IEnderecoTerceirosRepositorio _repositorio;

            public EnderecoTerceirosAppService()
            {
                _repositorio = new EnderecoTerceirosRepositorio();
            }
            public EnderecoTerceirosViewModel Adicionar(EnderecoTerceirosViewModel enderecoViewModel)
            {
                var endereco = Mapper.Map<EnderecoTerceiros>(enderecoViewModel);
                _repositorio.Adicionar(endereco);
                return enderecoViewModel;
            }
}

Honestly speaking, I don't know why I'm saving a new city. My Get Create Action looks like this:

public ActionResult Create()
        {
            ViewBag.CidadeId = new SelectList(_cidadeAppService.ObterTodos(), "CidadeId", "Nome");
            return PartialView();
        }

And DropDow looks like this:

 @Html.DropDownListFor(model => model.Endereco.Cidade.CidadeId, (IEnumerable<SelectListItem>)ViewBag.CidadeId, string.Empty, new { @class = "municipio caixa_texto", style = "" })

The name of the city is coming Null to the conttroller, theoretically this does not me it would cause problem, since I only need the Guid, however, as ta creating a new city, with the city I choose, this generates a validation error.

However, after it creates a new city, it adds the address in the bank, but when the counter persists, it gives the conflict error.

Author: Rafael Passos, 2019-07-11