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