繁体   English   中英

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

[英]Entity Framework is not creating database or tables

  • 试图首先使用Microsoft SQL Server Managment Server创建数据库。 我无法使用当前的凭据,因为创建新数据库导致错误。 这是通过使用我的管理员凭据解决的。
  • 将应用程序作为一个空白项目启动,但是正在使用Web表单。
  • 在应用程序启动中没有任何内容可以暗示Entity将创建数据库。
  • 验证SQL Express是否正在运行。

    • 在VS中,我可以连接到服务器并在SQL Server Object Explorer中查看我的数据库,但是查看Server Explorer将显示我的连接名称,并且数据库将带有红色的x。
    • 在服务器资源管理器下的VS中,我可以在服务器名称下键入。\\ SQLEXPRESS并查看系统数据库,但在“连接到数据库”下选择“ cfsEnergy DB”>“选择或输入数据库名称”。
  • 使用Visual Studio 2015

  • 尝试使用以下脚本将用户名和管理员帐户添加到我认为是SQL Express组的帐户中: 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();

    }
}

连接字符串:web.config

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

因为您没有实例化cfsEnergyDb类。 仅当您尝试从数据库表(任何数据库查询)首次访问任何数据时,仅实体框架,并且仅创建数据库。 因此,尝试访问表中的数据,EF将在数据库中为您创建所有表(在连接字符串中指定)。

暂无
暂无

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

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