0

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; }
}

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.

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.

3
  • Are you using Entity Framework? If you are, then just use the update model feature from the EDMX. stackoverflow.com/questions/6466347/… Commented Apr 6, 2018 at 19:29
  • Depends on two of things you didn't mention (yet): 1) how did you create/maintain the database and the EF class model until now? 2) Is Notification related to any other existing entity class? Besides that its always good to use the applicable EF version tag. Commented Apr 6, 2018 at 20:08
  • I'm taking over this project so I'm not quite sure how it was maintained before. I am using the Entitty Framework. Notification is related to one other table. Commented Apr 6, 2018 at 20:56

1 Answer 1

3

You need to run tow below commands in Package Manager Console

  1. Enable-Migrations: Enables the migration in your project by creating a Configuration class (need to be run only one time per project).
  2. Add-Migration AddNotifications: Creates a new migration class as per specified name with the Up() and Down() methods.
  3. Update-Database: Executes the last migration file created by the Add-Migration command and applies changes to the database schema.

You can read more about migrations Entity Framework Code First Migrations

Sign up to request clarification or add additional context in comments.

13 Comments

Does this create the table within the database? What if the table already exists?
Yes, If exist it will only update it with new changes if there is any changes in the model
And if any change is made on the database side, will the compiler catch this when compiling the web application? Won't it want another migration?
No, You are using Code First and your changes should be add in model first then migrated to the database, Also any changes not migrated will throw an exception.
Thanks for confirming. Consequently, I am not a fan of allowing the web application to dictate the creation and management of the data model >:[
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.