简体   繁体   English

实体框架未创建数据库或表

[英]Entity Framework is not creating database or tables

  • Tried to first create the database using Microsoft SQL Server Managment Server. 试图首先使用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. 将应用程序作为一个空白项目启动,但是正在使用Web表单。
  • There was not anything in application start that would suggest Entity would create a database. 在应用程序启动中没有任何内容可以暗示Entity将创建数据库。
  • Verified that SQL Express was running. 验证SQL Express是否正在运行。

    • 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. 在VS中,我可以连接到服务器并在SQL Server Object Explorer中查看我的数据库,但是查看Server Explorer将显示我的连接名称,并且数据库将带有红色的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. 在服务器资源管理器下的VS中,我可以在服务器名称下键入。\\ SQLEXPRESS并查看系统数据库,但在“连接到数据库”下选择“ cfsEnergy DB”>“选择或输入数据库名称”。
  • Using Visual Studio 2015 使用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 尝试使用以下脚本将用户名和管理员帐户添加到我认为是SQL Express组的帐户中: https : //gist.githubusercontent.com/wadewegner/1677788/raw/16862d429feaa5d2d799533c548632365a2f04ff/addselftosqlsysadmin.cmd

/App_code/departments.cs /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 /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 /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 dbContextRun.cs

namespace cfsEnergyManagement.App_Code
{
    public class dbContextRun
    {

        cfsEnergyDb CfsEnergyDb = new cfsEnergyDb();

    }
}

Connection String: web.config 连接字符串: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. 因为您没有实例化cfsEnergyDb类。 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). 因此,尝试访问表中的数据,EF将在数据库中为您创建所有表(在连接字符串中指定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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