Using Entity Framework 1 to 0..1 relationships, Non Persistent Object Space, User Defined Controls in XAF

In a relational database like SQL Server, we seek to store data in normal form so as to reduce redundancy and improve data integrity.

XAF is brilliant for creating C.R.U.D applications

However often times we want a UI that is different to the shape of the data as it in the tables.

The purpose of this project is to demonstrate XAF with a more flexible UI than is dictated by the database structure.

I setup a project to illustrate.

The project uses Winforms, Entity Framework Code First, and the XAF Conditional Appearance Module.

The database structure I want is as follows

Note the 1 to 0…1 relationship between Toys and the different sorts of Toys: Baby,Toddler,PreSchool

The application looks like this.

The Main section is for the business objects shaped the way I want to edit them. i.e NPToy.

The Data section is for the business objects shaped the way the database is defined.

You may like to run the project and compare the usability of the two ways!

The NPToy detail view contains controls that I have created to show the toy properties.
All except one of these controls are hidden. The Category controls which one is visible.

The database is set up using the following

using System;
using System.Data.Entity;
using System.Data.Common;
using DevExpress.ExpressApp.EF.Updating;
using DevExpress.ExpressApp.Design;

namespace Toys.Module.BusinessObjects {
	public class ToysDbContext : DbContext {
        public ToysDbContext(String connectionString)
            : base(connectionString)
           Database.SetInitializer(new MyInitializer());
        public ToysDbContext(DbConnection connection)
            : base(connection, false)
            Database.SetInitializer(new MyInitializer());
        public ToysDbContext()

        public DbSet<ModuleInfo> ModulesInfo { get; set; }
        public DbSet<Toy> Toys { get; set; }
        public DbSet<BabyToy> BabyToys { get; set; }
        public DbSet<ToddlerToy> ToddlerToys { get; set; }
        public DbSet<Brand> Brands { get; set; }
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
            modelBuilder.Entity<Toy>().HasOptional(x => x.BabyToy).WithRequired(y => y.Toy);
            modelBuilder.Entity<Toy>().HasOptional(x => x.PreSchoolToy).WithRequired(y => y.Toy);
            modelBuilder.Entity<Toy>().HasOptional(x => x.ToddlerToy).WithRequired(y => y.Toy);

I have used “NP” to stand for Nonpersistent because we will be using the NonPersistent object space. NonPersistent is a bit of a missnommer because we will be actually saving the data. It is just that we are not editing the entities directly.

Useful links

Toys on GitHub

How to perform CRUD operations with non persistent objects

Property Changed Events in Non Persistent Business Classes

Automatically refresh listview when detailview changes