Search code examples
angularjsbreezehottowel

dreaded "Ids can not be autogenerated for entities with multipart keys"


Ok I’m at a loss, being new to breeze I’m still learning the ropes. My project uses the hot towel template for AngularJs and breeze from John Papa.

He's what I’m trying to achieve: I have a master\slave tables in my database. An "Agency" has many people it can "Notify". Here are the EF classes for the server side:

public class Agency {

    public Agency() {  
    this.Notifies = new HashSet<Notify>();
   }

   public long Id { get; set; }

   [Required, MaxLength(50)]
   public string Name { get; set; }
   <<removed unneeded details>>
   public bool Active { get; set; }

   public virtual ICollection<Notify> Notifies { get; set; }
}   

public class Notify
{
   public long Id { get; set; }
   public long? AgencyId { get; set; }
   public string Name { get; set; }
   <<removed unneeded details>>

   public virtual Agency Agency { get; set; }
}

Now the Maps:

public class AgencyMaps : EntityTypeConfiguration<Agency>
{
   internal AgencyMaps()
   {
     HasKey(x => x.Id);
   }
}

public class NotifyMap : EntityTypeConfiguration<Notify>
{
   internal NotifyMap()
   {
      HasKey(x => x.Id);

      HasOptional(x => x.Agency)
         .WithMany(p => p.Notifies)
         .HasForeignKey(i => i.AgencyId);
   }
}

Now on the client side I use breeze to create new entities like this:

// create a new entity
function create() {
  return manager.createEntity(entityName);
}
// create a new notify entity
function createNotify(){
    return manager.createEntity(entityNameNotify);
}

Then there are two scenarios I need to achieve: - First is where I retrieve an existing agency and add additional people to notify - Second is where I create a new agency and add people to notify

Both fail in the same place.

Note: I’m using SQL server and my Id fields are bigint (long) at this point in time.

I’m retrieving the “Agency” entity and placing it in a variable called “vm.agency”. “vm.agency” has a navigation called “notifies” with an entity type of “Notify”. So when I want to create and add a new person I’m calling this function:

function addNotifyRec(){
   if (vm.agency !== undefined){
      var notifyRec = datacontext.agency.createNotify(); //<<< fails here
      notifyRec.agencyId = vm.agency.id;
      notifyRec.name = vm.notify.name;
      <<removed unneeded details>>
      vm.agency.notifies.push(notifyRec);
      logSuccess(“New person to notify added”);
   }
   else
   { logError(“Agency is undefined”); }
}

As soon as the createNotify() is called I get the “Ids can not be autogenerated for entities with multipart keys” error.

So I’m stuck. It seems to me this is a pretty common scenario. I am obviously not understanding the breeze framework well enough to implement this. If you can point me in the right directions I’d appreciate your help.

UPDATE 4/9/2014 I'm thinking i could eliminate this issue altogether if i switch over to guid id and generate them client side. Is this correct thinking?


Solution

  • I took a different approach on working around my issue. Since i have the luxury to change out the id types, i swapped out the bigint ids to Uuid types and removed the auto generation of the ids in sql. Now i'm just creating my own ids using breeze.core.getUuid() when a new record is created. Not sure this is the most efficient way to work around the issue, but it seems to be working fine.