Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

EF Exception:The context cannot be used while the model is being created

$
0
0

I'm trying to crate database depening on class variables.

My classes are defined as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CodeFirstAnnotationEntities
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public string Job { get; set; }
        public string Last { get; set; }

        public Department department;
    }
    public class Department
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Location { get; set; }
        public Employee employees;
    }
}

My DbContext class is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace CodeFirstAnnotationEntities
{
    public class CodeFirst_AnnotationEntities : DbContext
    {
        public CodeFirst_AnnotationEntities()
            : base("CodeFirstAnnotationConnection")
        {

            Database.SetInitializer<CodeFirst_AnnotationEntities>(new CreateDatabaseIfNotExists<CodeFirst_AnnotationEntities>());

        }

        public DbSet<Employee> Employees { get; set; }
        public DbSet<Department> Departments { get; set; }
     }
}

my connection String is

<add name="CodeFirstAnnotationConnection" connectionString="Data Source=Localhost;Initial Catalog=CodeFirstAnnotation;Integrated Security=true;" providerName="System.Data.SqlClient" />
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CodeFirstAnnotationEntities
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CodeFirst_AnnotationEntities context = new CodeFirst_AnnotationEntities();
            Employee emp = new Employee() { Name="Scott",Job="admin",Salary=40000,Last="Ben"};
            Department dept = new Department() { Name = "Accounts", Location = "NY" };
              try{
                context.Employees.Add(emp);
                context.Departments.Add(dept);
                context.SaveChanges();
            }
            catch(Exception ex)
            {

            }
        }
    }
}

I've tried using all types of SetInitializer

The exception I'm getting is: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.




Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>