Objects Entity Framework

Asked

Viewed 50 times

0

I’m having a question with Entity Framework

I have a situation where I want to implement a database structure of an accounting plan, for example: the active current account ID 1.1 is the parent of the cash account 1.1.1, is my business rule

Assuming the hypothetical situation that I already have the parent account Activocirculant saved in the bank, and want to save a daughter account, example account cashier.

I created these two methods: one that saves a new bank account, and another that queries a bank account and returns a 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 account type object. This objetoconta comes from the user interface layer and I just want to include a contapai property in it, before saving it in the bank. However I am not succeeding as I have had the error below.

Detail of the ERROR: "Violation of PRIMARY KEY Constraint 'Pk_dbo.Account'. Cannot Insert Duplicate key in Object 'dbo.Account'. 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 bank.

Can someone give me a hand?

  • Dude, don’t you better put an Idcontapai on Objconta?

1 answer

3

Tiago, my first tip is about using the Contexto, It is interesting that you do all your operations within the same Contexto and that it lives only to carry out its work... 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 re-invent the wheel... so that a method Consultar(id), when the DbSet owns the Find(id)? Besides, you’re creating a second Contexto, in addition to violating the UnitOfWork, as you are using a Repositorio Statico, you can end up with more open connections than you would like (not to mention the memory consumption), after all you will have to wait for the goodwill of the 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 of mine: When to use Entity Framework with Repository Pattern?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.