Entity framework 6 and Persisting record to DB
I have given myself a new ASP.Net Core project, with a backend MS SQL relationship database.
Normally I would create my own Entity Classe, use a DataContext connection and work from there (Complex Linq). As this projects new and entity framework has moved on since the last time I used it.

It did not take long for me to find my first problem. The idea and logic is simple but EF did not want to make it simple.

I have two tables, which has a one to many relationship and I needed to insert a parent and child records within the same post.
Normally I would do this using the below logic, but EF gave me an error saying that the parent id did not exists within the database.

Example
If you have a Order system the Parent could be the transaction record and the child could be the multiple items on the order, linked to the transactions

Basic logc

private static void basiceLogic()
{
int parentId = addMain("Food order");
int childId = addChild(parentId, "Pizza",5.99);
childId = addChild(parentId, "Chips",1.99);

}


Creating the Parent

private static int addMain(string name)
{
using (var context = new DatabaseEntity())
{
var table = new DBMainTable() { Name = name, };
context.DBMainTable.Add(table);
context.SaveChanges();

return table.Id;

}
}


Creating the Child

private static int addChild(int parentId, string name, decimal cost)
{
using (var context = new DatabaseEntity())
{
var table = new DBChildTable() { Name = name, Cost = cost };
context.DBChildTable.Add(table);
context.SaveChanges();

return table.Id;

}
}




My Resolve
Went back and forth on Stackoverflow and other site and could not find a resolve, but in the end I found out why and the below code resolved my issue

Create a global Database context

private static Models.Database.DatabaseEntity context;

Basic logc

private static void basiceLogic()
{
context = new Models.Database.DatabaseEntity();

int parentId = addMain("Food order");

//Dispose the current context and connection to db.
//This then will persist the record to the db (Insert,Updated or deleted)
//Then create new record
context.Dispose();
context = new Models.Blog.blogEntity();

int childId = addChild(parentId, "Pizza",5.99);
childId = addChild(parentId, "Chips",1.99);

context.Dispose();
}


Creating the Parent

private static int addMain(string name)
{

var table = new DBMainTable() { Name = name, };
context.DBMainTable.Add(table);
context.SaveChanges();

return table.Id;


}


Creating the Child

private static int addChild(int parentId, string name, decimal cost)
{

var table = new DBChildTable() { Name = name, Cost = cost };
context.DBChildTable.Add(table);
context.SaveChanges();

return table.Id;


}


Reason
When you use the "USING" element, this does not DISPOSE and close the connection to the current context connection.
Because of this, the parent record does not get persisted to the database and it wont be available for relationship required by the child record.
By doing the physical Dispose and reload of the context, it forces the parent record into the DB and creates a new connection for the EF with the Database and the child will see the relationship on insert.

Idea or Points
There is a hundred ways to screw a light bulb, so if someone has had this issue and resolved this another way.
Please comment and let me know.
Thanks
Created: 03/08/2016 Total Comment: 0

Comments

(Not Displayed)
Human validation
Enter 7888 number, before submitting to confirm your human
[Home] [All Blogs] [Contact] [Me]