c# - 在 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-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; }
}
}
解决方案
简单的方法:拥有一个包含所有属性的抽象基类,并映射具体类型:
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
{
}
推荐阅读
- python - pyspark 在 groupBy 之后使用 agg 连接字符串
- python - GCP 通过 api 创建负载均衡器
- ruby-on-rails - Docusign 宝石块请求
- swift - 在某个点停止动画并从该点重新启动
- android - 无法使用 android 模拟器上的 Firestore 模拟器访问云 Firestore 后端
- kubernetes - 从 pod 内解析外部域不起作用
- javascript - 尝试在 DOM 中添加删除按钮
- python - 如何在熊猫上堆叠这个特定的行?
- angular - 在 Windows 中安装 @angular/cli@next --global 时出错
- javascript - Angular JavaScript AutoComplete 找不到地址字段(将自定义字段添加到自动完成)