Entity Framework duplication rows (C#) -


in system, testing dao classes , found bug. when object database , use other object, ef duplicate record.

my main code:

        try         {             usuariodao usuariodao = new usuariodao();             bugdao bugdao = new bugdao();             tipostatusdao tipostatusdao = new tipostatusdao();              usuario usuario = usuariodao.getbyid(1, true);              tipostatus tipostatus = tipostatusdao.getbyid(1, true);              bug bug1 = new bug()             {                 tipostatus = tipostatus,                 descricao = "erro ao salvar dados",                 dtabertura = datetime.now,                 usuario = usuario             };              bugdao.add(bug1);              bugdao.savechanges();             bugdao.dispose();             usuariodao.dispose();                             tipostatusdao.dispose();         }         catch (exception ex)         {             messagebox.show(ex.message);         } 

usuariodao code:

    private sgsnimodel database;      public usuariodao()     {         database = databasecontexthelper.context;     }      public usuario getbyid(int id, bool asnotracking = false)     {         try         {             if (asnotracking)                 return database.usuario.asnotracking().firstordefault(x => x.id == id);             else                 return database.usuario.firstordefault(x => x.id == id);         }         catch (exception ex)         {             throw new exception(string.format("{0}.{1} -> {2}", methodbase.getcurrentmethod().declaringtype.fullname, methodbase.getcurrentmethod().name, ex.message));         }     }      public void dispose()     {         if (database != null)             database.dispose();     }      public void savechanges()     {         database.savechanges();     } 

databasecontexthelper code:

    private static sgsnimodel _context;      public static sgsnimodel context     {                 {             if (_context == null)             {                 _context = new sgsnimodel();             }              return _context;         }     } 

i create bug object , setting propertie "usuario" return of usuariodao , same "tipostatus".

when savechanges, ef duplicate records in database. i'm googled , suggestion foud using 1 context, not work.

who can fix this?

thanks

[edit]

to solve issue: pass false "asnotracking", because object need tracked.

i.e.

old code:

        usuario usuario = usuariodao.getbyid(1, true);          tipostatus tipostatus = tipostatusdao.getbyid(1, true); 

new code:

        usuario usuario = usuariodao.getbyid(1);          tipostatus tipostatus = tipostatusdao.getbyid(1); 

thanks ivan stoev


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -