I watched some videos about Entity
and saw two different versions, in a first it was something simple and straightforward (I believe the raw form of Entity), where it only instantiated the context of the bank and applied the add, :
using(sampleEntities ctx = new sampleEntities()){
client clt = new client();
clt.name = txtName.Text;
clt.phone = txtPhone.Text;
ctx.Add(clt);
ctx.SaveChanges();
But another teacher in another video does something different using the DAL
, BLL
and UI
layers, this time also adding a Session that would "isolate" each operation of each user on the site, while the first version supposedly would have conflicts between the many users who would be making use of the same connection for several operations simultaneously, follow the example:
public static sample01Entities Current
{
get
{
if (System.Web.HttpContext.Current.Session["SampleDbContext"] == null)
{
db = new sample01Entities();
System.Web.HttpContext.Current.Session["SampleDbContext"] = db;
}
return db;
}
}
And here in the DALCity class:
public void Add(cidade c)
{
SampleDbContext.Current.cidade.Add(c);
SampleDbContext.Current.SaveChanges();
SampleDbContext.Current.ChangeTracker.Entries<cidade>();
}
The question is: is it safe to use the first form for CRUD
operations without compromising code / site / security etc? Or should I always use the second example with the Session to "individualize" operations and connections?
Thank you all.