diff --git a/src/Microsoft.AspNetCore.OData/Query/Expressions/SelectExpandBinder.cs b/src/Microsoft.AspNetCore.OData/Query/Expressions/SelectExpandBinder.cs index e8ac55a2b..9fab4f77a 100644 --- a/src/Microsoft.AspNetCore.OData/Query/Expressions/SelectExpandBinder.cs +++ b/src/Microsoft.AspNetCore.OData/Query/Expressions/SelectExpandBinder.cs @@ -458,7 +458,7 @@ private static Expression RemoveNonStructucalProperties(QueryBinderContext conte { foreach (var sp in structuralProperties) { - if (model.GetClrPropertyName(sp) == prop.Name) + if (prop.CanWrite && model.GetClrPropertyName(sp) == prop.Name) { MemberExpression propertyExpression = Expression.Property(source, prop); bindings.Add(Expression.Bind(prop, propertyExpression)); diff --git a/test/Microsoft.AspNetCore.OData.Tests/Query/Expressions/SelectExpandBinderTest.cs b/test/Microsoft.AspNetCore.OData.Tests/Query/Expressions/SelectExpandBinderTest.cs index e2558465b..dd105644b 100644 --- a/test/Microsoft.AspNetCore.OData.Tests/Query/Expressions/SelectExpandBinderTest.cs +++ b/test/Microsoft.AspNetCore.OData.Tests/Query/Expressions/SelectExpandBinderTest.cs @@ -8,6 +8,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Linq.Expressions; @@ -295,6 +296,10 @@ public void Bind_UsingEFQueryProvider_GeneratedExpression__DoesNot_ContainExpand SelectExpandWrapper innerInnerCustomer = Assert.IsAssignableFrom>(customer); Assert.Null(innerInnerCustomer.Instance.Orders); + Assert.Equal(2, innerInnerCustomer.Instance.TestReadonlyProperty.Count); + Assert.Equal("Test1", innerInnerCustomer.Instance.TestReadonlyProperty[0]); + Assert.Equal("Test2", innerInnerCustomer.Instance.TestReadonlyProperty[1]); + Assert.Equal(2, innerInnerCustomer.Instance.TestReadOnlyWithAttribute); } [Theory] @@ -2228,6 +2233,26 @@ public class QueryCustomer public IList Orders { get; set; } + public List TestReadonlyProperty + { + get { return new List() { "Test1", "Test2" }; } + } + + [ReadOnly(true)] + public int TestReadOnlyWithAttribute + { + get + { + return 2; + } + } + + [ReadOnly(true)] + public int TestReadOnlyWithAttributeAndSetter + { + get; set; + } + public IDictionary CustomerProperties { get; set; } }