locked
Entity Framework Not Generating Database Tables RRS feed

  • Question

  • User1231829591 posted

    Hi all, I have a web app which is composed of 3 layers; namely Presentation, BusinessLogic, and DataAccess that is supposed to display data from a database.  I am using Entity Framework code first to generate my database tables but at the moment it is not working. I added a connection string to the Web.Config file in the Presentation layer and tried to run my web app so that  my code will generate my database tables. 

    I planned to add data into the table fields after the tables are created but my code failed to create the tables. So I tried adding a connection string to the AppConfig file in the DataAccess layer where I have my DbContext and Repository classes but that still didn't work.. Please help suggest what might be causing my problems, thanks in advance

    Sunday, December 13, 2015 6:35 PM

Answers

  • User1231829591 posted

    Hi thanks for replying but your suggestion did not help my situation. It turns out I was missing several files so I deleted my project, reinstalled Entity Framework, and recreated my project and this time around my code runs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 16, 2015 5:19 AM

All replies

  • User-986267747 posted

    Hi ManyTitles,

    Please help suggest what might be causing my problems, thanks in advance

    In my experience, DataBase Initialization consists of two steps. First, Using Code First to create a model in memory based on the default rules and configuration. Secondly, using the database that has been set up will be used to initialize the database for storing data. Initialization is delayed, so an instance is created that is not entirely satisfied with the conditions of the initialization. Operations that must be performed on the model, such as query or add entities. Of course, we could also call the DbContext.Database.Initialize method, in the absence of the model to perform any operation of the mandatory initialization.

    using (var context = new CvContext())
                    {
                        context.Database.Initialize(true);
                    }

    You could refer to the following link to know the knowledge about the problem.

    http://stackoverflow.com/questions/6570901/entity-framework-not-creating-database

    Besides, You could refer to the following link to look how to create database using entity framework code first.

    https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    Monday, December 14, 2015 1:59 AM
  • User1231829591 posted

    Hi, thanks for replying. After reading your comments I thought about initializing my classes with constructors but I am getting the error

                       Object Reference Not Set To An Instance Of An Object

    The following are my classes in the DataAccess layer

     namespace DataAccess
    {
        public class Department
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Location { get; set; }
            public virtual List<Employee> Employees { get; set; }
    
            public Department()
            {
                this.Employees = new List<Employee>();
            }
        }
    
        public class Employee
        {
            public int ID { get; set; }
            public string EmployeeId { get; set; }
            public string FirstName { get; set; }        
            public string LastName { get; set; }        
            public string Gender { get; set; }
            public double Salary { get; set; }
            public virtual List<Department> Departments { get; set; }
    
            public Employee()
            {
                this.Departments = new List<Department>();
            }
        }
    
        class EmployeeDBContext:DbContext
        {
            public DbSet<Department> Departments { get; set; }
            public DbSet<Employee> Employees { get; set; }
        }
        
         public class EmployeeRepository
        { 
            public List<Department> GetDepartments()
            {
                EmployeeDBContext employeeDbContext = new EmployeeDBContext();
    
                return employeeDbContext.Departments.Include("Employees").ToList();
            }
          
            public Employee GetEmployeeByID(string id)
            {
                EmployeeDBContext employeeDbContext = new EmployeeDBContext();
                return employeeDbContext.Employees.Find(id);
            } 
        }
    }

    The following is my Business layer

    namespace BusinessLogic
    {
        public class Business
        {
            EmployeeRepository empRep = new EmployeeRepository();
            public Employee GetEmployeeByID(string id)
            {
                return empRep.GetEmployeeByID(id);
            }
            public List<Department> GetDepartments()
            {
                return empRep.GetDepartments();
            }
        }   
    }

    The line of code that is causing the error to be thrown is 

     return employeeDbContext.Departments.Include("Employees").ToList();

    I thought that the constructor would take care of this error but it didn't. Please point out what I am missing.

    Monday, December 14, 2015 7:20 AM
  • User-986267747 posted

    Hi ManyTitles,

     Object Reference Not Set To An Instance Of An Object

    public List<Department> GetDepartments() { EmployeeDBContext employeeDbContext = new EmployeeDBContext(); return employeeDbContext.Departments.Include("Employees").ToList(); }

    According to the error message, it seems like the return value of this method called GetDepartments is null and caused this problem. You could try to modify your code, like this:

        public List<Department> GetDepartments()
            {
                List<Department> list = new List<Department>();
                var getList=empRep.GetDepartments();
    
                if (getList == null)
                {
                    return list;
                }
                else
                {
                    return getList;
                }
                 
            }

    Best Regards,

    Klein zhang

    Tuesday, December 15, 2015 9:36 AM
  • User1231829591 posted

    Hi thanks for replying but your suggestion did not help my situation. It turns out I was missing several files so I deleted my project, reinstalled Entity Framework, and recreated my project and this time around my code runs.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, December 16, 2015 5:19 AM