0

我有一个允许用户将记录标记为已删除的 MVVM 应用程序

public void DeleteRecord(object p)
{
    db.entry((MyEntityCast)p).state = EntityState.Deleted;
    ((DelegateCommand)ICommandSave).RaiseCanExecuteChanged();
}

然后我ICommandSave用这个canExecute保存:

public bool canExecuteSave(object param)
{
    decimal count = (from c in MyList
                      select c).Count(x=> db.Entry(x).State != EntityState.Unchanged);     
    return count !=0;
}

我的问题是 count 总是返回ListaTipoAsociado较少的已删除项目,这意味着

如果MyList.count = 5,并且用户标记删除 2 个项目,则此查询返回0,并将MyList.count新值设置为3

我的问题是?

为什么如果记录仅marked用于删除(尚未删除),我无法在查询中访问它们?

我缺少一个EF设置吗?

更新 2: MyList 定义

private ObservableCollection<MyEntity> _MyList;
public ObservableCollection<MyEntity> MyList
{
    get { return _MyList; }
    set { SetProperty(ref _MyList, value); }
}

public constructor()
{
    var Q= from ta in db.MyEntity
           select ta;
    MyList= new ObservableCollection<MyEntity>(Q.ToList()); 
}

更新 3:类定义

public partial class MyEntity
{
    public MyEntity()
    {
        this.Childs = new HashSet<MyEntity2>();
    }

    public int IntTipoAsociado { get; set; }
    public string StrDescripcionTipoAsociado { get; set; }

    public virtual ICollection<MyEntity2> MyEntity2{ get; set; }
}

和上下文:

public partial class MyEntities : DbContext
{
    public MyEntities()
        : base("name=MyEntities")
    {
    }

    public MyEntities(string connectioString)
        : base(connectioString)
    {
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    public DbSet<MyEntity> MyEntity { get; set; }
    public DbSet<MyEntity2> MyEntity { get; set; }
}
4

0 回答 0