Pergunta

I have a squirrely situation, where in some relationships a tables ID field is used as the key reference. But in other, older implementations, the Name field was used as the relationship mapping.

FileType:
ID | Name |....

FileTypeDetails:
ID | FileTypeId | ....

PostingGroupDetails :
FileType | ....

PostingGroupDetails.FileType has a map to FileType.Name. FileTypeDetails.FileTypeId maps to FileType.ID.

I am using the Fluent API structure to do the mappings manually but am running into a wall with this relationship mapping.

I though that doing a multi-key mapping might work but am unsure. Still in the design stage, and about to implement the interface side of the project.

Any ideas on how i make the mapping available to both, until we can consolidate the relationship mapping to one or the other?

EF Implementation:

FileType

I know FileType is currently Optional, but its only a direct correlation to the Table design. I am flagging it for schema updating, currently in practice it is a required field in order for the entry to be submitted.

ToTable( "FileType" , "Int" );

HasKey( ftd => ftd.ID );
Property( ftd => ftd.ID ).HasColumnName( "ID" )
    .HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity );
Property( ftd => ftd.Name).HasColumnName( "Name" )
    .IsOptional();
//Navigation Properties
HasMany( ftd => ftd.FileNames )
    .WithRequired( fn => fn.FileType );
HasMany( ftd => ftd.PostingGroupDetails )
    .WithRequired( pgd => pgd.FileTypeDetails );

FileNames

FileNames.FileTypeID relates to FileType.ID

ToTable( "FileNames" , "Int" );

HasKey( fn => fn.ID );
Property( fn => fn.ID ).HasColumnName( "ID" )
    .HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity );
Property( fn => fn.FileTypeID ).HasColumnName( "FileTypeID" )
    .IsRequired();
//Navigation Properties
HasRequired( fn => fn.FileType )
    .WithMany( ftd => ftd.FileNames )
    .HasForeignKey( fn => fn.FileTypeID );

PostingGroupDetails

PostingGroupDetails.FileType relates to FileType.Name

ToTable( "PostingGroupDetails " , "Int" );

HasKey( pgd => pgd.ID );
Property( pgd => pgd.ID ).HasColumnName( "ID" )
    .HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity );
//Required Properties
Property( pgd => pgd.FileType ).HasColumnName( "FileType" )
    .IsRequired();

//Navigation Properties
HasRequired( pgd => pgd.FileTypeDetails )
    .WithMany( ftd => ftd.PostingGroupDetails );

Any ideas would be greatly appreciated. Refactoring the Database structure is not an option right now, but it is on the plate to be done.

Foi útil?

Solução

Entity Framework allows for differences between the database model and the class model (or conceptual model). You can define primary keys in the conceptual model that don't exist, or aren't even constrained as unique, in the data model.

So you can (and have to) choose which fields you want to mark as PK in the conceptual model. You can only define associations to PKs that are defined in the conceptual model and there can only be one PK per entity. So for FileType you must either take ID or Name and then you can define an association with FileTypeDetails or PostingGroupDetails. For the other associations you know exist you'll have to write join queries. As in

from x in X
join y from Y on x.Name equals y.Name
select ...
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top