简体   繁体   中英

EF6 one-to-many fluent api with navigation properties

I'm trying to apply a one-to-many for my entities using EF6 and fluent API but keep getting this error:

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

These are my models:

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

}

These are the entities configurations

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

    }

What am I doing wrong? I've tried so many wait to configure the foreign key and keep getting the same exception again and again

In your EmailTemplateConfiguration, you define the primary key for EmailTemplate is composite key:

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

But in EmailTemplateAttachmentConfiguration, you configure the dependent to use EmailTemplateId as foreign key, which is different from the primary key you defined above. Foreign key should be the same with principal table primary key.

Also, you define the relation between EmailTemplate and EmailTemplateAttachment twice (one in EmailTemplateConfiguration and one in EmailTemplateAttachmentConfiguration). It's redundant, one is enough

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM