LinqConnect Documentation
In This Topic
    ClassConfiguration and ClassConfiguration<TClass> Classes
    In This Topic

    These classes are the abstract base classes for EntityConfiguration and EntityConfiguration<TClass> classes. They are intended for configuring the property mapping.

    Configuring Properties

    Generic ClassConfiguration<TClass> has 29 Property method overloads, accepting different LINQ expressions for identifying the specific class property. Mapping can be configured both for the primitive properties that belong to the class directly and for properties, belonging to the complex type property of the entity.

    The reflection-based ClassConfiguration class provides a set of methods to get a PropertyConfiguration or one of its descendants:

    The Property method can create mapping configurations (or get the existing one, if there is a configuration already for this property) for properties of any type. type-specific methods can create only the properties of specific types; property type is checked when using these methods.

    Methods, accepting the string memberName can be used for getting configuration of both primitive properties that belong to the class directly and for properties, belonging to the complex type property of the entity.

    Methods, accepting the single MemberInfo parameter can be used only for primitive property that belong to the class directly.

    Methods, accepting the MemberInfo collection parameter and the complexTypePropertyInfo parameter can be used only for properties, belonging to the complex type property of the entity.

    The example of getting a mapping configuration for a primitive property:

        builder.Entity<Company>()
            .Property(company => company.CompanyName)
                .ConcurrencyCheck(UpdateCheck.Always);
     
        builder.Entity(typeof(Company))
            .Property("CompanyName")
                .ConcurrencyCheck(UpdateCheck.Always);
     
        Type type = typeof(Company);
        PropertyInfo property = type.GetProperty("CompanyName");
        builder.Entity(type)
            .Property(property)
                .ConcurrencyCheck(UpdateCheck.Always);

    The example of getting a mapping configuration for a property, belonging to the complex type property of the entity:

        builder.Entity<Company>()
            .Property(company => company.Address.City)
                .MaxLength(100);
     
        builder.Entity(typeof(Company))
            .StringProperty("Address.City")
                .MaxLength(100);
     
        Type type = typeof(Company);
        PropertyInfo complexTypeProperty = type.GetProperty("Address");
        PropertyInfo primitiveProperty = complexTypeProperty.PropertyType.GetProperty("City");
        builder.Entity(type)
            .StringProperty(new MemberInfo[] { complexTypeProperty, primitiveProperty })
                .MaxLength(100);

    If a complex type is used in more than one entity, it is better to define mapping for its properties just once, directly for the complex type itself. In this case you don't need to define complex type properties for each entity, having this complex type property.

        builder.ComplexType<Address>()
            .Property(address => address.City)
                .MaxLength(100)
                .ColumnName("City");

    If different entities have the property of the same complex type and the columns for the properties of the complex type property have different names in different entities, then it is necessary to remap this property for each entity and to specify the column name.

        builder.ComplexType<Address>()
            .Property(address => address.City)
                .MaxLength(100);
     
        builder.Entity<Company>()
            .Property(company => company.Address.City)
                 .ColumnName("OFFICE_CITY"); 
     
        builder.Entity<Contact>()
            .Map(contact => new { HOME_CITY = contact.Address.City });

    There is no point in getting an entity property of a complex type through the Property method, and, besides, it is impossible through the generic ClassConfiguration<TClass> interface because the interface has no corresponding overload of the Property method. The mapping of this property cannot be configured because it has no representation in the database and it is only its primitive properties that are mapped. You also don't need to get the mapping configuration of a navigation property.

    Adding Properties to Ignore List

    ClassConfiguration classes also have the following methods for adding properties to the ignore list:

    You may need to add a property to ignore list, for example, to avoid automatic mapping generation for the property, which value is application-generated and should not be persisted to the database.

    You can add a primitive property, a primitive property that belongs to a complex type, or a navigation property to the ignore list. If a navigation property is exclude from the mapping configuration generation, the whole association, which is related to this navigation property, is excluded from mapping.

        builder.Entity<Company>()
            .Ignore(p =< p.CompanyName);
     
        builder.Entity(typeof(Company))
            .Ignore("CompanyName");
     
        Type type = typeof(Company);
        PropertyInfo property = companyType.GetProperty("CompanyName");
        builder.Entity(type)
            .Ignore(property);
     
        var properties = type.GetProperties().Where(p => p.Name.Contains("Office"));
        builder.Entity(type)
            .Ignore(properties);