尝试使用EntityFramework.Extensions进行删除,我有一个案例,我从标题中得到错误。这是场景:
public class AList
{
[Key]
public int Id { get; set; }
[Column]
public int XId { get; set; }
}
public abstract class X
{
[Key]
public int Id { get; set; }
public ICollection<AList> TheAList { get; set; }
}
public class Y : X
{
[Column("TheId")]
public int? SomeId { get; set; }
}
public class Z : X
{
[Column("TheId")]
public int? SomeIdZ { get; set; }
}
这是映射:
modelBuilder.Entity<X>()
.HasKey(t => t.Id)
.Map<Y>(t => t.Requires("XType").HasValue(1))
.Map<Z>(t => t.Requires("XType").HasValue(2));
modelBuilder.Entity<X>()
.HasMany(t => t.TheAList)
.WithRequired()
.HasForeignKey(t => t.XId);
这就是我如何删除行:
db.XTable.Where(t => t.Id == Id).Delete();
我的设置有什么问题?我这样做很好:
db.AListTable.Where(t => t.XId == Id).Delete();
答案 0 :(得分:2)
它可能是EntityFramework.Extended中的错误,在这种情况下您可以:
可能的解决方法:
// not ideal, because object(s) are fetched from db
// I assume that you use the library to prevent this situation.
var existing = db.XTable.SingleOrDefault(t => t.Id == Id);
if (existing != null)
db.XTable.Remove(existing);
// without fetching the object from db
db.Database.ExecuteSqlCommand("DELETE [XTable] WHERE Id = @Id", new SqlParameter("Id", Id));
无论哪种方式,情况都有点糟糕;)