EntityKey is not defined error - breeze

I develop an asp.net mvc solution with breeze/durandal.
I would like to execute the following code:
var entityKey = new EntityKey(entityNames.transport, transportId);
var query = EntityQuery.fromEntityKey(entityKey).expand("Sender.Country");
And I got the following error:
EntityKey is not defined
I don't know why.
I can execute code below:
var query = EntityQuery.from('Transports')
.where('id', 'eq', transportId)
.expand('Sender.Country')
.orderBy(orderBy.transport);
return manager.executeQuery(query)
.then(fetchSucceeded)
.fail(queryFailed);
So breeze seems correctly installed.
Any idea?
Thanks.

It should be breeze.EntityKey, but to make it easier to use we often assign it to a shorter name before using it. i.e.
var EntityKey = breeze.EntityKey;
You've obviously done something similar for EntityQuery.

Related

Is it possible for Handlebars.Net to report errors/warnings for missing properties?

Imagine that I have a template that requests {{Foo}} but that property does not exist on the given model. Can Handlebars.Net be configured to report this problem to me? To me it looks like it just silently inserts an empty string and proceeds.
The answer is yes:
var config = new HandlebarsConfiguration()
{
ThrowOnUnresolvedBindingExpression = true,
};
var handlebars = HandlebarsDotNet.Handlebars.Create(config);
var template = handlebars.Compile("Hello {{MissingProperty}}");
template(new object());
This will throw an exception telling you that MissingProperty was missing.
There is also the UnresolvedBindingFormatter which can be used if you don't want to throw an exception.
Related: The discussion on https://github.com/Handlebars-Net/Handlebars.Net/issues/143

Can't get media image URL from Umbraco after upgrade

I have recently upgraded to 7.6.3 and I am having an issue retrieving media image URLs in my views. For testing purpposes, I added a new MediaPicker2 property for one of the nodes, set up a value for it and tried to get its value in my razor view:
var icon2 = Model.Content.GetProperty("icon2");
The object then looks like this:
Executing Model.Content.GetPropertyValue("icon2") throws the following error:
An exception of type 'System.Exception' occurred in Umbraco.Core.dll but was not handled in user code
Additional information: Exception while creating a value.
InnerException: "Object reference not set to an instance of an object."
Model.Content.GetPropertyValue<IPublishedContent>("icon2") throws the same error.
What am I doing wrong?
According to this answer, what you're doing is correct. However, I'm not sure why it's not working.
However, I have answered a similar question to this before, and I found a roundabout way of getting the node for a Udi:
You can get an IPublishedContent from your image string using this code:
// Your string.
var imageString = Model.Content.GetPropertyValue<string>("icon2");
// Get the guid from this string.
var imageGuidUdi = GuidUdi.Parse(imageString);
// Get the ID of the node!
var imageNodeId = ApplicationContext.Current.Services.EntityService.GetIdForKey(guidUdi.Guid, (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), guidUdi.EntityType, true));
// Finally, get the node.
var imageNode = Umbraco.TypedMedia(imageNodeId.Result);

fixupKeys() function in breeze.debug.js

function fixupKeys(em, keyMappings) {
keyMappings.forEach(function (km) {
var group = em._entityGroupMap[km.entityTypeName];
group._fixupKey(km.tempValue, km.realValue);
});
}
For one of my entities, I am seeing group return undefined, and so throws an error with group._fixupKey().
It looks to me that the km.entityTypeName stores the short name of the entity, but the em._entityGroupMap stores a concatenated name of the entity with it's the namespace.
Anyone know if this a breezejs bug? Or am I doing something wrong in my model that is responsible for this mismatched key?
It was an issue with the implementation of my custom breeze dataservice adapter. Thanks for looking at it with me.

What is the proper syntax to chain multiple queries together?

I'm using the HotTowel SPA template which makes use of Durandal. In my Durandal ViewModels I am using Breeze to get some data from the database.
I have a datacontext class that I put all my breeze queries in and the queries all follow the pattern like the following:
getAthletes: function (queryCompleted) {
var query = breeze.EntityQuery.from("Athletes");
return manager
.executeQuery(query)
.then(queryCompleted)
.fail(queryFailed)
}
Since I'm doing an asynchronous call in the activate method of the view model, I have to return the promise that comes back from these calls in the activate method.
Using a single query works great like this:
function activate() {
datacontext.getAthlete(loadAthlete);
}
However, if I need to perform two queries I run into problems, but only in the release version of my application. I have tried doing this with the following syntax:
function activate() {
datacontext.getAthlete(loadAthlete).then(datacontext.getOtherData(loadOtherData));
}
This will work fine in debug mode, but when I deploy it out to the server and my scripts get bundled, I get an exception which isn't very clear.
t is not a function
I've also tried chaining them together in my datacontext class like below, but I still get the same error.
getAthleteAndEfforts: function (athleteId, athleteQueryCompleted, effortsQueryCompleted) {
var athleteQuery = breeze.EntityQuery.from("Athletes").where("id", "==", athleteId);
var effortsQuery = breeze.EntityQuery.from("BestEfforts").where("athleteId", "==", athleteId);
return manager.executeQuery(athleteQuery).then(athleteQueryCompleted)
.then(manager.executeQuery(effortsQuery).then(effortsQueryCompleted))
.fail(queryFailed);
}
So I'm assuming I just don't understand the Q.defer() enough to use it properly or there is something else going on.
What is the correct syntax to accomplish this?
Ok, thanks to RainerAtSpirit for pointing me in the right direction to find this. I looked at John Papa's jumpstarter examples and he has a datacontext that does this under the primeData function.
So using the syntax he used there I was able to get it to work correctly like this:
getAthleteAndEfforts: function (athleteId, athleteQueryCompleted, effortsQueryCompleted) {
return Q.all([
datacontext.getAthlete(athleteId, athleteQueryCompleted),
datacontext.getAthleteEfforts(athleteId, effortsQueryCompleted)]);
}
I had seen the Q.all in the Q documentation but wasn't sure how to use it, but this example helped. I tested this and it works both in debug and release modes.
Not sure why the first version is working at all, but you'd return a promise when datacontext is making async calls.
function activate() {
return datacontext.getAthlete(loadAthlete);
}
or
function activate() {
return datacontext.getAthlete(loadAthlete).then( return datacontext.getOtherData(loadOtherData));
}
Check #John Papa's jumpstarter for more examples: https://github.com/johnpapa/PluralsightSpaJumpStartFinal/search?q=activate

Getting only what is needed with Entity Framework

With a plain connection to SQL Server, you can specify what columns to return in a simple SELECT statement.
With EF:
Dim who = context.Doctors.Find(3) ' Primary key is an integer
The above returns all data that entity has... BUT... I would only like to do what you can with SQL and get only what I need.
Doing this:
Dim who= (From d In contect.Doctors
Where d.Regeneration = 3
Select New Doctor With {.Actor = d.Actor}).Single
Gives me this error:
The entity or complex type XXXXX cannot be constructed in a LINQ to Entities query.
So... How do I return only selected data from only one entity?
Basically, I'm not sure why, but Linq can't create the complex type. It would work if you were creating a anonymous type like (sorry c# code)
var who = (from x in contect.Doctors
where x.Regeneration == 3
select new { Actor = x.Actor }).Single();
you can then go
var doctor = new Doctor() {
Actor = who.Actor
};
but it can't build it as a strongly typed or complex type like you're trying to do with
var who = (from x in contect.Doctors
where x.Regeneration == 3
select new Doctor { Actor = x.Actor }).Single();
also you may want to be careful with the use of single, if there is no doctor with the regeneration number or there are more than one it will throw a exception, singleordefault is safer but it will throw a exception if there is more than one match. First or Firstordefault are much better options First will throw a exception only if none exist and Firstordefault can handle pretty much anything
The best way to do this is by setting the wanted properties in ViewModel "or DTO if you're dealing with upper levels"
Then as your example the ViewModel will be:
public class DoctorViewModel{
public string Actor {get;set;}
// You can add as many properties as you want
}
then the query will be:
var who = (From d In contect.Doctors
Where d.Regeneration = 3
Select New DoctorViewModel {Actor = d.Actor}).Single();
Sorry i wrote the code with C# but i think the idea is clear :)
You can just simply do this:
Dim who= (From d In contect.Doctors
Where d.Regeneration = 3
Select d.Actor).Single
Try this
Dim who = contect.Doctors.SingleOrDefault(Function(d) d.Regeneration = 3).Actor

Resources