2

Im using Breeze in my ASP.NET WebAPI Project, I have added the breezecontroller

    [BreezeController]    
    public class BreezeController : ApiController
    {
        readonly EFContextProvider<MyEntities> _context 
            = new EFContextProvider<MyEntities>();

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

unfortunately when I call createEntity I get the error Cannot attach an object to an EntityManager without first setting its key or setting its entityType 'AutoGeneratedKeyType' property to something other than 'None'

Isn't breeze auto intialized the entity when the metadata api was called?

TIA

tereško
  • 58,060
  • 25
  • 98
  • 150
Mettlus Shaw
  • 69
  • 3
  • 5

2 Answers2

7

I'm betting that the entity you are trying to create is defined such that the client ... not the server ... is responsible for setting a unique key. I'll bet that the key property is a Guid because EF Code First assumes Guid key properties are determined by the client by default.

If so, then it is up to you to set the key when you create the entity BEFORE adding the new entity to the manager.

You have elected to create the new entity with the EntityManager.createEntity method. That's my favorite too.

But you must remember that this method tries to add the new entity to the manager for you automatically (as explained here). Therefore, to use this method, you must initialize the key to the new entity before Breeze adds it to the manager. You can do that in one of three ways:

  1. in a custom constructor that you've defined and registered with the type
  2. in a custom initializer that you've defined and registered with the type (see "Extending Entities" documentation page for both techniques)
  3. in the initializer parameter to the createEntity call.

Option #3 looks like this:

var newThing = manager.createEntity("Thing", {id: breeze.core.getUuid()});

I would pick option #1 myself.

Ward
  • 17,793
  • 4
  • 37
  • 53
  • 1
    so how would you go about creating an entity (and returning is as KO observable) for "Add new XXX" form screen when it is up to the user to specify the id (in my case string of length 7)? Do you just specify a non-unique temp key (e.g. "tmpXXX") and handle validation on save? Is there an example of this somewhere? – zam6ak May 28 '13 at 22:08
  • @Ward Thanks for the answer it solved my issue. It looks like the getUuid() method now resides in breeze.core. You might want to update your answer to reflect this to keep people from having to figure it out on their own. – Brent Stewart Oct 20 '14 at 22:22
  • Thanks. Fixed it. I think it always was in `breeze.core` and that I just had a typo. – Ward Oct 21 '14 at 09:10
2

Please post your client side breeze code. Without it I can't tell for sure, but I would guess that you have not fetched the metadata for that entity before your "createEntity" call. You must have the metadataStore before you can create an entity. For example:

breezeProjectManager().metadataStore.fetchMetadata().then(function() {
  //createEntity code here 
}
chris_dotnet
  • 844
  • 9
  • 14
  • I am calling this to intialize breezector breeze.NamingConvention.camelCase.setAsDefault(); var mgr = new breeze.EntityManager( { serviceName: config.remoteServiceName } ); – Mettlus Shaw Apr 12 '13 at 19:52