Model Example Code
All code is based on the Microsoft Entity Framework Core Version 8.0 Click Here for an Entity Framework tutorial.
Entities
public Customer() { Orders = new HashSet<Order>(); } [StringLength(5)] public string CustomerID { get; set; } [Required] [StringLength(40)] public string CompanyName { get; set; } [StringLength(30)] public string ContactName { get; set; }
(...) [public ICollection<Order> Orders { get; set; } }
public sealed class Employee { public Employee() { Underlings = new HashSet<Employee>(); Orders = new HashSet<Order>(); } public int EmployeeID { get; set; } public string FullName { get; set; } (...)
[StringLength(30)] public string Title { get; set; } [Column("ReportsTo")] public int? SupervisorId { get; set; } public ICollection<Order> Orders { get; set; }
public ICollection<Employee> Underlings { get; set; } }
public sealed class Order { public int OrderID { get; set; } public string OrderName { get; set; } [StringLength(5)] public string CustomerID { get; set; } public int EmployeeID { get; set; } (...) public Customer Customer { get; set; } public Employee Employee { get; set; } }
Context Configuration
public class EmployeeConiguration : IEntityTypeConfiguration<Employee>
{
public void Configure(EntityTypeBuilder<Employee> builder)
{
builder.HasOne(p => p.Supervisor)
.WithMany(p => p.Underlings)
.HasForeignKey(p => p.SupervisorId);
}
}
public class OrderConfiguration : IEntityTypeConfiguration<Order>
{
public void Configure(EntityTypeBuilder<Order> builder)
{
builder.HasOne(p => p.Customer)
.WithMany(p => p.Orders)
.HasForeignKey(p => p.CustomerID).IsRequired();
builder.HasOne(p => p.Employee)
.WithMany(p => p.Orders)
.HasForeignKey(p => p.EmployeeID).IsRequired();
}
}
public class NorthwindDbContextEfCore : DbContextEfCore { public DbSet<Customer> Customers { get; set; } public DbSet<Employee> Employees { get; set; } public DbSet<Order> Orders { get; set; } (...) public override DbContextEfCore GetNewDbContextEfCore() { return new NorthwindDbContextEfCore(); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new EmployeeConiguration()); modelBuilder.ApplyConfiguration(new OrderConfiguration()); (...) } }
public class SystemDataRepositoryEfCore : SystemDataRepository { public override IDbContext GetDataContext()
{ return new NorthwindDbContextEfCore(); } public override IDbContext GetDataContext(DbDataProcessor dataProcessor) { return GetDataContext(); } }
Lookup Entities
public class OrderLookup
{
public string Order { get; set; }
public string Customer { get; set; }
public string Employee { get; set; }
}
public class CustomerLookup
{
public string CustomerId { get; set; }
public string CompanyName { get; set; }
public string ContactName { get; set; }
}
public class EmployeeLookup
{
public string Name { get; set; }
public string Title { get; set; }
public string Supervisor { get; set; }
}
public class OrderLookup
{
public string Order { get; set; }
public string Customer { get; set; }
public string Employee { get; set; }
}
Note: All TableDefinition Property Names must match their corresponding DbSet Property Names in the DbContextEfCore inheritor. All TableDefinitions are automatically instantiated in the base class’s constructor.
public class NorthwindLookupContextEfCore : LookupContext {
protected override DbContext DbContext => NorthwindDbContext; protected NorthwindDbContextEfCore NorthwindDbContext { get; }
public TableDefinition<Customer> Customers { get; set; } public TableDefinition<Employee> Employees { get; set; } public TableDefinition<Order> Orders { get; set; }
(...)
public LookupDefinition<OrderLookup, Order> OrdersLookup { get; private set; } public LookupDefinition<CustomerLookup, Customer> CustomersLookup { get; private set; } public LookupDefinition<EmployeeLookup, Employee> EmployeesLookup { get; private set; } public NorthwindLookupContextEfCore() { NorthwindDbContext = new NorthwindDbContextEfCore(this); }
protected override void InitializeLookupDefinitions() { OrdersLookup = new LookupDefinition<OrderLookup, Order>(Orders); OrdersLookup.AddVisibleColumnDefinition(p => p.Order , "Order", p => p.OrderName, 20); OrdersLookup.Include(p => p.Customer) .AddVisibleColumnDefinition(p => p.Customer , "Customer" , p => p.CompanyName, 50); OrdersLookup .Include(p => p.Employee) .AddVisibleColumnDefinition(p => p.Employee , "Employee" , p => p.FullName, 30); Orders.HasLookupDefinition(OrdersLookup);
CustomersLookup = new LookupDefinition<CustomerLookup, Customer>(Customers); CustomersLookup.AddVisibleColumnDefinition(p => p.CustomerId , "Customer Id", p => p.CustomerID, 20); CustomersLookup.AddVisibleColumnDefinition(p => p.CompanyName , "Company Name", p => p.CompanyName, 40); CustomersLookup.AddVisibleColumnDefinition(p => p.ContactName , "Contact", p => p.ContactName, 40); Customers.HasLookupDefinition(CustomersLookup);
EmployeesLookup = new LookupDefinition<EmployeeLookup, Employee>(Employees); EmployeesLookup .AddVisibleColumnDefinition(p => p.Name , "Name" , p => p.FullName, 40); EmployeesLookup .AddVisibleColumnDefinition(p => p.Title , "Title" , p => p.Title, 20); EmployeesLookup .Include(p => p.Supervisor) .AddVisibleColumnDefinition(p => p.Supervisor , "Supervisor" , p => p.FullName, 40); Employees.HasLookupDefinition(EmployeesLookup); }