Domanda

Sto tentando di utilizzare ADO.NET Data Services per aggiornare il mio database.

Tavoli:

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

Sto cercando di creare un EntityRoot (nome sfortunato poiché potrebbe essere confuso con Entity Framework & Entity. EntityRoot è nel mio dominio) e aggiungerlo al 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); 

Come posso aggiungere un riferimento al campo entRoot.EntityTypeId?

Grazie per qualsiasi informazione in merito.

È stato utile?

Soluzione

Penso che quando fai questo dovresti usare SetLink invece di AddLink, perché la proprietà che stai indicando va da Entity a EntityType (un'entità ha un EntityType).

Questo è fondamentalmente ciò che ti dice il messaggio di errore (AddLink funziona solo sulle proprietà Collection, come la proprietà inversa dal tipo all'entità o per molte o molte relazioni).

Altri suggerimenti

È solo un refuso nella tua domanda o dovrebbe essere

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

All'inizio della tua domanda scrivi " EntityTypes " (con e finale "s").

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top