How to compare with Guid Mvc

1

Hello, I would like to make this comparison using a Guid , public Guid ClienteID {get;set;} . This code works by using int , I if , if my ClienteID == 0 I saved, and if not equal to 0 I change.

I want a way to do more or less this so I can 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();
    }
    
asked by anonymous 05.06.2016 / 11:14

2 answers

2

You just need to compare the property with Guid.Empty

Eg:

cliente.ClienteGuid == Guid.Empty;

About the displayed code: This second if, is not necessary, this way it will work, but it will bring you some problems later. For example, every time you create a new property you will have to enter the save 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();
}
  

I may have some typos because I wrote on my cell phone, anything tells me the comments I make as soon as I can.

    
05.06.2016 / 16:34
1

Test whether the variable of type 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 can vary in 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:

07.06.2016 / 01:33