Entity Framework Entity Objects

Entity Framework allows you to define your domain model with code. You can either create your entities in code and generate a database from those entities, or, you can generate entities from an existing database.

This documentation assumes you have a basic understanding of Entity Framework. For introductory information please check out Microsoft's documentation:

https://msdn.microsoft.com/en-us/library/ee712907(v=vs.113).aspx

Entity Framework Jump Start

To get a feel for the Entity Framework workflow, check out the topic: Jump Start: Creating Entity Framework Business Classs - C#

Primary Keys

Entity Framework requires that every table have a primary key!


The [Key] annotation marks the entity property that represents the primary key. For example:

[Key]
public int OrderID { getset; }

It's an Entity Framework convention to name your primary key column ID (not case-sensitive), or a combination of the class name and ID (for example, OrderID). When using this convention, it’s not required that you mark the entity property with a [Key] annotation, however, MM .NET code generation always adds the [Key] annotation for the sake of consistency.

If a primary key is a composite key (comprised of multiple columns), each column is marked with the [Key] attribute, with a corresponding Column(Order = n) that defines the order of the key properties. For example:

[KeyColumn(Order = 1)]
public int OrderID { getset; }
 
[KeyColumn(Order = 2)]
public int ProductID { getset; }

By default, Code First names your primary key's index using the convention PK_TableName. So, for example, a table named Orders would automatically have a primary key index named PK_Orders.

If you have a primary key index that does not follow this convention you must specify this using Fluent API in your DbContext's OnModelCreating() method. For example:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
	// Remove the following line of code if you want to generate index on FKs
	modelBuilder.Conventions.Remove<ForeignKeyIndexConvention>();
 
	modelBuilder.Entity<OrderEntity>().HasKey(e => e.OrderID, config => config.HasName("PK_Order_New"));
 
	base.OnModelCreating(modelBuilder);
}

When reverse-engineering a database using the MM .NET BLG this code is generated for you automatically.

In EF Code First, primary keys are clustered by default. If a primary key is not clustered, you must specify this using Fluent API. For example:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
	// Remove the following line of code if you want to generate index on FKs
	modelBuilder.Conventions.Remove<ForeignKeyIndexConvention>();
 
	modelBuilder.Entity<RegionEntity>().HasKey(e => e.RegionID, config => config.IsClustered(false));
 
	base.OnModelCreating(modelBuilder);
}

Foreign Keys

For tables with foreign keys, MM .NET's Business Layer Generator generates an EF Code First [ForeignKey("NavigationProperty")] attribute and an associated navigation property that points to the associated entity.

  • The navigation property is named the same as the table the foreign key points to.

  • The relationship property is marked virtual so it is lazy loaded.

For example:

[Table("Orders")]
public partial class OrderEntity : ABusinessEntity
{
	[Key]
	public int OrderID { getset; }
 
	[ForeignKey("Customer")]
	public string CustomerID { getset; }
	
	// Relationships
	public virtual CustomerEntity Customer { getset; }
}

The database foreign key generated by EF Code First when forward engineering a database from your entity model is named FK_TableNameFrom_TableNameTo_ForeignKeyName, where TableNameFrom is the table containing the foreign key, and TableNameTo is the table being pointed to and ForeignKeyName is the name of the foreign key.

In Entity Framework 6.x, you can't rename the default foreign key, but you can in Entity Framework Core (EF 7).

Required Entity Properties

The MM .NET BLG generates a [Required] annotation on properties marked as Required in the Business Layer Generator. By default, table columns declared as non-optional in SQL Server, are Required by default.

Minimum and Maximum String Lengths

The MM .NET BLG generates a [StringLength(n)] annotation for all SQL Server text-based columns where a maximum value is specified (e.g. varchar(10)). If a minimum text value is specified for a column, this is also included in the annotation. For example:

[StringLength(10, MinimumLength = 5)]
public string PostalCode { getset; }

Column Types

MM .NET's BLG generates a [Column(TypeName = "<type>")] annotation for column data types that need additional specification, such as:

  • ntext
  • text
  • varchar
  • nchar
  • image

This allows Code First to generate the correct data table column type for the associated entity property.

Default Values

Entity Framework Code First 6.x doesn’t handle default value constraints. Entity Framework 7 Core allows declaring them with Fluent API, but not annotations.

MM .NET handles default values by adding them to the business object’s HookSetDefaultValues() method.



© (c) 2026 Oak Leaf Enterprises, Inc., 1996-2026 • Updated: 09/02/20
Comment or report problem with topic