0

The Client is retrieving the metadata from a service. That call is succeeding.

But the call entityManager.CreateEntity(); is failing.

The error is:

"There are no KeyProperties yet defined on EntityType: 'Customer:#MyCommerceServer.Models'. Please insure that the metadata for this type is complete either by calling FetchMetadata or by explicitly updating the KeyProperties before creating an EntityKey for this type."}

But the following passes with an exception says the customer is detached.

var customerType = entityManager.MetadataStore.GetEntityType(typeof(Customer));
var customer = customerType .CreateEntity();

Here is my set up. The Customer entity has a key named Id. The Customer entity on the client also has the same key. The entities on client and server exist in the same namespace.

Is there any setup I have to add to have the Customer entity KeyProperties? I see the same problem in the ToDo sample project also.

******** Update on 8/12/2014

On the server:

namespace MyCommerceServer.Models
{
public class Customer
{
public int Id { get; set; }
}
}

On the client:

namespace MyCommerceServer.Models
{
public class Customer : BaseEntity
{
public int Id
{
get { return GetValue<int>(); }
set { SetValue(value); }
}
}
}
wonderful world
  • 10,969
  • 20
  • 97
  • 194

1 Answers1

1

The difference between the Breeze.sharp EntityManager.CreateEntity method and the EntityType.CreateEntity method is that the first, by default, adds the newly created entity to the EntityManager whereas the 2nd does not. The error you are getting occurs when an entity is added to an EntityManager and that entity either has no key defined or the key is set to just the default values for all of the key properties. The key is needed before the entity can be attached because the entity is cached in the EntityManager by its key.

So you have several options,

  • You can set the key properties in the EntityManager.createEntity call using an anon object like so:

    var newCust = (Customer) myEntityManager.CreateEntity(typeof(Customer), 
           new { Id = 999 }));
    
  • or you can use the EntityType.CreateEntity method and set the Id before adding the entity to the entityManager

    var customerType = myEntityManager.MetadataStore.GetEntityType(typeof(Customer));
    var customer = customerType.CreateEntity();
    customer.Id = 999;
    myEntityManager.AddEntity(customer);
    
  • or you can change your metadata for the customer type to use Identity keys. This will mean that the AutoGeneratedKeyType property of the customer is set to either Identity or KeyGenerator. Note that either of these will require changes to your server side model to accomodate the change.

    var customerType = entityManager.MetadataStore.GetEntityType(typeof(Customer));
    Assert.IsTrue(customerType.AutoGeneratedKeyType == AutoGeneratedKeyType.Identity);
    
Jay Traband
  • 17,053
  • 1
  • 23
  • 44
  • Thank you Jay for your time to answer this question. Appreciate it. The setting of the id is little puzzling me. How do I know the id of the entity that I want to add to the DB store? It depends on the last entity id that exists in the DB. In the RIA world what I have seen is that the id set to 0 for new entities. I think this new entity can exist in the RIA cache (EntityManager) also. When the changeset is submitted, the id will be changed back to whatever appropriate. – wonderful world Aug 13 '14 at 21:55
  • This is the same idea as setting the AutoGeneratedKeyType to Identity or KeyGenerator. In these cases you do not have to set the key on the client but you will have either use identity columns on the server or implement server side key generation. Take a look at the breeze docs about creating a new entity and about saving entities. – Jay Traband Aug 13 '14 at 22:10
  • Thank you Jay. My impression is that documentation is little lacking for Breeze.Sharp, but there is plenty for Breeze.js. Recently I found that the architecture looks the same, so the breeze.js documentation apply for Breeze.Sharp also. – wonderful world Aug 13 '14 at 23:59
  • Actually, the client side api are as similar as we could make them, and the server side is EXACTLY the same. So any server side docs are relevant to both Breeze.js and Breeze.sharp. and... we are trying to improve the Breeze.sharp client docs incrementally. – Jay Traband Aug 14 '14 at 03:17