Breeze js AutoGeneratedKeyType is always "no" with ODataConventionModelBuilder

I have a simple poco entityframework

public partial class Location: Entity
{
    [Key]
    public int Id { get; set; }
    public string Description { get; set; }
}

      

The baseClass object looks like this

public abstract class Entity : IObjectState
{
    [NotMapped]
    public ObjectState ObjectState { get; set; }
}

      

I expose this object through Odata service using ODataConventionModelBuilder

        var server = GlobalConfiguration.DefaultServer;
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.Namespace = "MyNameSpace.Models";
        builder.EntitySet<Location>(typeof(Location).Name);
        var model = builder.GetEdmModel();
        config.Routes.MapODataServiceRoute("odata", "odata", model, new DefaultODataBatchHandler(server));


        config.AddODataQueryFilter();

      

When I use this service with Breeze js, I get an error from Breeze when I try to create an object with manager.CreateEntity ().

It is not possible to attach an entity to the EntityManager without first configuring it or setting the entityType 'AutoGeneratedKeyType' property to something other than None

When I query an entity and edit the data and save it. Everything is good.

I am using angular combined with breeze on the client and Asp.net webapi 2 combined with odatacontroller on the server with EntityFramework 6 as ORM.

Can someone tell me what I am doing wrong?

+3


source to share


1 answer


Microsoft ODataConventionModelBuilder is very incomplete and they know about it (this applies to both OData WebApi v 2.1 and 2.2). Along with a few less issues, it does not support "referentialConstraints" or any store-generated key information. MS is aware of these issues, among other things, and said some of them will be fixed in a new release in October.

Until then, you have several options.



  • Use ODataConventionBuilder, but then update the breeze metadataStore after the metadata is fetched to fix missing or incorrect metadata.

    myMetadataStore.metadataFetched.subscribe(function(args) {
      var ms = args.metadataStore;
      var entityType = ms.getEntityType("Customer");
      entityType.setProperties({ autoGeneratedKeyType: AutoGeneratedKeyType.Identity });
      dp = entityType.getDataProperty("companyName");
      dp.validators.push(Validator.maxLength({ maxLength: 40 }));
      // etc... 
    });
    
          

  • Avoid fetching metadata from the server altogether and just describe the metadata on the breeze client directly: (see http://www.breezejs.com/documentation/metadata-by-hand )

  • Skip OData entirely and use the Breeze WebApi implementation (see http://www.breezejs.com/documentation/aspnet-web-api ). This has the advantage of being both the most complete and most reliable implementation of all breeze functions. Most of the Entity Framework samples in breeze.js.samples are built with this.

All in all, we WILL REALLY recommend the last choice if available to you. Please read this ( http://www.breezejs.com/documentation/odata-server ) for more information.

+2


source







All Articles