首页 > 解决方案 > 在 ASP.NET Core Razor 应用程序中使用相同模型在数据库中创建多个表

问题描述

我正在尝试使用 ASP.NET Core Razor App 制作一个 Web 应用程序。

这样做时,我在创建一些新表时遇到了麻烦。

这是我的ApplicationDbContext.cs文件。

using System;
using System.Collections.Generic;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using MyWebApp.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace MyWebApp.Data
{
    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
            : base(options)
        {
            
        }

        //Users
        public DbSet<ApplicationUser> ApplicationUser { get; set; } //already created table

        //Freeboard
        public DbSet<Article> FreeboardArticle{ get; set; }
        public DbSet<Comment> FreeboardComment{ get; set; }

        //QuestionBoard
        public DbSet<Article> QuestionBoardArticle { get; set; }
        public DbSet<Comment> QuestionBoardComment { get; set; }
        
    }
}

通过add-migrationupdate-database命令后,

我预计新创建了 4 个表( FreeboardArticle, FreeboardComment, QuestionBoardArticle, ),QuestionBoardComment

但实际上我的数据库中新创建了2 个表 ( FreeboardArticle, )。FreeboardComment

我认为为什么会发生这种情况是因为我尝试使用相同的模型创建多个表,但我想这样做。

我怎样才能做到这一点?提前致谢。

++ 我的模型文件在这里。

Article.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Threading.Tasks;

namespace MyWebApp.Models
{
    public class Article
    {
        [Key]
        public int Id { get; set; }

        [Required]
        public string Title { get; set; }

        public string Content { get; set; }

        [Required]
        public DateTime RegisterDate { get; set; }

        [Required]
        public int Hit { get; set; }

        [Required]
        public string UserId { get; set; }

        [ForeignKey("UserId")]
        public ApplicationUser ApplicationUser { get; set; }


        public Article()
        {
            Hit = 0;
        }
    }
}

Comment.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Threading.Tasks;

namespace MyWebApp.Models
{
    public class Comment
    {

        [Key]
        public int Id { get; set; }

        [Required]
        public int ArticleId { get; set; }


        [Required]
        public string UserId { get; set; }
        [ForeignKey("UserId")]
        public ApplicationUser ApplicationUser { get; set; }

        [Required]
        public string Content { get; set; }

        public int? ParentId { get; set; }

        [Required]
        public bool IsReply { get; set; }

        [Required]
        public DateTime RegisterDate { get; set; }

        [Required]
        public bool IsDeleted { get; set; }


        public Comment()
        {
            IsReply = false;
            IsDeleted = false;
        }
    }
}

ApplicationUser.cs

using Microsoft.AspNetCore.Identity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;

namespace MyWebApp.Models
{
    public class ApplicationUser : IdentityUser
    {

        [Required]
        public string Name { get; set; }

        public string Address { get; set; }
        public string City { get; set; }
        public string PostalCode { get; set; }

    }
}

标签: c#asp.netasp.net-mvcrazordbset

解决方案


简单的方法:拥有一个包含所有属性的抽象基类,并映射具体类型:

public abstract class BaseClass
{
    public int Id { get; set; }
    public string StringField { get; set; }
    /* Other fields */ 
}

[Table("Table1")]
public class Table1 : BaseClass
{
}

[Table("Table2")]
public class Table2 : BaseClass
{
}

推荐阅读