How to compare with Guid Mvc

Asked

Viewed 258 times

1

Hello, I would like to make this comparison using a Guid, public Guid ClienteID {get;set;}. This code works using a int, I make a if, if my ClienteID == 0 I save, and if it doesn’t equal 0 I change.

I want a way to be able to do more than that to save and change.

    public void Salvar(Cliente cliente)
    {
        if (cliente.ClienteID == 0)
        {                
            cliente.DataCadastro = DateTime.Now;
            _contexto.Clientes.Add(cliente);
        }
        else
        {                
            Cliente cli = _contexto.Clientes.Find(cliente.ClienteID);

            if (cli != null)
            {
                cli.Nome = cliente.Nome;
                cli.Cpf = cliente.Cpf;
                cli.Telefone = cliente.Telefone;
                cli.Email = cliente.Email;
                cli.Cep = cliente.Cep;
                cli.Endereco = cliente.Endereco;
                cli.Bairro = cliente.Bairro;
                cli.Numero = cliente.Numero;
                cli.Complemento = cliente.Complemento;
                cli.DataCadastro = cli.DataCadastro;
            }
        }
        _contexto.SaveChanges();
    }
  • That one Salvar is called exactly where?

  • All you had to do was put the classe Client, in your question, but, I made the two forms in the answer.

2 answers

2

You just need to compare the property with Guid.Empty

Ex.:

cliente.ClienteGuid == Guid.Empty;

About the code presented: This second if, is not necessary, this way it will even work, but will bring you some problems later. For example, every time you create a new property you will have to enter the saving method and edit it.

You can simplify everything to:

public void Salvar(Cliente cliente)
{
    if(cliente.ClienteGuid == Guid.Empty)
    {
        cliente.DataCadastro = DateTime.Now;
        _contexto.Clientes.Add(cliente);
    }
    else
    {
        _contexto.Entry(cliente).State = EntityState.Modified;
    }

    _contexto.SaveChanges();
}

It may have some typos because I wrote by cell phone, anything warns me by the comments I edit as soon as I can.

1

Test if type variable Guid is empty:

Example:

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Guid Id = Guid.Empty;

            if (Id == Guid.Empty)
            {
                Console.WriteLine("Guid is empty");                
            }

            Id = Guid.NewGuid();

            if (!(Id == Guid.Empty))
            {
                Console.WriteLine("Guid not empty");
            }

            Console.ReadKey();

        }
    }
}

DEMO


Another way that can happen is how much we define the type System.Nullable:

static void Main(string[] args)
{
    Guid? Id = null;

    if (Id.HasValue)
    {
        Console.WriteLine("Guid is not null");                
    }
    else
    {
        Console.WriteLine("Guid is null");                
    }

    Id = Guid.NewGuid();

    if (Id.HasValue)
    {
        Console.WriteLine("Guid not null");
    }

    Console.ReadKey();

}

DEMO

In your question the code snippet may vary according to these ways:

Guid.Empty


public void Salvar(Cliente cliente)
{
    if (cliente.ClienteID == Guid.Empty)
    {                
        cliente.DataCadastro = DateTime.Now;
        _contexto.Clientes.Add(cliente);
    }
    else
    {                
        Cliente cli = _contexto.Clientes.Find(cliente.ClienteID);

        if (cli != null)
        {
            cli.Nome = cliente.Nome;
            cli.Cpf = cliente.Cpf;
            cli.Telefone = cliente.Telefone;
            cli.Email = cliente.Email;
            cli.Cep = cliente.Cep;
            cli.Endereco = cliente.Endereco;
            cli.Bairro = cliente.Bairro;
            cli.Numero = cliente.Numero;
            cli.Complemento = cliente.Complemento;
            cli.DataCadastro = cli.DataCadastro;
        }
    }
    _contexto.SaveChanges();
}

Guid == null


public void Salvar(Cliente cliente)
{
    if (!cliente.ClienteID.HasValue)
    {                
        cliente.DataCadastro = DateTime.Now;
        _contexto.Clientes.Add(cliente);
    }
    else
    {                
        Cliente cli = _contexto.Clientes.Find(cliente.ClienteID);

        if (cli != null)
        {
            cli.Nome = cliente.Nome;
            cli.Cpf = cliente.Cpf;
            cli.Telefone = cliente.Telefone;
            cli.Email = cliente.Email;
            cli.Cep = cliente.Cep;
            cli.Endereco = cliente.Endereco;
            cli.Bairro = cliente.Bairro;
            cli.Numero = cliente.Numero;
            cli.Complemento = cliente.Complemento;
            cli.DataCadastro = cli.DataCadastro;
        }
    }
    _contexto.SaveChanges();
}

References:

  • 1

    I think it would be interesting if you put the answer in his context. Your answer is correct, but possibly doubt is part of a ASP.NET MVC business rule.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.