【发布时间】: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-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; }
}
}
【问题讨论】:
-
你的模型是什么样的?
-
@Vince 我添加了我的模型文件。谢谢。
标签: c# asp.net asp.net-mvc razor dbset