How to handle computed/read-only properties? #1544
Unanswered
DanielVernall
asked this question in
Q&A
Replies: 2 comments 1 reply
-
|
From a protocol perspective, generated properties can be annotated with a Computed annotation, and read-only properties can be annotated with Immutable. |
Beta Was this translation helpful? Give feedback.
1 reply
-
|
From OData model builder, you can use the fluent api to specify the annotation: var builder = new ODataConventionModelBuilder();
var customer = builder.EntityType<Customer>();
customer.Property(c => c.FullName).HasComputed().IsComputed(true);
customer.Property(c => c.Salary).HasImmutable().IsImmutable(true);
var model = builder.GetEdmModel();
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Salary { get; set; }
// Computed property
public string FullName
{
get { return $"{FirstName} {LastName}"; }
}
}If you serialize the model to csdl, you can get: <?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="Default" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityType Name="Customer">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="FullName" Type="Edm.String" />
<Property Name="Salary" Type="Edm.String" />
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<Property Name="FirstName" Type="Edm.String" />
<Property Name="LastName" Type="Edm.String" />
</EntityType>
<EntityContainer Name="Container" />
<Annotations Target="Default.Customer/FullName">
<Annotation Term="Org.OData.Core.V1.Computed" Bool="true" />
</Annotations>
<Annotations Target="Default.Customer/Salary">
<Annotation Term="Org.OData.Core.V1.Immutable" Bool="true" />
</Annotations>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Be noted, ASP.NET Core OData or OData.net library doesn't consume these annotations to validate the POST. It means developer should take the business logic to consume these annotations. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, how do I mark a property (primitive or navigation property) as readonly, so it is returned in GET queries but, rejected in POST/PATCH queries?
For example, I have a generated key for one of my entities. So, I want to reject POST requests which pass a key, and I want to generate it on the backend. The $metadata should give some indication that the property is readonly.
I have tried creating private setters in my entity class, and I've tried setting
AddedExplicitlytofalsein thePropertyConfiguration. But this doesn't seem to have any effect, I'm still able to POST the key and the metadata looks the same.Beta Was this translation helpful? Give feedback.
All reactions