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.
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.
Please mark the most helpful post(s) as Answer Blog | I need more space:DropBox Referral
saamisolutio...
Member
39 Points
74 Posts
Where do you store the Entity context object between postbacks
Jul 17, 2010 02:28 PM|LINK
Hi,
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.
Thank You
Entity Framework ASP.NET 2010 Framework 4.0
Programming your future...
whighfield
Star
11721 Points
1859 Posts
Re: Where do you store the Entity context object between postbacks
Jul 17, 2010 04:16 PM|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 http://www.mindscape.co.nz/blog/index.php/2008/05/12/using-the-unit-of-work-per-request-pattern-in-aspnet-mvc/
Blog | I need more space:DropBox Referral
saamisolutio...
Member
39 Points
74 Posts
Re: Where do you store the Entity context object between postbacks
Jul 17, 2010 04:23 PM|LINK
Hi,
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"]; if (!IsPostBack) { PublishContext = new PublishingCompanyEntities(); Session["PublishContext"] = PublishContext; ddlAuthors.DataSource = PublishContext.Authors; ddlAuthors.DataTextField = "FirstName" + "-" + "LastName"; ddlAuthors.DataValueField = "AuthorID"; ddlAuthors.DataBind(); } }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 select p; List<Payroll> lstPayroll = PayRollQuery.ToList(); if (lstPayroll != null && lstPayroll.Count > 0) { CurrentPayroll = lstPayroll.First(); Session["CurrentPayroll"] = CurrentPayroll; } else { CurrentPayroll = null; } }Programming your future...
saamisolutio...
Member
39 Points
74 Posts
Re: Where do you store the Entity context object between postbacks
Jul 17, 2010 04:36 PM|LINK
Hi Whighfield,
Thanks for your reference link.
I am trying to understand the concept, but its to heavy for me to go through.
Thanks again.
Programming your future...
whighfield
Star
11721 Points
1859 Posts
Re: Where do you store the Entity context object between postbacks
Jul 17, 2010 04:48 PM|LINK
Some pro/con discussions on ASP.NET Session state http://stackoverflow.com/questions/133236/asp-net-session
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.
Blog | I need more space:DropBox Referral