Hi I am creating a database for use with EntityFramework using code first approach. I can get the database to create itself but my Dbinitializer is not populating the MSSQL database with test data. I have run through the debugger and am not finding any exceptions
thrown.
This one of the DB table classes.
namespace FlightBooking.Models
{ public enum State { SA, WA, VIC, NSW, QLD, NT, TAS, ACT }
Hi I am creating a database for use with EntityFramework using code first approach. I can get the database to create itself but my Dbinitializer is not populating the MSSQL database with test data. I have run through the debugger and am not finding any exceptions
thrown.
According to your codes, I have created a test demo on my side.
I have below error when running Initialize method.
SqlException: Cannot insert explicit value for identity column in table 'Customers' when IDENTITY_INSERT is set to OFF.
As far as I know, the ef core will enable IDENTITY_INSERT for the id column.
I suggest you could try below codes:
public class DbInitializer
{
public static void Initialize(FlightSystemContext context)
{
context.Database.EnsureCreated();//if db is not exist ,it will create database .but ,do nothing .
//Look for any customers
if (context.Customers.Any())
{
return;
}
var customers = new Customer[]
{
new Customer {FirstName="Robert", LastName="Peters", LoginID="RPeters", LoginPassword="Test1234",
ContactNumber="0404123123", PassPortNum="PPN000001", StreetNumber="12", StreetName="Smith Street", Suburb="Boowhap", State=State.SA},
new Customer { FirstName="Sam", LastName="Alexander", LoginID="SamAlexander", LoginPassword="Test1234",
ContactNumber="0404123123", PassPortNum="PPN000002", StreetNumber="12", StreetName="Smith Street", Suburb="Boowhap", State=State.NSW},
new Customer { FirstName="Tom", LastName="Alexander", LoginID="TAlexander", LoginPassword="Test1234",
ContactNumber="0404123123", PassPortNum="PPN000003", StreetNumber="12", StreetName="Smith Street", Suburb="Boowhap", State=State.TAS}
};
foreach (Customer c in customers)
{
context.Customers.Add(c);
}
context.SaveChanges();
}
}
None
0 Points
1 Post
Data not populating database with test class DBInitializer
May 06, 2018 07:36 AM|TopTony|LINK
Hi I am creating a database for use with EntityFramework using code first approach. I can get the database to create itself but my Dbinitializer is not populating the MSSQL database with test data. I have run through the debugger and am not finding any exceptions thrown.
This one of the DB table classes.
}
This is part of the test data class.
I am finding that the EntityFrameworkcore is throwing an error some how . But can not work out why?
</div> </div>Star
8931 Points
2723 Posts
Re: Data not populating database with test class DBInitializer
May 07, 2018 05:35 AM|Brando ZWZ|LINK
Hi TopTony,
According to your codes, I have created a test demo on my side.
I have below error when running Initialize method.
As far as I know, the ef core will enable IDENTITY_INSERT for the id column.
I suggest you could try below codes:
Best Regards,
Brando