locked
How to insert rows into the DB via code using Code First approach RRS feed

  • Question

  • User-1582932587 posted

    Hello,

    I am writing an app that has a Select Category drop-down menu. In my local db I just used insert sql query to test out if the drop-down menu is displayed. So these are the steps that I have done for my production db:

    1. Create a new production db

    2. Add a connection string with proper name, username, password in my web.config file

    3. Enable migrations

    4. Add migrations InitalCreate - which created an initial dbo._MigrationHistory

    5. Update database => which added an appropriate tables based on my model

    Now I need to add that insert statement but because I am using a code first approach if I insert rows in the sql server itself, it might get lost so what I want to do is add it through the code. Where should I add this in my code. 

    1. Do I need to add it in my 20162203_InitialCreate.cs class that was added through add migrations?

    2. Or do I need to add code into the Context.cs file? 

    Also do I just write a simple insert statement using linq?

    If anyone can point me to the article that gives a great explanation or has some sample code and explanations of why we are doing what we are doing, I would greatly appreciate it. 

    Thank you.

    Thursday, November 3, 2016 9:36 PM

All replies