1

I would like to create my breeze entity on the server side rather than using breeze entitymanager.createEntity, so that I can set properties of the entity; I don't want to do this on the client side.

SERVER SIDE api controller:

[HttpGet]
public Foo CreateFoo() 
{

    Foo f = new Foo()
    {
       PrimaryKey = Guid.NewGuid(),
       SomeProperty = "XXX";
    };

    return f;
    // return _contextProvider.Context.Users.Add(user); TRIED THIS TOO

}

[HttpGet]
public string Metadata()
{
   return _contextProvider.Metadata();
}

CLIENT SIDE -

var query = breeze.EntityQuery.from("CreateFoo");
manager.executeQuery(query).then(function(data) {

    // returned entity has entityState "Unchanged" here

    // this fixes, but is it correct? - YES per accepted answer's comments 
    data.results[0].entityAspect.entityState = breeze.EntityState.Added; 

    return data;
});

I've tried this a number of ways but always get concurrency exceptions on the server side when I call SaveChanges.

How can I do this?

1 Answer 1

1

Several questions,

  1. Do you have metadata on the client for the "Foo" type. (either via the Metadata endpoint or created on the client with the MetadataStore api)
  2. Where are you assigning the primary key for the "Foo" instance?
  3. Are you updating or at least setting the concurrency field for the "Foo" instance (assuming that have one)?
Sign up to request clarification or add additional context in comments.

5 Comments

Jay, I have updated my question to hopefully answer 1 and 2 - yes I have a Metadata endpoint, and I am setting the Primary Key in this server side method. Not sure about 3 so I guess the answer would be no on this one.
Check your EntityType metadata for the "Foo" type to see if a concurrency field is defined. If so, go ahead and set it before you save. If not, please post back.
Jay - I added a TimeStamp field to the database/model, which is "Computed" in EF. That alone did not fix my problem, but on the client side I now set the breeze entity entityAspect.entityState to EntityState.Added before using, which did fix it. However I would like to know if this is the correct thing to do? I added this to my question
Yes, this is correct because a query by default returns "existing" entities. You are going around the system and creating a "new" entity with a query, so your workaround does make sense. The reason that you were getting a concurrency exception is that Breeze was trying to "update" an instance of this entity and couldn't find it and therefore assumed that this was a concurrency error (i.e. that another user had updated it).
The concurrency field helped confirm the problem. Now that you know how to use EntityState.Added to achieve your purpose, you shouldn't need the concurrency property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.