Reputation: 81
I'm a bit new to Entity Framework and I am trying to connect to the Oracle Database through Entity Framework. Using Visual Studio 2015 Entity framework wizard, I created a database by reverse engineering our current database. When I start debugging a had an error:
An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll
Additional information: The context cannot be used while the model is being created. This exception may be thrown if the context is used inside the OnModelCreating method or if the same context instance is accessed by multiple threads concurrently. Note that instance members of DbContext and related classes are not guaranteed to be thread safe.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LibraryDLL.DAL;
namespace LibraryDLL
{
public class Class1
{
public void testDB()
{
using (EntitiesDBTest db = new EntitiesDBTest())
{
List<BOOK> books = db.BOOKS.ToList(); // <-- error
List<USER> users = db.USERS.ToList();
List<HISTORY> histories = db.HISTORies.ToList();
}
}
}
}
The code below was generated from a template.
namespace LibraryDLL.DAL
{
using System;
using System.Collections.Generic;
public partial class BOOK
{
public BOOK()
{
this.HISTORies = new HashSet<HISTORY>();
}
public int BOOKID { get; set; }
public string AUTHORFIRSTNAME { get; set; }
public string AUTHORLASTNAME { get; set; }
public string TITLE { get; set; }
public string SHORTDESCRIPTION { get; set; }
public string LONGDESCRIPTION { get; set; }
public virtual ICollection<HISTORY> HISTORies { get; set; }
}
}
How can I fix it?
Upvotes: 0
Views: 112
Reputation: 201
Check if you have included following configs in your App.Config file
<configuration>
<entityFramework>
<providers>
<provider invariantName="Oracle.ManagedDataAccess.Client" type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
and
<configuration>
<configSections>
<section name="Oracle.ManagedDataAccess.Client" type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.121.2.0, Culture=neutral, PublicKeyToken=89b483f429c47342" />
EDIT:
It's also a good idea to move this
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
down to the bottom of the configuration tag.
Upvotes: 1