10

Uso schema Innanzitutto, ho una struttura di database, come cosìCome aggiungere un'associazione EF6 a una chiave candidata/chiave univoca che non è la chiave primaria?

ExternalDataItems 
--- 
edataitem_id PK -- surrogate auto-increment - NOT for FK relation here 
datahash  UX -- Candidate Key/Unique Index (binary(20)) 

ExternalMaps 
--- 
emap_id  PK 
ext_datahash FK on ExternalDataItems.datahash - NOT referencing the surrogate PK 

e dopo aver generato lo SSDL/CSDL ha questa

<Association Name="FK_ExtMaps_ExtDataItems"> 
     <End Multiplicity="1" Role="ExternalDataItems" Type="Store.ExternalDataItems" /> 
     <End Multiplicity="*" Role="ExternalMaps" Type="Store.ExternalMaps" /> 
     <ReferentialConstraint> <!-- error on this element --> 
     <Principal Role="ExternalDataItems"> 
      <PropertyRef Name="datahash" /> 
     </Principal> 
     <Dependent Role="ExternalMaps"> 
      <PropertyRef Name="ext_datahash" /> 
     </Dependent> 
     </ReferentialConstraint> 
    </Association> 

che genera un errore sull'elemento <ReferentialConstraint>

Running transformation: Properties referred by the Principal Role ExternalDataItems must be exactly identical to the key of the EntityType ExternalDataItem referred to by the Principal Role in the relationship constraint for Relationship FK_ExtMaps_ExtDataItems. Make sure all the key properties are specified in the Principal Role.

"Principale (?) Ruolo" per ExternalDataItems SSDL sia simile alla seguente, per il PK, e l'UX non sembra essere presente , se non come una semplice proprietà scalare:

<EntityType Name="ExternalDataItems"> 
     <Key> 
     <PropertyRef Name="edataitem_id" /> 
     </Key> 
     .. 
     <Property Name="datahash" Type="binary" MaxLength="20" Nullable="false" /> 
    </EntityType> 

Come posso aggiungere questo Relazione - utilizzando un FK per una chiave candidata non PK? (Dopo questo "funziona", desidero anche avere una proprietà di navigazione sul CSDL.)

Inoltre, la linea di associazione non viene visualizzata nella superficie del progetto, il che, a mio avviso, è solo una ricaduta di questo errore. Sto usando la versione Entity Framework 6.1.1 (ultima pubblicata su NuGet) e Visual Studio 2013 ultimo aggiornamento 4.


L'EDMX "Aggiornamento dal database" standard non sembra raccogliere questi FK relazioni (che may be related to this bug) e i risultati sopra riportati sono dopo aver utilizzato gli strumenti Huagati DBML/EDMX. Se provassi a "Aggiungi associazione" prima che il progettista tentasse in modo errato di utilizzare la chiave primaria, che non è supportata da alcuna relazione FK, non forniva opzioni per la scelta di proprietà alternative.


Il tentativo di aggiungere un elemento <UniqueConstraint> come descritto in "Unique Constraints in the Entity Framework" risultati in amichevole errore di convalida XML:

The element 'ElementType' .. has an invalid child element 'UniqueConstraint'.

risposta

7

Sembra "missing important feature"

.. The Entity Framework currently only supports basing referential constraints on primary keys and does not have a notion of a unique constraint.

È possibile rimuovere chiave esterna nello schema DB e utilizzare LINQ per unire le tabelle:

from item in ExternalDataItems 
join map in ExternalMaps on item.datahash = map.ext_datahash 
select new { item.edataitem_id, map.emap_id }; 

Inoltre è possibile creare la VISTA con queste tabelle unite e utilizzare quella.

+0

Questo è vero a partire da EF6.1 e affligge tutte le parti di EF che desiderano trattare un modello non "Code First" di qualsiasi complessità seria. Attualmente sto usando l'approccio di join manuale e "funziona". – user2864740

Problemi correlati