【问题标题】:System.Runtime.Serialization.SerializationException: Unable to find assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxx'System.Runtime.Serialization.SerializationException:找不到程序集 'EntityFramework,Version=6.0.0.0,Culture=neutral,PublicKeyToken=xxx'
【发布时间】:2015-09-15 02:26:35
【问题描述】:

我有一个使用Entity Framework 6MVVM - WPF Browser Application 应用程序。我正在尝试删除我的 dataGrid 中的一行。我可以删除一行,但是当我尝试删除第二行时,出现以下错误:

System.Runtime.Serialization.SerializationException:找不到 程序集 'EntityFramework,版本 = 6.0.0.0,文化 = 中性, PublicKeyToken=xxx'。

ViewModelBase:

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}" />

App.config:

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

【问题讨论】:

  • 您是否通过 NuGet 安装了 EF6?
  • 是的,我可以显示更新,有时还可以删除一行。
  • 调试应用的时候,到底哪里抛出了异常?
  • 在 ctx.SaveChanges();

标签: c# wpf entity-framework serialization mvvm


【解决方案1】:

我已经对您的代码进行了全面测试,删除所有 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()));

第二种解决方案可能是:

打开

工具 > Nuget 包管理器 > 包管理器控制台

然后运行

install-package entityframework -version 6.0.0.0

尝试此处发布的解决方案:

Why is Entity Framework 6.1.3 throwing a "Could not load type 'System.Data.Entity.Infrastructure.TableExistenceChecker'"

【讨论】:

  • 感谢您的帮助。我确保选择了一行并按照您所做的更改,但仍然有相同的错误。
  • 不,一样。我在我的问题中添加了 App.config。也许我应该指定我的应用程序是 WPF 浏览器应用程序。
  • 进一步改进了答案
  • 好的,我的错误应该来自我在 GAC 中没有 Entityframework.dll 的事实。但是在卸载和安装时,GAC 中没有创建任何内容。
【解决方案2】:

我看到这个问题是在随机环境下发生的。例如,昨天我运行了 Windows Update 并安装了 27 个附加更新。第二天,这个错误开始每 15 分钟左右抛出一次。运行的软件没有改变。

似乎 Windows 更新正在等待等待重启。确保服务器满意,这个错误就消失了......

【讨论】:

    猜你喜欢
    • 2013-02-27
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多