Web API 2 OData EDM Anomaly

I have the following code:

public class AwardTitle
{
    public int AwardTitleId
    {
        get;
        set;
    }

    public int? EpisodeId
    {
        get;
        set;
    }

    public virtual AwardEpisode Episode
    {
        get;
        set;
    }
}

public class AwardEpisode
{
    public int EpisodeId
    {
        get;
        set;
    }
}

public static class WebApiConfig
{
    public static void Register( HttpConfiguration config )
    {
        config.Routes.MapODataRoute( "ODataRoute", "api", GetImplicitEDM( ) );
    }
}

private static Microsoft.Data.Edm.IEdmModel GetImplicitEDM( )
{
    var builder = new ODataConventionModelBuilder( );
    builder.EntitySet<AwardTitle>( "AwardTitles" );

    return builder.GetEdmModel( );
}

      

Note that I only mapped a class AwardTitle

... not a class AwardEpisode

.

Now when I go to the controller, I would expect to get a no display message AwardEpisode

. However, there are no errors. In fact, in addition to AwardTitle

which is retrieved ... is AwardEpisode

also retrieved ... without any explicit calls to do so.

How is this possible ??? If possible?

I am using ASP.Net Web API 2 on Windows 7.

+3


source to share


1 answer


ODataConventionModelBuilder maps primitive, complex and navigation type properties T

to .EntitySet<TEntityType>(string name)

api invocation by default . code :

private void MapEntityType(EntityTypeConfiguration entity)
{
    IEnumerable<PropertyInfo> properties = ConventionsHelpers.GetProperties(entity, includeReadOnly: _isQueryCompositionMode);
    foreach (PropertyInfo property in properties)
    {
        bool isCollection;
        StructuralTypeConfiguration mappedType;

        PropertyKind propertyKind = GetPropertyType(property, out isCollection, out mappedType);

        if (propertyKind == PropertyKind.Primitive || propertyKind == PropertyKind.Complex)
        {
            MapStructuralProperty(entity, property, propertyKind, isCollection);
        }
        else
        {
            // don't add this property if the user has already added it.
            if (!entity.NavigationProperties.Where(p => p.Name == property.Name).Any())
            {
                NavigationPropertyConfiguration addedNavigationProperty;
                if (!isCollection)
                {
                    addedNavigationProperty = entity.AddNavigationProperty(property, EdmMultiplicity.ZeroOrOne);
                }
                else
                {
                    addedNavigationProperty = entity.AddNavigationProperty(property, EdmMultiplicity.Many);
                }

                addedNavigationProperty.AddedExplicitly = false;
            }
        }
    }

      

example test case:



public void ModelBuilder_Products()
{
    var modelBuilder = new ODataConventionModelBuilder();
    modelBuilder.EntitySet<Product>("Products");

    var model = modelBuilder.GetEdmModel();

    var product = model.AssertHasEntitySet(entitySetName: "Products", mappedEntityClrType: typeof(Product));
    product.AssertHasPrimitiveProperty(model, "ReleaseDate", EdmPrimitiveTypeKind.DateTime, isNullable: true);
    product.AssertHasComplexProperty(model, "Version", typeof(ProductVersion), isNullable: true);
    product.AssertHasNavigationProperty(model, "Category", typeof(Category), isNullable: true, multiplicity: EdmMultiplicity.ZeroOrOne);

      

To change the default behavior, we can ignore the property:

var builder = new ODataConventionModelBuilder();
builder.Entity<AwardTitle>().Ignore(a => a.Episode);
builder.EntitySet<AwardTitle>("AwardTitles");

return builder.GetEdmModel();

      

+1


source







All Articles