Отношение один к одному, другое имя ключевого столбца, Entity Framework, Code First - PullRequest
5 голосов
/ 13 мая 2011

У меня есть две таблицы, которые уже были созданы.Document и DocumentStyle.Они имеют отношение один к одному через столбец DocumentID.Тем не менее, он называется Id в Document таблице и DocumentId в DocumentStyle таблице.

Что-то вроде этого

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

Я получаю следующую ошибку вVS

Недопустимый атрибут ForeignKeyAttribute для свойства 'DocumentStyle' для типа 'KII.Models.Document'.Имя внешнего ключа «DocumentId» не найдено в зависимом типе «KII.Models.Document».Значение Name должно быть разделенным запятыми списком имен свойств внешнего ключа.

Это часть кода для класса модели документа

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

EDIT:

Это код моих занятий.

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; }

    }

1 Ответ

6 голосов
/ 13 мая 2011
Атрибут

ForeignKey соединяет свойство внешнего ключа и свойство навигации. Он не определяет свойства из связанной таблицы! Поэтому вы должны использовать либо:

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

, если Document является зависимым объектом или:

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

если DocumentStyle зависит

...