When using ODataConventionModelBuilder in Breeze js, the AutoGeneratedKeyType will consistently be set to 'none'

I am working with a straightforward entityframework poco object

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

The baseClass Entity is structured as below

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

I have exposed this object through an Odata service using the 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();

However, when attempting to consume this service with Breeze js and creating an entity with manager.CreateEntity(), an error is encountered.

Cannot attach an object to an EntityManager without first setting its key or setting its entityType 'AutoGeneratedKeyType' property to something other than 'None'

Interestingly, editing and saving an entity after querying works fine.

The client side technology being used includes angular with breeze, while on the server side Asp.net webapi 2 is combined with an odatacontroller and EntityFramework 6 as ORM.

If anyone can identify what might be going wrong here, please let me know!

Answer №1

It has come to light that Microsoft's ODataConventionModelBuilder is lacking in certain areas, a fact that they have acknowledged. This deficiency extends to both OData WebApi versions 2.1 and 2.2. Among the issues identified are the absence of support for 'referentialConstraints' and information on store generated keys. Microsoft has indicated that they intend to address these issues, along with others, in an upcoming release scheduled for October.

In the meantime, users have a couple of potential solutions:

  • One option is to utilize the ODataConventionBuilder but then update Breeze's metadataStore after retrieving the metadata to rectify any 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... 
    });
    
  • Another approach is to forego fetching metadata from the server altogether and manually define the metadata on the client side using Breeze (see: )

  • Alternatively, users can opt out of using OData entirely and instead leverage Breeze's WebApi implementation (see ). This particular implementation boasts comprehensive features and robustness, making it the preferred choice. Many of the Entity Framework samples in breeze.js.samples utilize this method.

Overall, we highly recommend choosing the last option if feasible. For more information, please refer to this link: ()

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered difficulties while attempting to use keyboard input and received a null value when attempting to retrieve a data-* attribute using Python and Selenium

Encountered an issue with keyboard interaction and finding a null value for the data-* attribute during automated login attempts on Gmail using Python and Selenium While attempting to automatically log in to Gmail using Python and Selenium, I successfully ...

Seeking assistance with setting up BxSlider installation

I'm struggling with adding bxslider to my HTML template. No matter what I try, the slider doesn't work as expected. When I preview my website, all three images are displayed together in a single column. Can someone please guide me on how to fix t ...

Iterate through an array and append individual elements to a fresh array - ensuring only a single item is present in the new

I have been working on a project where I need to call a backend API and retrieve a JSON response. I have come across various solutions, but none seem to address my specific problem. The JSON data returned from the API is structured like this: [ { ...

Experimenting with an Angular controller while maintaining its dependency integrity without the use

After spending 48 hours scouring the internet for answers to my question without success, I find myself seeking help here. The controller I am trying to test is as follows: (function () { "use strict"; angular .module("productManagement" ...

Struggling with making an Ajax and Jquery drop down menu work? Wondering how to use Javascript to retrieve a variable and then utilize it in PHP? Let's troubleshoot

Currently, I am utilizing Ajax/Jquery to present a dropdown menu with data retrieved from my SQL database. The process involves using javascript to obtain a variable that is then utilized in PHP. However, the function doesn't seem to be working as exp ...

Using Regular Expressions to eliminate any characters that are not directly adjacent to numbers (beside specific characters)

My function generates a result that looks like this: given output For comparison, consider the example string below: var string = '    111   1   1\n 1111111 1 \n   &nb ...

How to hide the header on a specific page using Angular

Currently, I am working on an Angular project and my requirement is to hide the header block specifically on the login page. Despite attempting to hide the header on the login page, it seems that my efforts have been unsuccessful so far. Is there anyone wh ...

You can install the precise version of a package as mentioned in package.json using npm

At this moment, executing the command npm install will download the latest versions of packages. Is there a way to install the exact versions specified in the package.json file? ...

How can I keep the cursor from automatically moving to the beginning of an input field when the type changes?

Currently in the process of creating a Password field that allows users to hide or display their password in React/Ionic. I am aiming to maintain focus on the password field when switching the input type, similar to how it is done on the PayPal Login page ...

A webpage layout featuring an HTML table enhanced with JavaScript functionality

Hi there, I'm new to Javascript and looking for a way to simplify my code. Is there a way to use a loop to change the id values in HTML without manually editing them one by one? Here's what I have so far: <table border="2" cellpadding="4"> ...

The Safari 7.1+ browser seems to be having trouble with the spotlight effect

Why is the CodePen not functioning properly in Safari? Despite working well in Chrome and Firefox, it completely fails to work in Safari 7.1+. Can anyone provide some insights? http://codepen.io/cchambers/pen/Dyldj $(document).on("mousemove", function( ...

Utilizing data interchange within AngularJS services

How can data be shared between services in AngularJS? A scenario where data aggregation is needed from various services For instance, having a service1 retrieving data from a REST Service and then service2 adding supplementary data from another REST API ...

Is there a way to conclude an Inquirer prompt depending on the response received?

My current project involves creating an application that presents a series of questions to gather information and generate an employee profile. This application is designed to be recursive, meaning that users will have the option to exit and terminate the ...

Can a repetitive 'setTimeout' function invocation ultimately cause the JS Engine to crash?

Imagine a scenario where I require data from the server every 10 seconds. A function would be created to fetch the data using AJAX, and then setTimeout would be used to schedule the function to run again: function RetrieveData(){ $.ajax({ url: " ...

Button-operated lightbox image slider

I am currently developing a website on WAMP where users can store images and view them in a lightbox effect. I have successfully implemented the lightbox effect, but I am facing challenges with moving between images within the lightbox. <html> <? ...

What is the best way to add borders to the cities on interactive SVG maps?

I'm currently developing Interactive SVG Maps showcasing Turkey. The map consists of 81 cities, each represented by <g> elements, and their respective districts, created as child elements within the city elements. It's worth noting that the ...

Having trouble with the search function in my array, as it is consistently returning false instead of the expected result

Aim: I am working on creating a basic search bar that allows users to input a zip code and matches it with zip codes stored in an array. The objective is to develop a function that can determine whether the entered zip code exists in the array or not, and ...

Restrict HTML Content in Json Result using PHP and Jquery

I'm currently working with a Controller that outputs JSON response and a JavaScript function that appends that response to the last tr in an HTML table. <pre> $reponse="<tr class=\"border_bottom\"><td>My Repo ...

Leveraging Javascript/jquery to retrieve image data for a specific frame within a video

Query My aim is to optimize the code for image manipulation on a web browser using JavaScript. One possible solution I am considering is utilizing a video file to reduce HTTP requests. Is there a way to extract a single frame from a video in png, jpg, or ...

What is the best way to leverage a webbrowser control for design purposes while keeping the functionality in C#?

Observing some apps, it seems they utilize HTML/CSS/Javascript for styling - a much simpler approach compared to crafting the same thing natively. However, these apps have their logic written in C#. Sadly, I am clueless on connecting the two. Research yiel ...