0

I am writing a CMS project with .NET Framework 4.6 but I ran into a problem at the beginning. The project has 3 layers, the data, domain and MVC layers are separate. I created the tables with fluent. Everything seems to be fine and runs without errors, but the project database is not created. I will also provide the link to the complete source code of the project. Please let me know if anyone knows the problem. github

In total, three tables must be created, and dbcontext and connectionstring of the project are as follows:

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
}
    <connectionStrings>
        <add name="CMS_DB" connectionString="Data Source=.;Initial Catalog=CMSProject_DB;Integrated Security=True" providerName="System.Data.SqlClient" />
    </connectionStrings>

Migration code:

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");
}

Problem solving: I should have defined the connection string in the data layer of the app.config file, not web.config.

1 Answer 1

0

Try to call the base method in your override.

protected override void OnModelCreating(ModelBuilder builder)
{          
    base.OnModelCreating(builder);
}
Sign up to request clarification or add additional context in comments.

3 Comments

I made the changes, but the data base still wasn't created.
Did you hide the datasource in the connectionString on purpose ?
I mainly set the datasource to '.' for development projects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.