locked
ENTITY FRAMEWORK, .MDF NOT BEING CREATED AND PUT INTO APP_DATA FOLDER RRS feed

  • Question

  • User-953356284 posted

    Hi,

    I have created the class models, context class, intializer class & updated the web.config & global.asax files and have built the application and it returns no errors.

    However, it is not building the database and putting it in the APP Data folder, anyone able to help?

    //CONTEXT CLASS
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    
    namespace LJLNew.Models
    {
        public class CvContext : DbContext
        {
            public CvContext(): base("LJLNew")
            {
            }
            public DbSet<cvdetail> cvdetails { get; set; }
            public DbSet<education> educations { get; set; }
            public DbSet<employment> employments { get; set; }
            public DbSet<qualification> qualifications { get; set; }
            public DbSet<reference> references { get; set; }
        }
    }
    //Initializer Class
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.Entity;
    
    namespace LJLNew.Models
    {
        public class CvInitializer : DropCreateDatabaseIfModelChanges<CvContext>
        {
            protected override void Seed(CvContext context)
            {
                base.Seed(context);
            }
        }
    }

    //GLOBAL.ASAX
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Optimization;
    using System.Web.Routing;
    using System.Web.Security;
    using System.Web.SessionState;
    using System.Data.Entity;
    using LJLNew.Models;
    
    namespace LJLNew
    {
        public class Global : HttpApplication
        {
            void Application_Start(object sender, EventArgs e)
            {
                // Code that runs on application startup
                RouteConfig.RegisterRoutes(RouteTable.Routes);
                BundleConfig.RegisterBundles(BundleTable.Bundles);
    
              // Initialize the product database.
                Database.SetInitializer(new CvInitializer());
            }
        }
    }



    //WEB CONFIG FILE  
    
    
    <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-LJLNew-20151126115905.mdf;Initial Catalog=aspnet-LJLNew-20151126115905;Integrated Security=True"
          providerName="System.Data.SqlClient" />
    
        <add name="LJLNew"
             connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ljlnew.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
      </connectionStrings>
    Thursday, November 26, 2015 7:15 PM

Answers

  • User-986267747 posted

    Hi LukeHarris01,

    However, it is not building the database and putting it in the APP Data folder, anyone able to help?

    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

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 27, 2015 2:04 AM