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?
– Rafael Ferreira