2011-05-13 3 views
5

J'ai deux tables qui ont déjà été créées. Document et DocumentStyle. Ils ont une relation un à un à travers la colonne DocumentID. Cependant, il est appelé Id dans le tableau Document, et DocumentId dans le tableau DocumentStyle.Relation un à un, nom de colonne de clé différent, Entity Framework, Code Première approche

Quelque chose comme ça

> Document   DocumentStyle 
> |----------|  |----------------| 
> |Id - Key |<------>|DocumentId- key | 
> |Name-VChar|  |Color  -VChar| 
> |Desc-VChar|  |Font  VChar | 
> |----------|  |----------------| 

Je reçois l'erreur suivante dans VS

Le ForeignKeyAttribute sur la propriété 'DocumentStyle' sur le type 'KII.Models.Document' est pas valide . Le nom de clé étrangère 'DocumentId' était introuvable sur le type dépendant 'KII.Models.Document'. La valeur Nom doit être une liste séparée par des virgules des noms de propriété de clé étrangère .

Cela fait partie du code pour le modèle de document classe

[ForeignKey("DocumentId")] public 
DocumentStyle DocumentStyle { get;set; } 

EDIT:

Voici le code de mes classes.

public class Document 
    { 
     [Key] 
     public int ID { get; set; } 
     public string Name { get; set; } 
     public int FundId { get; set; } 
     public int ClientId { get; set; } 

     [ForeignKey("FundId")] 
     public Fund Fund { get; set; } 

     [ForeignKey("ClientId")] 
     public Client Client { get; set; } 
     //public ImageWrapper Logo { get; set; } 

     [ForeignKey("ID")] 
     public DocumentStyle DocumentStyle { get; set; } 

     public Document() 
     { 

     } 

     public Document(DocumentStyle documentStyle) 
     { 
      DocumentStyle = documentStyle; 
     } 

    } 


public class DocumentStyle 
    { 

     public DocumentStyle() 
     { 

     } 

     [Key] 
     [DisplayName("Document ID")] 
     public int DocumentId { get; set; } 

     [ForeignKey("DocumentId")] 
     public Document Document { get; set; } 

     [DisplayName("Title Foreground Color")] 
     public string TitleForegroundColor { get; set; } 

     [DisplayName("Title Background Color")] 
     public string TitleBackgroundColor { get; set; } 

     [DisplayName("Title Font Family")] 
     public string TitleFontFamily { get; set; } 

     [DisplayName("Title Font Size")] 
     public string TitleFontSize { get; set; } 

     [DisplayName("Title Font Style")] 
     public string TitleFontStyle { get; set; } 

     [DisplayName("Title Font Weight")] 
     public string TitleFontWeight { get; set; } 

     [DisplayName("Title Text Decoration")] 
     public string TitleTextDecoration { get; set; } 

     [DisplayName("Section Title Foreground Color")] 
     public string SectionTitleForegroundColor { get; set; } 

     [DisplayName("Section Title Background Color")] 
     public string SectionTitleBackgroundColor { get; set; } 

     [DisplayName("Section Title Font Family")] 
     public string SectionTitleFontFamily { get; set; } 

     [DisplayName("Section Title Font Size")] 
     public string SectionTitleFontSize { get; set; } 

     [DisplayName("Section Title Font Styled")] 
     public string SectionTitleFontStyle { get; set; } 

     [DisplayName("Section Title Font Weight")] 
     public string SectionTitleFontWeight { get; set; } 

     [DisplayName("Section Title Text Decoration")] 
     public string SectionTitleTextDecoration { get; set; } 

     [DisplayName("Paragraph Foreground Color")] 
     public string ParagraphForegroundColor { get; set; } 

     [DisplayName("Paragraph Background Color")] 
     public string ParagraphBackgroundColor { get; set; } 

     [DisplayName("Paragraph Font Family")] 
     public string ParagraphFontFamily { get; set; } 

     [DisplayName("Paragraph Font Size")] 
     public string ParagraphFontSize { get; set; } 

     [DisplayName("Paragraph Font Style")] 
     public string ParagraphFontStyle { get; set; } 

     [DisplayName("Paragraph Font Weight")] 
     public string ParagraphFontWeight { get; set; } 

     [DisplayName("Paragraph Text Decoration")] 
     public string ParagraphTextDecoration { get; set; } 

     [DisplayName("Logo")] 
     public byte[] Logo { get; set; } 

    } 

Répondre

6

ForeignKey paires d'attributs propriété clé étrangère et la navigation. Il ne définit pas la propriété de la table liée! Donc, vous devez utiliser:

public class Document 
{ 
    public int Id { get; set; } 
    [ForeignKey("Id")] 
    public DocumentStyle DocumentStyle { get; set; } 
} 

si Document est entité dépendante ou:

public class DocumentStyle 
{ 
    public int DocumentId { get; set; } 
    [ForeignKey("DocumentId")] // Should not be needed 
    public Document Document { get; set; } 
} 

si DocumentStyle dépend

+0

Grand. Je me suis trompé. Il a fait l'affaire. J'ai passé cette erreur. Cependant, je reçois celui-ci maintenant. "La multiplicité n'est pas valide dans le rôle" Document_DocumentStyle_Source "dans la relation" Document_DocumentStyle "Étant donné que le rôle dépendant fait référence aux propriétés de clé, la limite supérieure de la multiplicité du rôle dépendant doit être 1" – Omar

+0

Afficher vos entités ou mappage fluide. Cela ressemble à vous essayez de définir un côté facultatif quand ce n'est pas possible. –

+1

L'attribut ForeignKey ne doit être utilisé que d'un côté de la relation où la clé étrangère est définie. Je suppose que 'Document' est principal dans votre modèle, donc supprimez l'attribut de la propriété' DocumentStyle'. –

Questions connexes