我正在使用EF 6並使用TPT策略來模擬我的問題。 Rule
是一個抽象類。 OvertimeRule
是一個具體的類,繼承自Rule
。EF和TPT:在SET子句中多次指定列名稱
Rule
看起來是這樣的:
public abstract class Rule
{
public int Id { get; set; }
public PeriodType PeriodType { get; set; }
public int SortOrder { get; set; }
public int StatuteId { get; set; }
public bool IsActive { get; set; }
}
OvertimeRule
看起來是這樣的:
public partial class OvertimeRule : Rule
{
public decimal? ThresholdCoefficient { get; set; }
public decimal? LimitCoefficient { get; set; }
}
當我創建一個新的OvertimeRule
並嘗試將其保存,EF首先生成此查詢:
INSERT [dbo].[Rules]([PeriodType], [SortOrder], [StatuteId], [IsActive], [StatuteID])
VALUES (@0, @1, @2, @3, @4, @5, @6, NULL)
正如你所看到的,EF增加了一個StatuteID
列添加到插入,該解決方案不存在於任何位置,並使其成爲無效的SQL查詢。
SQL然後理所當然地拋出: The column name 'StatuteId' is specified more than once in the SET clause. A column cannot be assigned more than one value in the same SET clause. Modify the SET clause to make sure that a column is updated only once. If the SET clause updates columns of a view, then the column name 'StatuteId' may appear twice in the view definition.
的映射是這樣的:
modelBuilder.Entity<Rule>().ToTable("Rules", TimmiSchemaName);
modelBuilder.Entity<Rule>().HasRequired(s => s.Statute).WithMany(s => s.Rules).HasForeignKey(r => r.StatuteId);
modelBuilder.Entity<OvertimeRule>().ToTable("OverTimeRules", TimmiSchemaName);
誰能告訴我什麼可能會引發這種行爲?
顯示您OvertimeRule定義請 – tede24
完成。沒有什麼有趣的,看到真的在這裏.. –