简体   繁体   中英

Entity Framework is not creating database or tables

  • Tried to first create the database using Microsoft SQL Server Managment Server. I was not able to use my current credentials as creating a new database resulted in an error. This was solved by using my admin credentials.
  • Started the application as a blank project but am using web forms.
  • There was not anything in application start that would suggest Entity would create a database.
  • Verified that SQL Express was running.

    • In VS I am able to connect to the server and view my databases in SQL Server Object Explorer, however viewing Server Explorer will show my connection name and the database will have a red x.
    • In VS under server explorer, I can type in .\\SQLEXPRESS under server name and see the system databases but I cannot see my cfsEnergy DB under Connect to a database > Select or enter a database name.
  • Using Visual Studio 2015

  • Tried to add my username and admin account to the what I believe is the SQL Express group using the following script: https://gist.githubusercontent.com/wadewegner/1677788/raw/16862d429feaa5d2d799533c548632365a2f04ff/addselftosqlsysadmin.cmd

/App_code/departments.cs

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

namespace cfsEnergyManagement.App_Code
{
    public class department
    {
        public string ID { get; set; }
        public string title { get; set; }
        public List<utilityUse> utilityUse { get; set;  }
    }
}

/App_code/utilityUse.cs

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

namespace cfsEnergyManagement.App_Code
{
    public class utilityUse
    {
        public string utilityID { get; set; }
        public string title { get; set; }
        public string year { get; set; }
        public string month { get; set; }
        public int kiloWattHour { get; set; }
        public int tonHour { get; set;  }
        public int kiloPoundsHour { get; set; }
        public int netCost { get; set; }
    }
}

/App_Code/dbContext.cs

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

namespace cfsEnergyManagement.App_Code
{
    public class cfsEnergyDb: DbContext
    {
    public DbSet<department> departments { get; set; }
      public DbSet<utilityUse> utilityUses { get; set; }

    }
}

dbContextRun.cs

namespace cfsEnergyManagement.App_Code
{
    public class dbContextRun
    {

        cfsEnergyDb CfsEnergyDb = new cfsEnergyDb();

    }
}

Connection String: web.config

<connectionStrings>
    <add name="cfsEnergyDb" connectionString="server=SQLEXPRESS;integrated security=SSPI;database=cfsEnergy" providerName="System.Data.SqlClient" />
</connectionStrings>

Because you are not instantiating cfsEnergyDb class. Entity framework only and only create database when you will try to access any data for the first time from database table(any db queries). So try to access data from table and EF will create all the tables for you in database(specified in connection string).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM