0

When I try to add a new object to a Context via AddOrUpdate Migrations method, I have the error "Object reference not set to an instance of an object." in Package Console when updating database.

So, I tried to do use simple Add instead and appears it worked.

Here is my Entity Class:

[Table("Sistema_TipoTela")]
public class TipoTela : TabelaBase
{
    public TipoTela() { }

    public TipoTela(int Codigo, string Descricao)
    {
        this.Codigo = Codigo;
        this.Descricao = Descricao;
    }

    public int Codigo { get; set; }
    public string Descricao { get; set; }
}

So, with this code I have error:

var tipo = new TipoTela()
        {
            Codigo = 1,
            Descricao = "Teste"
        };

_db.Sistema_TelaTipo.AddOrUpdate(o => o.Codigo, tipo);

...or this:

_db.Sistema_TelaTipo.AddOrUpdate(tipo);

And with this code it's ok:

var tipo = new TipoTela()
        {
            Codigo = 1,
            Descricao = "Teste"
        };

_db.Sistema_TelaTipo.Add(tipo);

However, I need use AddOrUpdate to specify an array with "Codigo" as key property.

Thanks for your help

Edit 1

Forgot to metion that "TabelaBase" class have Id property

Oswaldo
  • 3,296
  • 4
  • 25
  • 35
  • did you try `_db.Sistema_TelaTipo.AddOrUpdate(t=>t.Id, tipo);` I recommend you check thıs http://stackoverflow.com/questions/10007351/entity-framework-code-first-addorupdate-method-insert-duplicate-values – renakre Apr 07 '15 at 03:25
  • No, i didn't tried it cause I need to add record only if that "Codigo" doesn't exist yet – Oswaldo Apr 07 '15 at 10:57

0 Answers0