已删除的计数始终返回0

时间:2013-10-01 11:42:58

标签: c# entity-framework entity-framework-5

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

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

然后我使用canExecute:

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

我的问题是,计数总是返回ListaTipoAsociado少删除的ítems,这意味着

如果MyList.count = 5,并且用户标记为删除2Ítems,则此查询返回0MyList.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; }
}

0 个答案:

没有答案