【问题标题】:Creating Multiple Tables in DB Using Same Model in ASP.NET Core Razor App在 ASP.NET Core Razor 应用程序中使用相同模型在数据库中创建多个表
【发布时间】:2020-07-01 06:33:38
【问题描述】:

我正在尝试使用 ASP.NET Core Razor 应用程序制作一个 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 个表(FreeboardArticleFreeboardCommentQuestionBoardArticleQuestionBoardComment),

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

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

我该怎么做?提前致谢。

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

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

    }
}

【问题讨论】:

  • 你的模型是什么样的?
  • @Vince 我添加了我的模型文件。谢谢。

标签: c# asp.net asp.net-mvc razor dbset


【解决方案1】:

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

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
{
}

【讨论】:

    【解决方案2】:

    有两种方法可以解决这个问题:

    1. 您可以让模型继承公共类:

    模型

     public class FreeboardArticle : Article {}
    
     public class FreeboardComment: Comment{}
    
     public class QuestionBoardArticle : Article {}
    
     public class QuestionBoardComment : Comment{}
    

    ApplicationDbContext.cs

     //Freeboard
     public DbSet<FreeboardArticle > FreeboardArticles{ get; set; }
     public DbSet<FreeboardComment> FreeboardComments{ get; set; }
    
     //QuestionBoard
     public DbSet<QuestionBoardArticle > QuestionBoardArticles { get; set; }
     public DbSet<QuestionBoardComment > QuestionBoardComments { get; set; }
    
    1. 其次,你可以调整你的人际关系:

    模型

    public class Article{}
    
     public class Comment{}
    
     public class FreeboardArticle {
       public int ArticleId{get;set;}
       public virtual Article Article{get;set;}      
     }
    
     public class FreeboardComment {
       public int CommentId{get;set;}
       public virtual Comment Comment{get;set;}
     }
    
     public class QuestionBoardArticle {
       public int ArticleId{get;set;}
       public virtual Article Article{get;set;}      
     }
    
     public class QuestionBoardComment {
       public int CommentId{get;set;}
       public virtual Comment Comment{get;set;}
     }
    

    ApplicationDbContext.cs

     public DbSet<Article> Articles{ get; set; }
     public DbSet<Comment> Comments{ get; set; }
    
     //Freeboard
     public DbSet<FreeboardArticle > FreeboardArticles{ get; set; }
     public DbSet<FreeboardComment> FreeboardComments{ get; set; }
    
     //QuestionBoard
     public DbSet<QuestionBoardArticle > QuestionBoardArticles { get; set; }
     public DbSet<QuestionBoardComment > QuestionBoardComments { get; set; }
    

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-25
      • 2020-10-24
      • 2021-11-06
      • 2019-03-05
      • 2020-09-12
      相关资源
      最近更新 更多