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

EF6 one-to-many fluent api with navigation properties

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

EmailTemplate_Attachments_Source_EmailTemplate_Attachments_Target: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

这些是我的模型:

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");

    }

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

在您的 EmailTemplateConfiguration 中,您将 EmailTemplate 的主键定义为组合键:

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

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

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