You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Framework 4.6三层CMS项目执行Update-Database后数据库未创建

Update-Database执行后数据库未创建的解决办法(.NET Framework 4.6三层CMS)

基于.NET Framework 4.6开发三层架构CMS,数据层、领域层、MVC层完全分离,使用Fluent API配置实体映射,项目运行无报错,但执行Update-Database命令后目标数据库未创建。


相关代码

DbContext实现

public class CMSContext : DbContext
{
    #region Tables
    public DbSet<PageGroup> PageGroups { get; set; }
    public DbSet<Page> Pages { get; set; }
    public DbSet<PageComment> PageComments { get; set; }
    #endregion

    #region Fluent
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new PageMap());
        modelBuilder.Configurations.Add(new PageGroupMap());
        modelBuilder.Configurations.Add(new PageCommentMap());

        base.OnModelCreating(modelBuilder);
    }
    #endregion
}

原连接字符串(位于MVC层web.config)

<connectionStrings>
    <add name="CMS_DB" connectionString="Data Source=.;Initial Catalog=CMSProject_DB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

迁移代码

public override void Up()
{
    CreateTable(
        "Portal.Comments",
        c => new
            {
                CommentId = c.Int(nullable: false, identity: true),
                PageId = c.Int(nullable: false),
                Name = c.String(nullable: false, maxLength: 150),
                Email = c.String(maxLength: 200),
                WebSite = c.String(maxLength: 200),
                Comment = c.String(maxLength: 500),
                CreateDate = c.DateTime(nullable: false),
            })
        .PrimaryKey(t => t.CommentId)
        .ForeignKey("Portal.Pages", t => t.PageId, cascadeDelete: true)
        .Index(t => t.PageId);
    
    CreateTable(
        "Portal.Pages",
        c => new
            {
                PageId = c.Int(nullable: false, identity: true),
                GroupId = c.Int(nullable: false),
                Title = c.String(nullable: false, maxLength: 350),
                ShortDescription = c.String(nullable: false, maxLength: 250),
                Text = c.String(nullable: false),
                Visit = c.Int(nullable: false),
                ImageName = c.String(),
                ShowInSlider = c.Boolean(nullable: false),
                CreateDate = c.DateTime(nullable: false),
            })
        .PrimaryKey(t => t.PageId)
        .ForeignKey("Portal.Groups", t => t.GroupId, cascadeDelete: true)
        .Index(t => t.GroupId);
    
    CreateTable(
        "Portal.Groups",
        c => new
            {
                GroupId = c.Int(nullable: false, identity: true),
                GroupTitle = c.String(nullable: false, maxLength: 150),
            })
        .PrimaryKey(t => t.GroupId);
}

public override void Down()
{
    DropForeignKey("Portal.Comments", "PageId", "Portal.Pages");
    DropForeignKey("Portal.Pages", "GroupId", "Portal.Groups");
    DropIndex("Portal.Pages", new[] { "GroupId" });
    DropIndex("Portal.Comments", new[] { "PageId" });
    DropTable("Portal.Groups");
    DropTable("Portal.Pages");
    DropTable("Portal.Comments");
}

解决办法

将连接字符串从MVC层的web.config迁移到**数据层项目的app.config**文件中。

补充说明

如果你的连接字符串名称(示例中为CMS_DB)与DbContext类名(CMSContext)不一致,需要在DbContext的构造函数中显式指定连接字符串名称,确保EF能正确读取:

public class CMSContext : DbContext
{
    public CMSContext() : base("CMS_DB") // 指定连接字符串名称
    {
    }

    // 其余代码保持不变...
}

内容的提问来源于stack exchange,提问作者hrmsystem

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 13:10:17