Web API 2 OData EDM异常

时间:2014-11-04 03:15:53

标签: odata asp.net-web-api asp.net-web-api2 ado.net-entity-data-model

我有以下代码:

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( );
}

请注意我只是映射了AwardTitle类...而不是AwardEpisode类。

现在,当我浏览控制器时,我希望得到一个关于没有映射AwardEpisode的错误。但是,没有错误。事实上,除了AwardTitle被检索之外... AwardEpisode也正在被检索......没有任何明确的调用。

这怎么可能?这有可能吗??

我在Windows 7上使用ASP.Net Web API。

1 个答案:

答案 0 :(得分:1)

ODataConventionModelBuilder默认为T api调用映射.EntitySet<TEntityType>(string name)类型的原始,复杂和导航属性。 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

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);

要更改默认行为,我们可以忽略该属性:

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

return builder.GetEdmModel();