6
I created a very simple application to simulate a small client registration, but when testing the manipulation of data by the application I could see in the database that the fields that are null in the register are inserted in the database with two simple quotes ('').
In other applications I can solve this by changing the data type of the parameter to NpgsqlDbType.Text
, but I don’t know a way to do this at EF.
I use an entity POCO class mapped in this way:
[Table("cliente", Schema = "public")]
public class Cliente
{
[Key]
[Column("id")]
public int Id { get; set; }
[Required(ErrorMessage = "Nome não pode ser nulo.")]
[Column("nome")]
public string Nome { get; set; }
[Required(AllowEmptyStrings = true)]
[Column("endereco")]
public string Endereco { get; set; }
[Column("bairro", TypeName="text")]
public string Bairro { get; set; }
[Required(ErrorMessage = "Cidade não pode ser nulo.")]
[Column("cidade")]
public int CidadeID { get; set; }
[ForeignKey("CidadeID")]
public Cidade Cidade { get; set; }
[Column("cpfcnpj")]
public string CPFCNPJ { get; set; }
[Column("telefone")]
public string Telefone { get; set; }
[Column("ativo")]
public bool Ativo { get; set; }
public virtual IQueryable<Cliente> Clientes { get; set; }
}
Below method that inserts the customer data informed in the form in the database:
public static void InserirCliente(Cliente cli)
{
using (var db = new Repositorio.DBContexto())
{
try
{
db.Clientes.Add(cli);
var usuarioSalvo = db.SaveChanges();
}
catch (Exception)
{
throw;
}
}
}
Below method of the event started by Click of a Save button:
private void btnSalvar_ItemClick(object sender, ItemClickEventArgs e)
{
try
{
var cliente = new Cliente();
cliente.Nome = Convert.ToString(txtNome.EditValue);
cliente.Telefone = Convert.ToString(txtTelefone.EditValue);
cliente.CPFCNPJ = Convert.ToString(txtCPF.EditValue);
cliente.Endereco = Convert.ToString(txtEndereco.EditValue);
cliente.Bairro = Convert.ToString(txtBairro.EditValue);
cliente.CidadeID = Convert.ToInt32(lkeCidade.EditValue);
cliente.Ativo = true;
DAL.ClienteDAL.InserirCliente(cliente);
MessageBox.Show("Cliente Inserido com Sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.DialogResult = DialogResult.OK;
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(string.Format("{0}\n\n{1}", ex.Message, ex.InnerException), "Ooops", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Is there any way to prevent the insertion of "single quotes" in fields that allow null values?
So... I tried to use string? and tmb Nullable<string> but I get a build error because string already a type that predicts nulls...
– TNERING
Which fields give the problem? Are only the type string? Are they null? If they are not, the behavior is correct. If you want to write null, you need to make sure they are null.
– Maniero
Give a read on exceptions that are not done like this: http://answall.com/search?tab=votes&q=user%3a101%20[exce%C3%A7%C3%A3o]
– Maniero
so man... not that this is superfluous but open a table and see that lot of simple quotes bothers me, and besides I may have problems with clauses where the field is null or not null...
– TNERING
in my small test base I only reported in fields of type Varying Character... But I just tested with the Boolean type and the null was a good one...
– TNERING