Last post Jul 17, 2010 04:48 PM by whighfield
Jul 17, 2010 02:28 PM|saamisolutions|LINK
I am trying out Entity Framework in ASP.Net applications.
Should I recreate the context object between postbacks or can I store it in session or viewstate, which one is the best practise?
Also can you point me out a sample tutorial where Entity framework is used with ASP.Net application which includes insert,update, executing stored procedue etc, most examples which I find in internet are based on windows application. Though the concept remains
the same I would like to see how the objects are managed in stateless environment.
Entity Framework ASP.NET 2010 Framework 4.0
Jul 17, 2010 04:16 PM|whighfield|LINK
Stay away from the session or viewstate, what you probably want is the context object per-request.
Here is an article that talks about it along with the Unit Of Work-per-Request
Jul 17, 2010 04:23 PM|saamisolutions|LINK
I am adding additional detail on how I am doing it right now
protected void Page_Load(object sender, EventArgs e)
if (Session["PublishContext"] != null)
PublishContext = (PublishingCompanyEntities)Session["PublishContext"];
PublishContext = new PublishingCompanyEntities();
Session["PublishContext"] = PublishContext;
ddlAuthors.DataSource = PublishContext.Authors;
ddlAuthors.DataTextField = "FirstName" + "-" + "LastName";
ddlAuthors.DataValueField = "AuthorID";
so on postback I am having it in session
So When I have to update
protected void ddlAuthors_OnSelectedIndexChanged(object sender, EventArgs e)
int intSelectedAuthorId = Convert.ToInt32(ddlAuthors.SelectedValue);
IQueryable<Payroll> PayRollQuery = from p in PublishContext.Payrolls
where p.Author.AuthorID == intSelectedAuthorId
List<Payroll> lstPayroll = PayRollQuery.ToList();
if (lstPayroll != null && lstPayroll.Count > 0)
CurrentPayroll = lstPayroll.First();
Session["CurrentPayroll"] = CurrentPayroll;
CurrentPayroll = null;
Jul 17, 2010 04:36 PM|saamisolutions|LINK
Thanks for your reference link.
I am trying to understand the concept, but its to heavy for me to go through.
Jul 17, 2010 04:48 PM|whighfield|LINK
Some pro/con discussions on ASP.NET Session state
If this is an internal site with few users then the session state is probably not a big deal to use (in moderation) if the site is public with hundreds of concurent users probably a bad idea.