I'm trying to make a Web App using ASP.NET Core Razor App.
While doing this, I have a trouble with creating some new tables.
Here is my ApplicationDbContext.cs
file.
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; }
}
}
After passing add-migration
and update-database
commands,
I expected that 4 tables (FreeboardArticle
, FreeboardComment
, QuestionBoardArticle
, QuestionBoardComment
) are newly created,
but actually 2 tables (FreeboardArticle
, FreeboardComment
) are newly created in my db.
I think why this happens is because I tried to create multiple tables using same model, but I want to do this.
How can I do this? Thanks in advance.
++ My model files are here.
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; }
}
}
The easy way: have an abstract base class with all the properties, and map concrete types:
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
{
}