Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8999585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:11:59+00:00 2026-06-16T00:11:59+00:00

Using Breeze, what is the simplest way to populate a GUID key when an

  • 0

Using Breeze, what is the simplest way to populate a GUID key when an entity is created?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:12:00+00:00Added an answer on June 16, 2026 at 12:12 am

    I’ll assume that your entity is configured such that the client is responsible for setting the Guid key for new entities. That’s the default for the Guid key of an Entity Framework Code First entity; it is as if the key property were adorned with [DatabaseGenerated(DatabaseGeneratedOption.None)]

    The obvious approach is to set the key after creating the entity and before adding it to the manager, e.g.:

    function createFoo() {
        var foo = fooType.createEntity(); 
        foo.id(breeze.core.getUuid()); // Knockout implementation
        manager.addEntity(foo);
    }
    

    This may be all you ever need.

    On the other hand, you may find that you’re creating new Foos in many places and for some strange reason you can’t use the createFoo function. You certainly don’t want to repeat that code.

    You can extend the Foo entity type with id-setting behavior after which you’d be able to write:

    function createFoo() {
        var foo = fooType.createEntity(); // foo.id is set for you
        manager.addEntity(foo);
    }
    

    There are two approaches to consider – custom constructor and type initializer; both are described in “Extending Entities“

    Constructor

    You can initialize the key inside a custom constructor. Breeze calls the constructor both when you create the entity and when it materializes a queried entity. Breeze will replace the initial key value when materializing.

    Here’s an example that assumes the Knockout model library.

    function Foo() {
        foo.id(breeze.core.getUuid()); // using KO
    }
    
    // one way to get the MetadataStore
    var store = manager.metadataStore;
    // register the ctor with the Foo type
    store.registerEntityTypeCtor("Foo", Foo);
    

    Pretty simple. The only downside is that Breeze will generate a Guid every time it makes an entity, whether creating a new one or materializing one from a query. It’s wasted effort during materialization but so what? Well, I suppose that might become a performance issue although I wouldn’t assume so until I had measured it.

    Initializer

    Suppose you measured and the repeated Guid generation is a serious problem (really?). You could set the key in a type initializer instead and only call the Guid generator when creating a new entity.

    Breeze calls a type initializer after the entity has been created or materialized from query just before returning that entity to the application. Clearly you don’t want to overwrite a materialized key from the database so you’ll test the key value to make sure it’s not real (i.e. to make sure you’re fixing a created entity) before assigning it. Here’s an example.

    function fooInitializer(foo) {
        var emptyGuid = "00000000-0000-0000-0000-000000000000";
        if (foo.id() !=== emptyGuid) {
            foo.id(breeze.core.getUuid());
        }
    }
    
    var store = manager.metadataStore;
    // register the initializer; no ctor in this example
    store.registerEntityTypeCtor("Foo", function(){}, fooInitializer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Usually using Visual Studio's debugger is a breeze. Scanning through Locals quickly shows the
Using SQL Server 2008 R2 we are looking for a way to select the
Using the following as an example (with $db being a previously created database connection
Using Breeze 0.73.3. Modeling my app based on John Papa's Breeze version of Code
How would I use breeze against an odata service using only the server's metadata
I've recently added Breeze to a project using Knockout and ASP.NET MVC4. I really
Using a JEditorPane, is there any way to tell if the caret is currently
We are using Hibernate 4.1.4 with an entity manager implementation and are very pleased
Using CRM 4, I have an entity form that contains a tab with an
Using R, what is the best way to read a symmetric matrix from a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.