Update Single Column In Entity Framework

by Vahid 29. April 2011 18:00

I was working with Entity Framework and I needed to update a single column of a row in the database. basically I was logging the users visit to a web site (ASP.NET). so first time user lands in the website I create a record in database and keep the id in the session. when user’s session times out I needed to update the row and set the exit time of the user. it sounded a bit difficult to do this with Entity Framework I did it this way and worked pretty well:

        public void UpdateVisit(long id, string userId)
        {
            //id (row id) comes from the user so we create an object and assign the id
            VisitInformation visitInformation = new VisitInformation() { ID = id };
            try
            {
                using (var context = new TravIranLogEntities())
                {
                    //Here we attach the object to the context and set the state to unchanged
                    context.Entry(visitInformation).State = EntityState.Unchanged;
                    //update the column(s) that you need to modify
                    visitInformation.UserId = userId;
                    //Send the informatin back to the Database
                    context.SaveChanges();
                }

            }
            catch (Exception ex)
            {
                LogManager.Log("Error in UpdateVisit for user id", ex, LogMode.LogAndEmai);
            }

        }

Tags: ,

.Net

Perfect reference to get started with EntityFramework 4.1

by Vahid 27. April 2011 16:27

Just found these set of post in the MSDN site for getting started with EntityFramework 4.1. if you are new to entityframework this is a good point to start

http://msdn.microsoft.com/en-us/data/gg192989

Tags:

.Net

Could not create the driver from NHibernate.Driver.OracleDataClientDriver error in NHibernate

by Vahid 5. April 2011 03:59

I have been playing with Nhibernate to connect to an oracle database using ODP.NET. first POC project which was a console application worked properly but as soon as I moved the codes to a Web Application for POC I started getting “Could not create the driver from NHibernate.Driver.OracleDataClientDriver” error. scratching my head and think about the failure where the same exact code was running in the Console application, tried so many ways but no result. everything was in place, Oracle reference, Nhibernate reference and configuration etc…

finally I just thought of giving a shot to marking the Oracle.DataAccess database to be copied locally in the property window and surprisingly it started working. now don’t ask me why but if you face the same scenario just mark your set Copy Locally property of Oracle.DataAccess to true.

Tags:

SQL | Technical