问题描述
所以我制作了一个称为通知的新模型并添加了
So I made a new model called notifications and added
public virtual DBSet<Notification> Notifications { get; set; }
到我的上下文文件.所以我有:
to my context file. So I have:
public class myContext : IdentityDbContext
{
// ... my existing tables and models
public virtual DBSet<Notification> Notifications { get; set; }
}
我注意到该文件中没有使用 onConfiguring()
,还有一个 OnModelCreating()
似乎用于将身份信息映射到我的数据库表中像IdentityUser.
I noticed that theres an onConfiguring()
in this file thats not being used and an OnModelCreating()
which seems to be used to map Identity stuff to my db Tables like IdentityUser.
除了在我的上下文类中添加新行之外,我还需要做些什么才能使用我的新表?我注意到 OnModelCreating()
中未提及某些现有表,所以我不确定它们是如何添加的.我需要在数据库中物理添加一个新表还是由实体以某种方式处理它.
Besides adding that new line in my context class, what else would I have to do to be able to use my new table? I noticed that some of the existing tables aren't mentioned in OnModelCreating()
so I'm not sure how they were added. Would I have to physically add a new table in my db or does entity handle this some how.
推荐答案
您需要在 Package Manager控制台
-
启用迁移
:通过以下方式启用项目中的迁移创建一个Configuration类(每个项目只需要运行一次). -
添加迁移AddNotifications
:使用Up()
和Down()
方法按照指定的名称创建新的迁移类./li> -
更新数据库
:执行由Add-Migration
命令创建的最后一个迁移文件,并将更改应用于数据库架构.
Enable-Migrations
: Enables the migration in your project bycreating a Configuration class (need to be run only one time per project).Add-Migration AddNotifications
: Creates a new migration class as per specified name with theUp()
andDown()
methods.Update-Database
: Executes the last migration file created by theAdd-Migration
command and applies changes to the database schema.
您可以阅读有关迁移的更多信息实体框架代码优先迁移
You can read more about migrations Entity Framework Code First Migrations
这篇关于向现有.NET项目添加新模型/表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!