In this step, we create a utility class to help us obtain the required object for using NPersistence.
- Right click on the ‘code’ folder node, click ‘Add New Item…’
- In the opened dialog, choose 'Class', name it PersistenceUtils.cs and click Add.
- Go to the newly created PersistenceUtils class and paste the following code into it:
using NPersistence;
public class PersistenceUtils
{
private static EntityManager em;
private static EntityManagerFactory emf;
public static EntityManager getEm()
{
if (em == null)
{
emf = Persistence.CreateEntityManagerFactory("testPersistenceUnit");
em = emf.CreateEntityManager();
}
if (!em.IsOpen())
{
em = emf.CreateEntityManager();
}
return em;
}
internal static EntityManagerFactory getEmf()
{
return emf;
}
}