实体框架外键不同类型

时间:2014-05-06 17:42:03

标签: frameworks entity

我最难处理不同类型的外键。在一个表中,它是一个字符串,在另一个表中,它是一个int,我无法更正数据库。

我有两个表,我将称之为MainLine和CommentLine,以一对多的关系。

public partial class MainLineDTO
{
   public MainLineDTO()
    {
       this.CommentLineDTO = new List<CommentLineDTO>();
    }

    public virtual ICollection<CommentLineDTO> CommentLineDTO { get; set; }

    public string Timesheet_No_ { get; set; }
    public int Line_No_ { get; set; }     
}


public partial class CommentLineDTO
{
    public CommentLineDTO()
    {
    }

    public string No_ { get; set; }
    public String Code { get; set; }
    public string Comment { get; set; }

    public virtual MainLineDTO MainLineDTO { get; set; }
}

这些是我的映射文件:

public class MainLineMap : EntityTypeConfiguration<MainLineDTO>
{
    public MainLineMap ()
    {
        // Primary Key
        this.HasKey(t => new { t.Timesheet_No_, t.Line_No_});

        // Properties
        this.Property(t => t.Timesheet_No_)
            .IsRequired()
            .HasMaxLength(10);

        this.Property(t => t.Line_No_).IsRequired();
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

        this.ToTable("Main Table");
        this.Property(t => t.Line_No_).HasColumnName("Line No_");
        this.Property(t => t.Timesheet_No_).HasColumnName("Timesheet No_");

    }
}

public class CommentLineMap : EntityTypeConfiguration<CommentLineDTO>
{
    public CommentLineMap()
    {
        // Primary Key
        this.HasKey(t => new {t.No_, t.Code, t.Comment});

        // Properties
        this.Property(t => t.No_)
            .IsRequired()
            .HasMaxLength(20);

        this.Property(t => t.Code)
            .IsRequired();

        this.Property(t => t.Comment)
            .IsRequired()
            .HasMaxLength(80);


        this.ToTable("Comment Table");
        this.Property(t => t.No_).HasColumnName("No_");
        this.Property(t => t.Code).HasColumnName("Code");
        this.Property(t => t.Comment).HasColumnName("Comment");

        this.HasRequired(t => t.Timesheet_WIP_LineDTO)  
            .WithMany(t => t.CommentLineDTO)
            .HasForeignKey(d => new { d.No_, d.Code });
    }
}

所以我要做的是加入行WHERE MainLine.Line_No_ = CommentLine.Code AND MainLine.Timesheet_No_ = CommentLine.No_。 麻烦是Line_No_是一个int,Code是一个String。无论我尝试过什么,代码要么没有编译,要么给了我一个 例外以后。我应该如何修改我的代码以使联接起作用?

0 个答案:

没有答案
相关问题