Entity FrameWork Objects

0

I'm having a question with Entity Framework

I have a situation in which I want to implement a structure in the database of a chart of accounts, for example: the current active account ID 1.1 is the parent of the account box 1.1.1, is my business rule

Assuming the hypothetical situation I already have the active parent account saved in the bank, and you want to save a child account, example account box.

I created these two methods: one that saves a new account in the bank, and another that queries an account in the bank and returns an IQueriable collection

        //Método para salvar uma nova conta

    public static void IncuirDB(Conta ObjConta, string IDContaPai)
    {
        Contexto Db = new Contexto();

        var ContaPai = Repositorio.Consultar(IDContaPai, "");
        var ContaPaiFirst = ContaPai.FirstOrDefault();

        ObjConta.ContaPai = ContaPaiFirst;

        Db.Conta.Add(ObjConta);

        Db.SaveChanges();

    }

    //Método para consultar uma conta
    public static IQueryable<Conta> Consultar(string _IDConta, string _Nome)
    {
        Contexto Db = new Contexto();

            var Consultar = from c in Db.Conta where c.Codigo == _IDConta select c;


            return Consultar;
    }

The problem occurs when I try to save my account object, which has a property of type contaPai which is also an object of type account. This object comes from the user interface layer and I just want to include a contapai property in it, before saving it to the database. However I can not because I have had the error below.

ERROR Detail: "Violation of PRIMARY KEY constraint 'PK_dbo.Conta'. Can not insert duplicate key in object 'dbo.Conta' .The duplicate key value is (1.1). \ R \ nThe statement has been terminated."}

NOTE: I am sure that the account I am trying to include does not exist in the database.

Can anyone give me a hand?

    
asked by anonymous 12.04.2016 / 21:25

1 answer

3

James, my first tip is about using Contexto , it is interesting that you do all your operations within the same Contexto and that the same live only to do your job ... after all DbContext uses UnitOfWork Pattern .

then in place of:

public static void IncuirDB(Conta ObjConta, string IDContaPai)
{
    Contexto db = new Contexto();
    //efetuar consultas e pesistencia.
    db.SaveChanges();
}

do:

public static void IncuirDB(Conta ObjConta, string IDContaPai)
{
    using (var db = new Contexto())
    {
        //efetuar consultas e pesistencia.
        db.SaveChanges();
    }
}

Second point, do not reinvent the wheel ... so that a Consultar(id) method, when DbSet has Find(id) ? What's more, you're creating a second Contexto , in addition to violating UnitOfWork , as you are using a Static Repository, you can end up with more open connections than you would like (not to mention memory consumption), after all to wait for the good will of Garbage Collector .

public static void IncuirDB(Conta ObjConta, string IDContaPai)
{
    using (var db = new Contexto())
    {
        var ContaPai = db.Conta.Find(IDContaPai);
        ContaPai.ContasFilha.Add(ObjConta);
        db.SaveChanges();
    }
}

And finally, before continuing to use a Repository with Entity Framework ... read this answer to a question from me: #

    
12.04.2016 / 21:55