How can I get a computed property without extending the associated navigation properties?

I followed some great advice here ( Handling calculated properties using breezejs and web api ) to allow Breeze to access my calculated properties, which I set in a partial class on the server side:

public partial class EventPerson { [NotMapped] public Decimal TotalAmountPaid { get { return this.EventPersonPayments.Sum(p => p.AmtPaid); } } } 

But for each EventPerson event received, this value is displayed as 0 if I do not use server.expand ("EventPersonPayments") clients or .Include ("EventPersonPayments").

I do not want all data in EventPersonPayments to be serialized and sent to the client; all i want is the total value. Is it possible?

EDIT: if my computed property is derived from other properties already existing in the object, it works fine. For instance:

  public partial class EventPerson { [NotMapped] public String DisplayName { get { return this.FirstName + " " + this.LastName; } } } 

returns the value of DisplayName in the JSON payload. The first type of computed property always returns 0 or null, unless I specifically download all the additional information.

I decided to convert them to User Defined Functions in SQL Server, but I did not need to throw out the C # code to make it work as it should.

+6
source share
2 answers

One approach is to use a projection, which includes both the requested objects and some calculated properties. that is, your server request might look like this:

 [HttpGet] public IQueryable<Object> CustomersAndFreightTotals(companyName) { var stuff = ContextProvider.Context.Customers .Where(c => c.CompanyName.StartsWith(companyName)) .Select(c => new { Customer = c, FreightTotal = c.Orders.Sum(o => o.Freight)) }); return stuff; } 

This request will download all your customers that start with the specified company name, but also provide you with a โ€œcommon freightโ€ for all orders for each customer.

You would call it code with this code:

 var query = EntityQuery.from("CustomersAndFreightTotals") .withParameters({ companyName: "C" }); myEntityManager.executeQuery(query).then(function(data) { var results = data.results; results.forEach(function (r) { // note that each customer WILL also be added to the local entityManager // because it is an entity, whereas the freightTotal is only available here. var customer = r.Customer; var freightTotal = r.FreightTotal; // and if you wanted to hack the customer entity // you could do this. customer.freightTotal = freightTotal; }); } 
+3
source

I ran into this problem too, and there are a few more questions / answers that seem to indicate what is happening:

In my opinion, in short, [NotMapped] prevents the correct posting to the Breeze / Entity Framework field. However, Json.NET will serialize the field and send it to Breeze, which will populate the field if you manually configured it through the class constructor, and the data was extracted using the extension for another property that the Entity Framework recognizes. It seems to be an almost random case when you can get [NotMapped] fields to work on the client in this case; The Breeze + Entity Framework does not seem to be designed for this case.

There is an offer on Breeze User Voice that you can vote on and comment on. I'm not sure that Breeze can solve this problem on its own without having any work from the Entity Framework team, but at least this could put the problem on their radar.

+2
source

Source: https://habr.com/ru/post/973560/


All Articles