1

尝试使用 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();
4

1 回答 1

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));

不管怎样,情况有点糟糕;)

于 2014-05-16T16:24:21.950 回答