Model First Specifics

This topic lists the specifics of using the Entity Framework data model created within the Entity Framework Designer (the Model First approach) in XAF. If you create your entities in code (Code First), skip this topic.

  1. In Visual Studio 2012/2013, the T4 Code Generation Strategy is used by default, and collection properties (navigation properties) are initialized by the HashSet. However, in XAF, List View data sources should support the IList interface. Thus, it is necessary to modify the designer's template (the *.tt file) in order to use List instead of HashSet.

    Default template:

    this.<#=code.Escape(navigationProperty)#> = 
    new HashSet<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
    

    Required template:

    this.<#=code.Escape(navigationProperty)#> = 
    new List<<#=typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType())#>>();
    
  2. In the Model First approach, a constructor with the connectionString parameter is required in the DbContext descendant when you specify the connection via the XafApplication.ConnectionString property or via the ConnectionString attribute in the configuration file (see Use the Entity Framework Data Model). Add this constructor by creating a partial class that supplements your DbContext/ObjectContext descendant.

    public partial class MySolutionModelContainer : DbContext {
        public MySolutionModelContainer(string connectionString) : base(connectionString) { }
    }
    
  3. When using Model First, the metadata part of the connection string specified in the configuration file (or directly assigned to XafApplication.ConnectionString) is required. You can copy the connection string from the App.Config file generated by the Entity Framework Designer.
  4. To be able to apply attributes to entity classes, add partial classes that supplement the designer-generated code. To apply attributes to entity properties, use the approach described in the How to: Apply Attributes to Entity Properties when Using Model First topic.

See Also