views:

526

answers:

2

I’m attempting to use ADO.NET Data Services to update my database.

Tables:

  • Person( PK PersonId; FK EntityRootId)
  • EntityRoot( PK EntityRootId, FK EntityTypeId)
  • EntityTypes( PK EntityTypeId)

I’m trying to create an EntityRoot (unfortunate name since it may be confused with Entity Framework’s Entity. EntityRoot is in my domain) and add it to the database:

var entRoot = EntityRoot.CreateEntityRoot(
    0, "Lou", DateTime.Now, "Lou", DateTime.Now);
var entityType = 
   (from type in myContext.EntityTypeSet
    where (type.Description == "Person")
    select type).FirstOrDefault(); // this works fine and returns the entityType I’m looking for

entRoot.EntityType = entityType;
myContext.AddToEntityRootSet(entRoot); 

// with the line below I get a runtime error:
//  “AddLink and DeleteLink methods only work when the sourceProperty is a collection.”
//myContext.AddLink(entRoot, "EntityType", entityType); 

// without the line above I get an error from the save:
//  “Entities in 'MyEntities.EntityRootSet' participate in the 'FK_EntityRoots_EntityTypeId_Lookup_EntityTypes' 
//    relationship. 0 related 'EntityTypes' were found. 1 'EntityTypes' is expected.”
myContext.BeginSaveChanges(SaveChangesOptions.Batch,
                      new AsyncCallback(OnSaveAllComplete),
                      null);

How can I add a reference to the entRoot.EntityTypeId field?

Thanks for any insight into this.

A: 

Is it just a typo in your question or should it be

myContext.AddLink(entRoot, "EntityTypes", entityType);

At the start of your question you write "EntityTypes" (with and ending "s").

splattne
Yeah, you're right it's a typo. It doesn't affect the example. Though I hate methods that take strings as values like that. BTW, thanks for formatting my question correctly. I tried it a bunch of times and couldn't get it right. I think there may be a browser issue: I'm using IE8.
I'm glad you found the solution using Denis' answer. Interesting question!
splattne
+2  A: 

I think when doing this you should use SetLink instead of AddLink, because the property you are indicating is from Entity to EntityType (an Entity has one EntityType).

This is basically what the error message is telling you (AddLink works only on Collection properties, like the reverse property from type to entity, or for many to many relationships).

Denis Troller
Yep, thanks that's the problem. The save went through perfectly.