EF6一对多流畅的api具有导航属性

时间:2015-06-30 03:03:03

标签: c# entity-framework ef-fluent-api

我正在尝试使用EF6和流畅的API为我的实体应用一对多,但不断收到此错误:

  

EmailTemplate_Attachments_Source_EmailTemplate_Attachments_Target ::关系约束中的从属角色和主要角色中的属性数必须相同。

这些是我的模特:

public class EmailTemplate
{
    public EmailTemplate()
    {
        Attachments = new List<EmailTemplateAttachment>();
    }

    public int EmailTemplateId { get; set; }
    public int OperatorId { get; set; }
    public EmailTemplateType MailType { get; set; }
    public int LanguageId { get; set; }
    public string Subject { get; set; }
    public string Content { get; set; }
    public string FromEmail { get; set; }
    public DateTime CreationDate { get; set; }

    public virtual ICollection<EmailTemplateAttachment> Attachments { get; set; }
}

public class EmailTemplateAttachment
{
    public int EmailTemplateAttachmentId { get; set; }
    public string ShortDescription { get; set; }
    public string FilePath { get; set; }
    public int EmailTemplateId { get; set; }

    public virtual EmailTemplate EmailTemplate { get; set; }

}

这些是实体配置

public EmailTemplateConfiguration()
    {
        ToTable("T_EMAILS");

        HasKey(emailTemplate => new { emailTemplate.OperatorId, emailTemplate.MailType, emailTemplate.LanguageId });
        HasMany(t => t.Attachments)
            .WithRequired(a => a.EmailTemplate)
            .HasForeignKey(a => a.EmailTemplateId);

        Property(emailTemplate => emailTemplate.EmailTemplateId).HasColumnName("row_id")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(emailTemplate => emailTemplate.OperatorId).HasColumnName("operator_id");
        Property(emailTemplate => emailTemplate.MailType).HasColumnName("mail_type");
        Property(emailTemplate => emailTemplate.LanguageId).HasColumnName("language_id");
        Property(emailTemplate => emailTemplate.Subject).HasColumnName("subject");
        Property(emailTemplate => emailTemplate.Content).HasColumnName("mail_content");
        Property(emailTemplate => emailTemplate.FromEmail).HasColumnName("from_email");
        Property(emailTemplate => emailTemplate.CreationDate).HasColumnName("insert_date");

    }

 public EmailTemplateAttachmentConfiguration()
    {
        ToTable("T_EMAILS_ATTACHMENTS");

        HasKey(a => a.EmailTemplateAttachmentId);
        HasRequired(a => a.EmailTemplate)
            .WithMany(t => t.Attachments)
            .HasForeignKey(a => a.EmailTemplateId);

        Property(a => a.EmailTemplateAttachmentId).HasColumnName("attachment_id")
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
        Property(a => a.ShortDescription).HasColumnName("short_description");
        Property(a => a.FilePath).HasColumnName("file_url");
        Property(a => a.EmailTemplateId).HasColumnName("mail_id");

    }

我做错了什么?我已经尝试了很多等待配置外键并一次又一次地获得相同的异常

1 个答案:

答案 0 :(得分:2)

在您的EmailTemplateConfiguration中,您定义了EmailTemplate的主键是复合键:

HasKey(emailTemplate => new { emailTemplate.OperatorId, emailTemplate.MailType, emailTemplate.LanguageId });

但是在EmailTemplateAttachmentConfiguration中,您将依赖项配置为使用EmailTemplateId作为外键,这与您在上面定义的主键不同。外键应与主表主键相同。

此外,您定义了两次EmailTemplate和EmailTemplateAttachment之间的关系(一个在EmailTemplateConfiguration中,一个在EmailTemplateAttachmentConfiguration中)。它是多余的,一个就够了