locked
Data not populating database with test class DBInitializer RRS feed

  • Question

  • User221438035 posted

    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 }

    public
    class Customer { public int ID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string LoginID { get; set; } public string LoginPassword { get; set; } public string ContactNumber { get; set; } public string PassPortNum { get; set; } public string StreetNumber { get; set; } public string StreetName { get; set; } public string Suburb { get; set; } public State? State { get; set; } public ICollection<Booking> Bookings { get; set; } }

    }

    This is part of the test data class.

        public class DbInitializer
    {
        public static void Initialize(FlightSystemContext context)
        {
            context.Database.EnsureCreated();
    
            //Look for any customers
            if (context.Customers.Any())
            {
                return;
            }
    
            var customers = new Customer[]
            {
                new Customer {ID=1,FirstName="Robert", LastName="Peters", LoginID="RPeters", LoginPassword="Test1234",
                ContactNumber="0404123123", PassPortNum="PPN000001", StreetNumber="12", StreetName="Smith Street", Suburb="Boowhap", State=State.SA},
                new Customer { ID=2,FirstName="Sam", LastName="Alexander", LoginID="SamAlexander", LoginPassword="Test1234",
                ContactNumber="0404123123", PassPortNum="PPN000002", StreetNumber="12", StreetName="Smith Street", Suburb="Boowhap", State=State.NSW},
                new Customer { ID=3, 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();

    I am finding that the EntityFrameworkcore is throwing an error some how . But can not work out why?

    Sunday, May 6, 2018 7:36 AM

All replies

  • User283571144 posted

    Hi TopTony,

    TopTony

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

    Best Regards,

    Brando

    Monday, May 7, 2018 5:35 AM