Reputation: 3958
I will start by saying I am new to Entity Framework and am still learning about the concepts.
I have the following Model:
public class AppProduct
{
public int Id { get; set; }
public string Product { get; set; }
}
Which I then add to the context as follows:
public virtual DbSet<AppProduct> AppProducts { get; set; }
This is a basic table that should only have 3 entries which will be added once when the table is created and then not edited or deleted from then on.
Aside from loading SQL server and writing insert statements myself, is there a way to add these entries with entity framework?
I could write some insert statement in my code somewhere, but it seems to me that there should be some safe way of doing this within the existing framework.
I am thinking along the lines of adding insert statements to the migration file?
Searching for terms like add entry once using entity framework does not provide any answer that seem to address this issue.
Upvotes: 2
Views: 885
Reputation: 62213
You can use migrations or the DbContext initializer for this. Search on entity framework seed.
Upvotes: 3
Reputation: 85
If you are still looking for how to seed your table, This is a good tutorial on how to do with a good example. Please take a look once.
https://www.asp.net/web-api/overview/data/using-web-api-with-entity-framework/part-3 Use Code First Migrations to Seed the Database
Upvotes: 2
Reputation: 3840
Use a separate DbContext for your AppProduct table (Only enable migrations for the other DbContext).
Upvotes: 1