Creating a database with initial data using LINQ. RRS feed

  • Question

  • The following code creates the database, but doesn't enter the initial data afterward.  Why?

            // Creating the database if it doesn't exist on the current server.         
            public void CreateEmptySQLDatabase()
                SmalLibData myDb = new SmalLibData(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString);
                // Creation of database at the first run if it doesn't exist. 
                if (!myDb.DatabaseExists())
                    SmalLibData myDb1 = new SmalLibData(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString);
                    // Entering initial data to the database.
                    UserStatus Status1 = new UserStatus();
                    Status1.UserStatusDef = "Active";
                    UserStatus Status2 = new UserStatus();
                    Status2.UserStatusDef = "Passive";
                    EventType Event1 = new EventType();
                    Event1.EventTypeDef = "Borrowing";
                    EventType Event2 = new EventType();
                    Event2.EventTypeDef = "Returning";
                    ItemCopyStatus cs1 = new ItemCopyStatus();
                    cs1.ItemCopyStatusDef = "Borrowable";
                    ItemCopyStatus cs2 = new ItemCopyStatus();
                    cs2.ItemCopyStatusDef = "Damaged";
                    ItemCopyStatus cs3 = new ItemCopyStatus();
                    cs3.ItemCopyStatusDef = "Stolen/Lost";
                    ItemCopyStatus cs4 = new ItemCopyStatus();
                    cs4.ItemCopyStatusDef = "Library use only";
                }// end if
            }// func CreateEmptySQLDatabase
    • Moved by Linda Liu Monday, February 8, 2010 2:40 PM (From:Windows Presentation Foundation (WPF))
    Wednesday, February 3, 2010 11:49 AM

All replies

  • Because you haven't added the new objects to the myDb1 object before the SubmitChanges. You need to do this for each:

    UserStatus Status1 = new UserStatus();
    Status1.UserStatusDef = "Active";


    (note that the UserStatus is usually pluralized in the context, I'm only guessing that it is called UserStatuses in your context).

    Saturday, February 13, 2010 12:07 PM