1

我有 2 个实体:

  • 声明(ID、其他原语、另外一个关系)- 声明表
  • ClaimDetail(ID、ClaimID、其他原语、关系) - ClaimDetails 表

映射:

modelBuilder.Entity<Claim>()
                        .HasMany(x => x.ClaimDetails)
                        .WithRequired(x => x.Claim)
                        .HasForeignKey(x => x.ClaimID)

生成的查询:

exec sp_executesql N'SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[ClaimID] AS [ClaimID], 
[Extent1].[UserID] AS [UserID], 
[Extent1].[ClaimTypeID] AS [ClaimTypeID], 
[Extent1].[PermissionID] AS [PermissionID], 
[Extent1].[StartDate] AS [StartDate], 
[Extent1].[EndDate] AS [EndDate], 
[Extent1].[Justification] AS [Justification], 
[Extent1].[Claim_ID] AS [Claim_ID]
FROM  [dbo].[ClaimDetails] AS [Extent1]
OUTER APPLY  (SELECT TOP (1) [Project1].[ClaimDetailStatusID] AS [ClaimDetailStatusID]
    FROM ( SELECT 
        [Extent2].[ClaimDetailStatusID] AS [ClaimDetailStatusID], 
        [Extent2].[CreatedOn] AS [CreatedOn]
        FROM [dbo].[ClaimDetailStatus] AS [Extent2]
        WHERE [Extent1].[ID] = [Extent2].[ClaimDetailID]
    )  AS [Project1]
    ORDER BY [Project1].[CreatedOn] DESC ) AS [Limit1]
WHERE ([Extent1].[UserID] = @p__linq__0) AND ( NOT ([Limit1].[ClaimDetailStatusID] IN (12,11,4,7,13,10)))',N'@p__linq__0 int',@p__linq__0=4106

如果我遗漏了映射,那么 EF 将生成另一个名为 Claim_ID1 的列。这有什么问题?我应该如何定义关系?

编辑

public class Claim
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }
    [Required]
    public int SenderID { get; set; }

    [NotMapped]
    public UserModel Sender { get; set; } //TODO:

    [Required]
    public DateTime Created { get; set; }

    public string Note { get; set; }
    [Required]
    public int BossID { get; set; }

    [NotMapped]
    public UserModel Boss { get; set; } //TODO:

    public virtual IList<ClaimDetail> ClaimDetails { get; set; }
    public virtual IList<UserClaim> UserClaims { get; set; }
}

public class BaseClaimItem
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required]
    public int PermissionID { get; set; }

    [Required]
    public DateTime StartDate { get; set; }

    [Required]
    public DateTime EndDate { get; set; }

    public string Justification { get; set; }

    public virtual Permission Permission { get; set; }

    [NotMapped]
    public virtual UserModel Claimant { get; set; }
}


public class ClaimDetail : BaseClaimItem
{
    [Required]
    public virtual int ClaimID { get; set; }

    [Required]
    public int UserID {get;set;}

    [NotMapped]
    public override UserModel Claimant {get;set;}

    [Required, Range(1, 5)]
    public int ClaimTypeID { get; set; }

    public virtual Claim Claim { get; set; }
    public virtual IList<ClaimDetailStatus> ClaimDetailStatus { get; set; }
    public virtual IList<Task> Tasks { get; set; }
    public virtual IList<ClaimDetailApprovalStep> ClaimDetailApprovalSteps { get; set; }

    public virtual ClaimType ClaimType { get; set; }
}
4

2 回答 2

4

Property mapping用而不是尝试一些简单的东西怎么样Fluent API mapping;

public class Claim 
{
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int ClaimId { get; set; }
   public string Name { get; set; }

   public virtual ICollection<ClaimDetail> ClaimDetails { get; set; }
}

public class ClaimDetail
{
   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int ClaimDetailId { get; set; }

   [ForeignKey("Claim"), DatabaseGenerated(DatabaseGeneratedOption.None)]
   public int ClaimId { get; set; }

   public string Detail { get; set; }

   public virtual Claim Claim { get; set; }
}

Fluent API Mapping 应该是;

modelBuilder.Entity<Claim>()
    .HasMany(c => c.ClaimDetail)
    .WithOptional()
    .Map(m => m.MapKey("ClaimId"));
于 2013-05-12T19:49:13.230 回答
1

上面缺少一个接口实现。在该接口中有一个返回集合的属性。缺少的 [NotMapped] 属性现在适用于关联。

于 2013-05-28T14:22:09.227 回答