我使用MVVM - WPF Browser Application
创建了Entity Framework 6
个应用程序。我试图删除dataGrid中的一行。我可以删除一行,但是当我尝试删除一行时,我有以下错误:
System.Runtime.Serialization.SerializationException:无法找到 assembly' EntityFramework,Version = 6.0.0.0,Culture = neutral, 公钥= XXX'
public class CommandBase<T> : INotifyPropertyChanged
{
#region "INotifyPropertyChanged members"
public event PropertyChangedEventHandler PropertyChanged;
//This routine is called each time a property value has been set.
//This will //cause an event to notify WPF via data-binding that a change has occurred.
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private ObservableCollection<T> collection;
public ObservableCollection<T> Collection
{
get
{
if (collection == null)
{
Get();
}
return collection;
}
set { collection = value;
OnPropertyChanged("Collection"); }
}
private T _selected;
public T Selected
{
get { return _selected; }
set { _selected = value;
OnPropertyChanged("Selected"); }
}
private ICommand getCommand;
private ICommand saveCommand;
private ICommand removeCommand;
public ICommand GetCommand
{
get
{
return getCommand ?? (getCommand = new RelayCommand(Get,CanGet));
}
}
protected virtual bool CanGet()
{
return true;
}
protected virtual void Get()
{
//return true;
}
public ICommand SaveCommand
{
get
{
return saveCommand ?? (saveCommand = new RelayCommand(Save, CanSave));
}
}
protected virtual void Save()
{
//return true;
}
protected virtual bool CanSave()
{
return true;
}
public ICommand DeleteCommand
{
get
{
return removeCommand ?? (removeCommand = new RelayCommand(Delete,CanDelete));
}
}
protected virtual void Delete()
{
}
protected virtual bool CanDelete()
{
if (Selected != null)
return true;
else
return false;
}
}
public class SupplierViewModel : CommandBase<foodSupplier>
{
public Context ctx = new Context();
protected override void Get()
{
ctx.foodSuppliers.ToList().ForEach(supplier => ctx.foodSuppliers.Local.Add(supplier));
Collection = ctx.foodSuppliers.Local;
}
protected override bool CanGet()
{
return true;
}
protected override void Save()
{
foreach (foodSupplier item in Collection)
{
if (ctx.Entry(item).State == System.Data.Entity.EntityState.Added)
{
ctx.foodSuppliers.Add(item);
}
}
ctx.SaveChanges();
}
protected override void Delete()
{
var id = Selected;
var supp = (from s in ctx.foodSuppliers
where s.idfoodSupplier == id.idfoodSupplier
select s).SingleOrDefault();
ctx.foodSuppliers.Remove(supp);
ctx.SaveChanges();
Collection.Remove(supp);
}
protected virtual bool CanDelete()
{
return true;
}
}
<DataGrid x:Name="dataGrid"
Margin="5"
ItemsSource="{Binding Collection}"
AutoGenerateColumns="False"
SelectedItem="{Binding Selected, Mode=TwoWay}"
SelectionMode="Extended"
SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn x:Name="dataGridTextColumn"
Header="Supplier"
Binding="{Binding idfoodSupplier, UpdateSourceTrigger=PropertyChanged}"
Visibility="Hidden" />
<DataGridTextColumn Header="Supplier"
Binding="{Binding supplier, UpdateSourceTrigger=PropertyChanged}" />
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding GetCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</DataGrid>
<Button Height="Auto"
Width="Auto"
Content="Delete"
Command="{Binding DeleteCommand}" />
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="MySql.Data.Entity.MySqlConnectionFactory, MySql.Data.Entity.EF6" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
<connectionStrings>
<add name="xx" connectionString="xxx" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
答案 0 :(得分:0)
我已经对您的代码进行了全面测试,并且可以正常删除所有5行,甚至是最后一行。
您的代码需要先选择一行,确保在DataGrid中选择一行,然后删除该行。只有当用户在DataGrid中选择一行时,您才可以禁用命令按钮并启用它。
我对您的代码进行了以下更改:
public ICommand GetCommand
{
get
{
return getCommand ?? (getCommand = new RelayCommand(p=>this.Get(), p=>this.CanGet()));
}
}
在其他命令中也是如此:
return saveCommand ?? (saveCommand = new RelayCommand(p=>this.Save(), p=>this.CanSave()));
并在
return removeCommand ?? (removeCommand = new RelayCommand(p=>this.Delete(), p=>this.CanDelete()));
第二个解决方案可能是:
打开
工具&gt; Nuget包管理器&gt;包管理器控制台
然后运行
install-package entityframework -version 6.0.0.0
尝试在此处发布的解决方案:
答案 1 :(得分:0)
我发现这个问题是在随机背景下发生的。例如,昨天我运行了Windows Update并安装了27个额外的更新。第二天,这个错误开始每15分钟左右抛出一次。正在运行的软件没有改变。
似乎Windows更新正在等待挂起的重启。确保服务器很满意,这个错误就消失了......