在 ASP.NET 核心 Razor 应用程序中使用相同模型在数据库中创建多个表
Creating Multiple Tables in DB Using Same Model in ASP.NET Core Razor App
我正在尝试使用 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-migration
和update-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; }
}
}
有两种方法可以解决这个问题:
- 你可以让你的模型继承通用的 class:
型号
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; }
- 其次你可以调整你的人际关系:
型号
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; }
简单的方法:拥有一个包含所有属性的抽象基础 class,并映射具体类型:
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
{
}
我正在尝试使用 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-migration
和update-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; }
}
}
有两种方法可以解决这个问题:
- 你可以让你的模型继承通用的 class:
型号
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; }
- 其次你可以调整你的人际关系:
型号
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; }
简单的方法:拥有一个包含所有属性的抽象基础 class,并映射具体类型:
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
{
}