Supply Initial Data (EF Core)
- 2 minutes to read
Note
Before you proceed, review the Inherit from the Business Class Library Class (EF Core) lesson.
Step-by-Step Instructions
Open the Updater.cs file located in the MySolution.Module project’s DatabaseUpdate folder. Add the following code to the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method.
using MySolution.Module.BusinessObjects; //... public class Updater : DevExpress.ExpressApp.Updating.ModuleUpdater { //... public override void UpdateDatabaseAfterUpdateSchema() { base.UpdateDatabaseAfterUpdateSchema(); Contact contactMary = ObjectSpace.FirstOrDefault<Contact>(x => x.FirstName == "Mary" && x.LastName == "Tellitson"); if(contactMary == null) { contactMary = ObjectSpace.CreateObject<Contact>(); contactMary.FirstName = "Mary"; contactMary.LastName = "Tellitson"; contactMary.Email = "tellitson@example.com"; contactMary.Birthday = new DateTime(1980, 11, 27); } //... ObjectSpace.CommitChanges(); } }
Run the application. Select the Contact item in the navigation control. Note that the new contact - “Mary Tellitson” - appears on the list to the right.
Detailed Explanation
Database Update
In step 1, you add code that checks the contact list for an entry that matches the name “Mary Tellitson”. If such a contact does not exist, your code creates it and adds it to the database.
An XAF application checks whether the database exists. If not, a new database is created.
After the database is created, the ModuleUpdater.UpdateDatabaseAfterUpdateSchema method is called. In this method, you can save the required business objects to the database.
Query Data
XAF uses an Object Space instance to manipulate persistent objects (see Create, Read, Update and Delete Data).
The sample uses the BaseObjectSpace.FirstOrDefault method call to find a record.
Next Lesson
Implement Custom Business Classes and Reference Properties (EF Core)