expressappframework-113630-getting-started-in-depth-tutorial-winforms-webforms-business-model-design-business-model-design-with-entity-framework-6-inherit-from-the-business-class-library-class-ef.md
In this lesson, you will learn how to implement business classes for your application using the Business Class Library. This library contains the most typical ready-to-use business classes. You will implement a custom Contact class by deriving from the Person class available in this library, and implement several additional properties. You will also learn the basics of automatic user interface construction based on data.
Typically, business classes should be implemented in a platform-independent module project, so that the same objects will be available in both WinForms and ASP.NET Web Forms applications. To add a new class, right-click the Business Objects folder in the MySolution.Module project, and choose Add | Class… to invoke the Add New Item dialog. In this dialog, specify “Contact” as the new item’s name and click Add. As a result, you will get an automatically generated code file with a single class declaration.
Replace the automatically generated class declaration with the following code.
After the class declaration, add all new business objects to the solution’s DbContext. Since Contact is a descendant of Person , entities used in the Person class should also be registered. Edit the BusinessObjects_MySolutionDbContext.cs_ file as shown below.
At this step, the business model has already been declared, but if you have started the application with another version of DbContext , you will get the following error: “The model backing the ‘MySolutionDbContext’ context has changed since the database was created. Consider using Code First Migrations to update the database (https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/migrations/)_“. To avoid this error, drop the database every time you change something in the business model (create a new class, add a new attribute to an existing class, rename a class or an attribute, etc.). To do it automatically during the debug, uncomment the following code in the MySolution.Module_Module.cs file.
Run the WinForms or ASP.NET Web Forms application. You will see how the user interface is automatically generated using the specified data structures. A navigation control allows you to display the Contact list. You can customize this collection using the corresponding editors. If you click the New button or double-click an existing record, the application will show a detail form (Detail View) filled with editors for each data field.
You can see the code demonstrated here in the MySolution.Module | Data | Contact.cs (Contact.vb) file of the EF Demo (Code First) installed with XAF. By default, the EF Demo (Code First) application is installed in %PUBLIC%\Documents\DevExpress Demos 21.2\Components\eXpressApp Framework\EFDemoCodeFirst.
Next Lesson: Supply Initial Data (EF 6)
See Also
Business Classes vs Database Tables