“New transaction is not allowed because there are other threads running in the session.” : Entity Framework

We often face this exception, “New transaction is not allowed because there are other threads running in the session.” when we try to try to save the object after adding to the context or after the edit action at “ObjectContext.SaveChanges()”.

First let us see, why this occurs. Consider the following snippet,

var entity = from e in context.Table_name

                      where e.id == id

                     select e;  //Selects the record from db with  id

foreach(var entityRecord in entity)

{

entityRecord.name =”some string”;

context.ObjectStateManager.ChangeObjectState(entity,EntityState.Modified);

context.SaveChanges(); //The exception arises here

}

The reason for this exception is, the “entity” object is of type Enummerable and it has to be converted into a list before it is iterated for edit/delete. Please find the change highlighted below:

var entity = from e in context.Table_name

                      where e.id == id

                     select e;  //Selects the record from db with  id

foreach(var entityRecord in entity.ToList)

{

entityRecord.name =”some string”;

context.ObjectStateManager.ChangeObjectState(entity,EntityState.Modified);

context.SaveChanges();

}

You can also do it, as soon as the LINQ ends. Hope it will solve the issue. Happy Coding!

Sharepoint Beginners tutorial

Sharepoint Beginners tutorial

SharePoint is a Microsoft technology which facilitates creation of intranet and internet sites with promising features like Collaboration, Document sharing and Management (with version control) , Content Management, websites, Business Intelligence with a highly compatible user management and security module. The stand-apart features of SharePoint in par with other Content Management Systems are, highly friendly interfaces for non-technical users; Flexible environment to developers for customizing the features and now it also offers Windows Azure support for SharePoint applications that provides maximum scalability and availability.

Interaction with your Microsoft office package(word, excel, visio, outlook,etc), Maintaining document repository with the version control support, Independent User management and privilege settings can be applied at various level, Use of web-parts (a page/ a piece of a web page viewed from another), Calender to keep track of tasks and appointments etc at one place make SharePoint unique.

Cited above is a wonderful website for those who want to kick-start with the SharePoint learning.Apart from the Video tutorials, I would recommend my fellow learners to read this wiki-page: http://en.wikipedia.org/wiki/Sharepoint .

Happy Learning!

Entity Framework Exception : “the underlying provider failed on open”

I came across this exception while calling a method in class library project (that in turns call a WCF service method), from another class library project. I have made use of the N-tier architecture that simply passes the objects of model classes to the other layers referencing their classes.

The exception ” the underlying provider failed on open”, occurred since my method inside the class library project, could not read the Connection string from the web.config in the service layer.

Let us now see a brief about how to solve the error:

1. Add a property, “ConnectionString” in the Model class (a class with collection of properties that is used for data transfer).

public class Model

// a class with collection of properties

.

public string ConnectionString {get;set;}

}

2. In the service layer, fetch the connection string and assign it to the model object’s ConnectionString property.

public void method(Model obj)

{

obj.ConnectionString = ConfigurationManager.ConnectionStrings[“connectionStringName”].ConnectionString;

Logic logicObj = new Logic(); //The class you are using the Data context of entity framework

logicObj.method(obj);

}

3. Now in the Class, where you make use of Entity framework, pass the connection string property as a parameter for data entity class’ constructor and write your logic.

public void method(Model obj)
{
using (DataEntities context = new DataEntities(obj.ConnectionString))
{
//your business logic goes here
.
.
context.AddToModel(obj);
context.SaveChanges();
}
}

Hope this implementation solves the issue! Happy coding!!!