c# - GZip header 中的魔数(Magic Number)不正确。确保您传递的是 GZip 流

标签 c# mysql winforms entity-framework io

我在 WinForm 应用程序和 Mysql 数据库中工作,所以当我在 MySql Workbench 上运行数据库脚本时会显示此消息

enter image description here

我点击“确定”并显示脚本

enter image description here

运行脚本并创建数据库后,当我运行我的 WinForm 应用程序时,在第一次查询数据库时显示此异常:

var permissions_in_db = db.Permissions
                            .Where(a => a.Name == s)
                            .Count();

System.IO.InvalidDataException 不受控制:“GZip header 中的魔数(Magic Number)不正确。确保您传递的是 GZip 流。”

这是痕迹

System.IO.InvalidDataException not be controlled
  HResult = -2146233087
  Message = The magic number in GZip header is not correct. Make sure you are passing a GZip stream.
  Source = System
  StackTrace :
       in System.IO.Compression.GZipDecoder.ReadHeader ( InputBuffer input)
       in System.IO.Compression.Inflater.Decode ( )
       in System.IO.Compression.Inflater.Inflate ( Byte [ ] bytes , Int32 offset , Int32 length)
       in System.IO.Compression.DeflateStream.Read ( Byte [ ] array , Int32 offset , Int32 count)
       in System.IO.Compression.GZipStream.Read ( Byte [ ] array , Int32 offset , Int32 count)
       in System.Xml.XmlTextReaderImpl.InitStreamInput ( baseUri Uri , String baseUriStr , Stream stream , Byte [ ] bytes , Int32 byteCount , Encoding encoding)
       in System.Xml.XmlTextReaderImpl.FinishInitStream ( )
       in System.Xml.XmlReaderSettings.CreateReader (Stream input , baseUri Uri , String baseUriString , XmlParserContext inputContext )
       in System.Xml.XmlReader.Create (Stream input , XmlReaderSettings settings , String baseUri )
       in System.Xml.Linq.XDocument.Load (Stream stream, LoadOptions options)
       in System.Data.Entity.Migrations.Edm.ModelCompressor.Decompress ( Byte [ ] bytes )
       in System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel (String & migrationId )
       in System.Data.Entity.Migrations.History.HistoryRepository.GetLastModel ( )
       in System.Data.Entity.Internal.InternalContext.QueryForModel ( )
       in System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel ( InternalContext internalContext , ModelHashCalculator modelHashCalculator , Boolean throwIfNoMetadata )
       in System.Data.Entity.Internal.InternalContext.CompatibleWithModel (Boolean throwIfNoMetadata )
       in System.Data.Entity.Database.CompatibleWithModel (Boolean throwIfNoMetadata )
       in System.Data.Entity.DropCreateDatabaseIfModelChanges ` 1.InitializeDatabase ( tcontext context)
       in System.Data.Entity.Database . < > c__DisplayClass2 ` 1. <SetInitializerInternal> b__0 ( DbContext c )
       in System.Data.Entity.Internal.InternalContext . < > c__DisplayClass8 . <PerformDatabaseInitialization> b__6 ( )
       in System.Data.Entity.Internal.InternalContext.PerformInitializationAction ( Action action)
       in System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization ( )
       in System.Data.Entity.Internal.LazyInternalContext . <InitializeDatabase> b__4 ( InternalContext c )
       in System.Data.Entity.Internal.RetryAction ` 1.PerformAction ( TInput input)
       in System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction ( Action ` 1 action )
       in System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase ( )
       in System.Data.Entity.Internal.InternalContext.Initialize ( )
       in System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType (Type entityType )
       in System.Data.Entity.Internal.Linq.InternalSet 1.Initialize ` ( )
       in System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext ` ( )
       in System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider ` ( )
       in System.Linq.Queryable.Where [ TSource ] (IQueryable ` 1 source , Expression ` 1 predicate )
       in RoleCreatorV5_net4.Login.button1_Click (Object sender, EventArgs e) in c: \ Users \ Rafa \ Downloads \ 1 \ final versions \ RoleCreatorV5_net4 \ Login.cs : line 30
       in System.Windows.Forms.Button.OnMouseUp ( MouseEventArgs mevent )
       at System.Windows.Forms.Control.WmMouseUp (Message & m , MouseButtons button , Int32 clicks)
       at System.Windows.Forms.Control.WndProc (Message & m )
       in System.Windows.Forms.ButtonBase.WndProc (Message & m )
       in System.Windows.Forms.Button.WndProc (Message & m )
       in System.Windows.Forms.NativeWindow.DebuggableCallback (IntPtr hWnd , Int32 msg , IntPtr wparam , IntPtr lparam )
       in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW (MSG & msg)
       in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID , Int32 reason , Int32 pvLoopData )
       in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner (Int32 reason , ApplicationContext context)
       in System.Windows.Forms.Application.ThreadContext.RunMessageLoop (Int32 reason , ApplicationContext context)
       in RoleCreatorV5_net4.Program.Main () in c: \ Users \ Rafa \ Downloads \ 1 \ final versions \ RoleCreatorV5_net4 \ Program.cs : line 70
       in System.AppDomain._nExecuteAssembly ( RuntimeAssembly assembly, String [ ] args)
       in System.Runtime.Hosting.ApplicationActivator.CreateInstance ( ActivationContext activationContext , String [ ] activationCustomData )
       in Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone ( )
       in System.Threading.ExecutionContext.RunInternal ( ExecutionContext executionContext , ContextCallback callback , Object state , Boolean preserveSyncCtx )
       at System.Threading.ExecutionContext.Run ( ExecutionContext executionContext , ContextCallback callback , Object state , Boolean preserveSyncCtx )
       at System.Threading.ExecutionContext.Run ( ExecutionContext executionContext , ContextCallback callback , Object state )
       in System.Threading.ThreadHelper.ThreadStart ( )
  InnerException :

这是 Program.cs 中的代码

[STAThread]
        static void Main()
        {

            Database.SetInitializer<MySqlContext>(new DropCreateDatabaseIfModelChanges<MySqlContext>());


            var connString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ToString();

            using (var db = new MySqlContext())
            {

                #region Load CheckedList1

                const string f = "/Users/Rafa/Documents/Proyecto NS/SavedList.txt";
                List<string> lines = new List<string>();
                using (StreamReader r = new StreamReader(f))
                {
                    string line;
                    while ((line = r.ReadLine()) != null)
                    {
                        lines.Add(line);
                    }
                }

                foreach (string s in lines)
                {

                    var permissions_in_db = db.Permissions
                        .Where(a => a.Name == s)
                        .Count();
                    if (permissions_in_db < 1)
                    {
                        if (s!="")
                        {
                            db.Permissions.AddOrUpdate(new Permission
                            {
                                Name = s
                            });
                            db.SaveChanges();    
                        }

                    }
                }
                #endregion

                db.SaveChanges();
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Login());
        }
    }

我真的希望有人能帮助我!

最佳答案

是的,__migrationhistory 模型列中有一些数据,拉丁编码弄乱了数据。要进行救援,请在不打开文件的情况下运行脚本。使用新的 MySql Workbench,有一个按钮“运行 SQL 脚本”,它允许您在不指定编码的情况下运行。 enter image description here

关于c# - GZip header 中的魔数(Magic Number)不正确。确保您传递的是 GZip 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23526146/

相关文章:

c# - 鼠标移动事件的问题

c# - 如何使 Visual Studio 2010 将文件扩展名识别为 C#/ASPX/C/C++ 文件?

mysql 查询多个值

mysql - 如何从表中获取最低值

mysql - 如何在某些表上使用左连接编写正确的 sql?

C# 强制表单焦点

javascript - anchor 标记上的 JQuery click() 未从 WebBrowser 控件内的注入(inject)脚本触发

c# - 为什么我的休息不起作用?

c# - WCF NetTcpBinding 是否提供持久连接?

winforms - UserControl 如何 self 毁灭?