BreezeJS SaveChanges - undefined is not a function - c#

I'm very new to BreezeJS and i'm doing something wrong, but not sure what. I am using a 3rd party API for my GET requests and using my own server backend to process the SaveChanges to fire off each request individually to the 3rd party as I can't customize the post/put requests to the exact syntax and post data format I need.
Our Model is dynamic (meaning customers can add new attributes/fields which then flow through from the rest api to our client) so that's why the code looks like it does below, this is the controller:
[HttpPost]
public SaveResult SaveChanges(JObject saveBundle)
{
var context = JsonConvert.DeserializeObject<List<dynamic>>(saveBundle.SelectToken("entities").ToString());
foreach (var entity in context)
{
foreach (JProperty obj in entity)
{
if (obj != null)
{
// nothing right now but in future persist somehow
}
}
}
// Construct the save result to inform the client that the server has completed the save operation
var keyMappings = new List<KeyMapping>();
return new SaveResult()
{
Entities = context.Cast<object>().ToList(),
Errors = null,
KeyMappings = keyMappings
};
}
Call stack looks like such:
TypeError: undefined is not a function
at http://localhost:5749/Scripts/breeze.debug.js:14114:51
at http://localhost:5749/Scripts/breeze.debug.js:235:26
at Array.map (native)
at __map (http://localhost:5749/Scripts/breeze.debug.js:234:15)
at proto.visitAndMerge (http://localhost:5749/Scripts/breeze.debug.js:14111:16)
at http://localhost:5749/Scripts/breeze.debug.js:12806:48
at __using (http://localhost:5749/Scripts/breeze.debug.js:395:16)
at Object.processSavedEntities (http://localhost:5749/Scripts/breeze.debug.js:12794:13)
at saveSuccess (http://localhost:5749/Scripts/breeze.debug.js:12776:67)
at deferred.promise.then.wrappedCallback (http://localhost:5749/Scripts/angular.js:11046:81) undefined
I traced it to this line in proto.visitAndMerge (line 14114 in breeze.debug.js):
if (node.entityAspect.entityState.isDeleted()) {
If you think I'm doing something idiotic I'm all ears too. The third party API can be modified to do a GET accordingly but there is nothing to handle the SaveChanges bundle so as far as I know this is what I need to do.
Any advice would be great.
For reference, I was trying to follow this pattern: Breezejs SaveChanges: is it possible to return a custom SaveResult object, somehow?

I figured it out. entityAspect is sent along with the request. So converting it to a dynamic object created an entityAspect property which I then sent back to the client. This entityAspect needed to be removed so that it would not interfere with the JavaScript object.
Here's some code, maybe it helps someone someday:
var entities = JsonConvert.DeserializeObject<List<dynamic>>(saveBundle.SelectToken("entities").ToString());
foreach (var entity in entities)
{
JObject objEntityAspect = entity["entityAspect"];
JToken objEntityState = objEntityAspect["entityState"];
if (objEntityState.Value<string>() == "Modified")
{
// make a post with the instance id
}
entity.Remove("entityAspect");
}
entity.Remove("entityAspect") was the key component needed.

Related

Strange anomoly with: Attaching an entity of type 'X' failed because another entity of the same type already has the same primary key value

I had a similar issue with this question, when updating a record using EF6.
I really thought I had cracked the whole updating thing, but now have to almost identical functions updating in what I think was an identical way. One works, the other doesn't. I have fixed the one that doesn't work by using Jamie's comment in the above question, but I'd like to understand if the function that works, really shouldn't and so is on borrowed time and I should make more like the 'fixed' one. Or, why the 'fixed' one didn't work in the first place. I even moved them into the same controller so that the database (DB) context was guaranteed the same. Have I missed something and they are not identical (functionally) at all?
It might also help some others out there that struggle with this as I did.
The function that works (cut down) is:
[HttpPost]
[Route("UpdateAddBusinessService")]
public async Task<IHttpActionResult> UpdateAddBusinessService(BusinessServiceDTO servicetoupdateoradd)
{
... pre check stuff...
try
{
if (servicetoupdateoradd.Id != null) // This is an existing service to be updated - if Is Null then create new
{
BusinessService businessService = await db.BusinessServices.FindAsync(servicetoupdateoradd.Id);
if (businessService != null)
{
Mapper.Map(servicetoupdateoradd, businessService);
db.Entry(businessService).State = EntityState.Modified;
await db.SaveChangesAsync();
return Ok("Service Updated");
}
else
The function that doesn't work is:
[HttpPost]
[Route("UpdateImage")]
public async Task<IHttpActionResult> UpdateImage(ImageDTO imageDTO)
{
... pre check stuff ...
try
{
// First find the image
// Image imagetoupdate = await db.Images.FindAsync(imageDTO.Id); <<-This FAILS.
Image imagetoupdate = db.Images.AsNoTracking().Single(x => x.Id == imageDTO.Id); <<- This WORKS
if (imagetoupdate != null)
{
imagetoupdate = Mapper.Map<ImageDTO, Image>(imageDTO); // Move the stuff over..
db.Entry(imagetoupdate).State = EntityState.Modified;
await db.SaveChangesAsync();
return Ok();
}
I wondered (as you will no doubt), if my Mapper function was doing anything, but I suspect not (without digging too deep, but I guess it could be), my Mapper.Config functions for the two DTO's are very similar:
cfg.CreateMap<Image, ImageDTO>();
cfg.CreateMap<ImageDTO, Image>();
and:
cfg.CreateMap<BusinessService, BusinessServiceDTO>();
cfg.CreateMap<BusinessServiceDTO, BusinessService>();
I would really just like to understand the 'correct' way of doing this so it doesn't bite me again. Thanks in advance.
EDIT: I was asked (quite reasonably) if the 'pre-check stuff' does anything to fetch the data, it doesn't, but there is a subtle difference, that I might have missed...
This is from the BusinessService function that works:
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
string userid = User.Identity.GetUserId(); //Check user is valid
if (servicetoupdateoradd.UserId != userid)
{
var message = "User Id Not found - Contact support";
HttpResponseMessage err = new HttpResponseMessage() { StatusCode = HttpStatusCode.ExpectationFailed, ReasonPhrase = message };
return ResponseMessage(err);
}
This is from the UpdateImage function that didn't work:
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
string userid = User.Identity.GetUserId();
SGGUser user = db.Users.Find(userid); // Use this find and not UserManager becuase its a different context and buggers up the file save
if (user == null)
{
var message = "User Id Not found - Contact support";
HttpResponseMessage err = new HttpResponseMessage() { StatusCode = HttpStatusCode.ExpectationFailed, ReasonPhrase = message };
return ResponseMessage(err);
}
I see that in this one, though I don't fetch the relevant data, I do use the 'db' context.. could that be it?? The Image object does contain a reference to the user, so maybe that does some magic in the background code?
Appologies, I just didn't want to clutter up the question too much...
This line:
Mapper.Map(servicetoupdateoradd, businessService);
and this line:
imagetoupdate = Mapper.Map<ImageDTO, Image>(imageDTO); // Move the stuff over..
look similar, but do two different things.
The first line will tell Automapper to copy values from the first object over to the second object reference using the mapping rules.
The second line will tell Automapper to make a completely new reference to the entity with the mapped over values from the provided object and return it.
So in the first case, the entity reference is preserved to the one the DbContext knows about. The reference was loaded from a DbContext and should be tracking changes so you shouldn't even need to set it's entity state. In the second case, Automapper is creating an entirely new reference and assigning it over top the original reference. EF is treating that as a completely new instance and trying to attach it, resulting in it complaining because the context had already loaded that entity, you just overwrote the reference.
It should work if you change the second instance to:
Mapper.Map(imageDTO, imagetoupdate);

I have to update SQL value using HttpPost in dotnet core 2.0

I have an external API resource that I need to use. The API sends me trigger as a post request. What I need to do is get the values, and update the current corresponded record in the SQL. The code that I came up with keeps adding a new value to the database.
[HttpPost("status")]
public async Task<IActionResult> Post(CompletionForCreateDto
completionForCreateDto)
{
// che ck the model state
if (!ModelState.IsValid)
return BadRequest(ModelState);
// find the incoming trigger information and map them to the disctionary
var form = Request.ReadFormAsync();
var formData = form.Result;
Dictionary<string, string> ddata = new Dictionary<string, string>();
foreach ( var key in formData.Keys)
{
var value = formData[key.ToString()];
ddata.Add(key, value);
}
// find the existed completion by ID
int id = Int32.Parse(ddata["record"]);
var completion = await repository.GetCompletion(id);
completion = mapper.Map<CompletionForCreateDto, Completion>(completionForCreateDto);
if (completion == null)
return NotFound();
// some test values to make sure it works.
completion.VitalSignBLA = ddata["vital_signs_spring_complete"];
completion.VitalSignBLADateTime = DateTime.Now;
// repository.Add(completion); ==> adds value as a new record
// repository.Update(completion);==> adds value as a new record
repository.Attach(completion); // ==> adds value as a new record
// SaveChanges()
await unitOfWork.CompleteAsync();
return Ok();
}
Above, first I get the trigger and put them into a dicmtionary so that I can use. Then, I tried to find the existed trigger record in my sql servere using their record value. Trigger's record and SQL record value are the same values. After that, I use some data to test it. Lastly, I used context Add, Update and Attach to update the SQL database. For all cases, the post request is kept adding a new row.
I am new to the dotnet core. I am not sure it this because I used HttpPost attribute or the code itself. Any help wpuld be appriciated!
The most probable cause for EF generating an insert statement for an entity when attaching it to a context (Through Attach, Update, Add or by setting its State) is missing values from the PK (most commonly the ID, but the PK can be more complex in other cases) for that entity.
When no PK is present, EF cannot know which entity you are referring to, so it sets the state to New. In your code, I suspect this is generating your issues:
var completion = await repository.GetCompletion(id);
completion = mapper.Map<CompletionForCreateDto, Completion>(completionForCreateDto);
You are getting some data from the repository, but then generating a new entity through mapper.Map(completionForCreateDto);. I suspect that this code is not setting the PK values, causing your issues. A naive fix would be to set these values after mapper.map. That, or update the mappings to map the values.

Nopcommerce Update entity issue

Using NopCommerce 3.8, Visual Studio 2015 proff.
I have created a plugin that is responsible for making restful calls to my Web API that exposes a different DB to that of Nop.
The process is run via a nop Task, it successfully pulls the data back and i can step through and manipulate as i see fit, no issues so far.
Issue comes when i try to update a record on the product table, i perform the update... but nothing happens no change, no error.
I believe this is due to the Context having no idea about my newly instantiated product object, however I'm drawing a blank on what i need to do in relation to my particular example.
Similar questions usually reference a "model" object that is part of the parameter of the method call, "model" has the method ToEntity which seems to be the answer in similar question in stack.
However my example doesn't have the ToEntity class/method possibly because my parameter is actually a list of products. To Clarify here my code.
Method in RestClient.cs
public async Task<List<T>> GetAsync()
{
try
{
var httpClient = new HttpClient();
var json = await httpClient.GetStringAsync(ApiControllerURL);
var taskModels = JsonConvert.DeserializeObject<List<T>>(json);
return taskModels;
}
catch (Exception e)
{
return null;
}
}
Method in my Service Class
public async Task<List<MWProduct>> GetProductsAsync()
{
RestClient<MWProduct> restClient = new RestClient<MWProduct>(ApiConst.Products);
var productsList = await restClient.GetAsync();
InsertSyncProd(productsList.Select(x => x).ToList());
return productsList;
}
private void InsertSyncProd(List<MWProduct> inserted)
{
var model = inserted.Select(x =>
{
switch (x.AD_Action)
{
case "I":
//_productService.InsertProduct(row);
break;
case "U":
UpdateSyncProd(inserted);
.....
Then the method to bind and update
private void UpdateSyncProd(List<MWProduct> inserted)
{
var me = inserted.Select(x =>
{
var productEnt = _productRepos.Table.FirstOrDefault(ent => ent.Sku == x.Sku.ToString());
if(productEnt != null)
{
productEnt.Sku = x.Sku.ToString();
productEnt.ShortDescription = x.ShortDescription;
productEnt.FullDescription = x.FullDescription;
productEnt.Name = x.Name;
productEnt.Height = x.Pd_height != null ? Convert.ToDecimal(x.Pd_height) : 0;
productEnt.Width = x.Pd_width != null ? Convert.ToDecimal(x.Pd_width) : 0;
productEnt.Length = x.Pd_depth != null ? Convert.ToDecimal(x.Pd_depth) : 0;
productEnt.UpdatedOnUtc = DateTime.UtcNow;
}
//TODO: set to entity so context nows and can update
_productService.UpdateProduct(productEnt);
return productEnt;
});
}
So as you can see, I get the data and pass data through to certain method based on a result. From that list in the method I iterate over, and pull the the entity from the table, then update via the product service using that manipulated entity.
So what am I missing here, I'm sure its 1 step, and i think it may be either be because 1) The context still has no idea about the entity in question, or 2) Its Incorrect calls.
Summary
Update is not updating, possibly due to context having no knowledge OR my methodology is wrong. (probably both).
UPDATE:
I added some logger.inertlog all around my service, it runs through fine, all to the point of the call of update. But again I check the product and nothing has changed in the admin section.
plugin
I have provided the full source as i think maybe this has something to do with the rest of the code setup possibly?
UPDATE:
Added the following for testin on my execute method.
var myprod = _productRepos.GetById(4852);
myprod.ShortDescription = "db test";
productRepos.Update(myprod);
This successfully updates the product description. I moved my methods from my service into the task class but still no luck. The more i look at it the more im thinking that my async is killing off the db context somehow.
Turned of async and bound the getbyid to a new product, also removed the lambda for the switch and changed it to a foreach loop. Seems to finally update the results.
Cannot confirm if async is the culprit, currently the web api seems to be returning the same result even though the data has changed (some wierd caching by deafult in .net core? ) so im creating a new question for that.
UPDATE: It appears that the issue stems from poor debugging of async. Each instance I am trying to iterate over an await call, simply put im trying to iterate over a collection that technically may or may not be completed yet. And probably due to poor debugging, I was not aware.
So answer await your collection Then iterate after.

Entity Framework 6.1.3, 2 Web Applications 1 SQL Database, Browser Cache Issues

The Problem:
No matter how sure I am that my transactions are committed and the application can read the absolute latest from the database, sometimes when I refresh the application the changes aren't displaying the latest data and I suspect that the data is being cached in the browser! This suspicion comes from loading the web application in another browser after the changes are made and seeing the changes. I need to make it so that every time the page is refreshed, this data is not cached in the browser.
Setup:
One Web Application simply reads from the database, while AJAX calls are made client side to a REST API that adds, deletes, and updates the data.
I have been doing a lot of research and the most valuable resource I have found on this was here: http://mehdi.me/ambient-dbcontext-in-ef6/
My code that reads from the database uses this pattern:
public IEnumerable<Link> GetLinks(){
using (var context = new MyContext()){
foreach(var link in context.ChangeTracker.Entries())
{
link.Reload();
}
return context.Links.Where(x => x.UserId == this.UserId).ToList();
}
}
An example of one of my operations that reads follows this pattern:
public int AddLink(string text, string url)
{
using (var context = new MyContext())
{
Link linkresult;
using (var contextTransaction = context.Database.BeginTransaction())
{
var link = new Link()
{
Text = text,
Url = url
UserId = this.UserId
};
linkresult = context.Links.Add(link);
context.SaveChanges();
contextTransaction.Commit();
}
return linkresult.Id;
}
}
Now as shown above, the context.SaveChanges() with the contextTransaction.Commit() I'm making sure that the data gets written to the database and is not cached at any level. I have confirmed this by using the Server Explorer and watching the content get updated real time.
I also think I have confirmed that my read will pull up the latest information from the database by loading the web application in another browser after the changes have been made, but I acknowledge that this may also be a caching issue that I am not aware of.
My last step is getting around the caching that happens in the browser. I know chrome allows you to clear your app hosted data, but I don't know how to make certain data is not cached so that every time a request happens this code executes.
More Details on the REST API:
The Controller for the above example looks something nearly identical to this:
public ActionResult AddLink(MyLink model)
{
IntegrationManager manager = new IntegrationManager(System.Web.HttpContext.Current.User);
model.Id = manager.AddLink(model.Text, model.Url);
return Json(model);
}
The IntegrationManager is just a basic class that does not implement IDisposable because the context is created and disposed of during each transaction. As can be seen, the AddLink is a member of the IntegrationManager class.
More Details on the Web Application:
The model for the view creates an IntegrationManager in it's constructor as a temporary variable to make the getLinks call as follows:
public Home(IPrincipal user, Cache cache)
{
this.HttpCache = cache;
IntegrationManager _IntegrationManager = new IntegrationManager(user);
this.Links = this.GetLinks(_IntegrationManager);
}
AJAX Call:
.on("click", "#btn-add-link", function (event) {
var text = $("#add-link-text"),
url = $("#add-link-url");
if (text.val().length > 0 && url.val().length > 0) {
var hasHttp = /^http.*$/.test(url.val());
if (!hasHttp) {
url.val("http://" + url.val());
}
$.ajax({
url: addLinkUrl,
type: "POST",
data: { Text: text.val(), Url: url.val() }
}).success(function (data) {
var newLink = $('<li class="ui-state-default deletable" id="link-' + data.Id + '">' + data.Text + '</li>');
$("#user-links").append(newLink);
text.val("");
url.val("");
});
Okay, so I have found out how to make sure no caching happens. The following is an attribute for the controller of the web application called NoCache. To use it, your controller will need the attribute like this:
using whatever.namespace.nocache.lives.in
[NoCache]
Here is the details of the attribute:
public class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
I'm still looking into the details of whether or not I need everything that is included because it does increase the time the page takes to load significantly.

Updating Database via Anonymous Type?

The following code gets all the rows from my Activities table that have not already been posted on Twitter. It then loops through and posts Twitter updates for each of those row. In the process, I would like to update the database to indicate these rows have now been "twittered".
However, I'm getting an error (indicated below) when I try and update this value. I assume this is because I'm using an anonymous type. However, if I use the full type, that will require pulling a lot of unnecessary data from the database.
Is there a way to accomplish this efficiently? Or is this yet another case where EF forces me to make compromises in performance?
using (MyEntities context = new MyEntities())
{
var activities = from act in context.Activities
where act.ActTwittered == false
select new { act.ActID, act.ActTitle, act.Category,
act.ActDateTime, act.Location, act.ActTwittered };
foreach (var activity in activities)
{
twitter.PostUpdate("...");
activity.ActTwittered = true; // <== Error: ActTwittered is read-only
}
}
You could try a "fake object approach" like this:
using (MyEntities context = new MyEntities())
{
var activities = from act in context.Activities
where act.ActTwittered == false
select new { act.ActID, act.ActTitle, act.Category,
act.ActDateTime, act.Location, act.ActTwittered };
foreach (var activity in activities)
{
twitter.PostUpdate("...");
// Create fake object with necessary primary key
var act = new Activity()
{
ActID = activity.ActID,
ActTwittered = false
};
// Attach to context -> act is in state "Unchanged"
// but change-tracked now
context.Activities.Attach(act);
// Change a property -> act is in state "Modified" now
act.ActTwittered = true;
}
// all act are sent to server with sql-update statements
// only for the ActTwittered column
context.SaveChanges();
}
It's "theoretical" code, not sure if it would work.
Edit
Not "theoretical" anymore. I've tested this with DbContext of EF 4.1 and it works as described in the sample code above. (Because DbContext is only a wrapper API around ObjectContext it's almost safe to assume that it also will work in EF 4.0.)
If you simply select 'act', then it should work. Don't forget to submit after editing.
Why are you calling select new instead of returning entire object. Entity framework will only be able to update property if it is correctly defined in schema resources which certainly is not case with anonymous type.
Entity framework will never be able to determine which table and which field the property is mapped to.

Categories