wonderful world
wonderful world

Reputation: 11599

CreateEntity failure with KeyProperties not defined

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); }
}
}
}

Upvotes: 0

Views: 731

Answers (1)

Jay Traband
Jay Traband

Reputation: 17052

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);
    

Upvotes: 1

Related Questions