/// <summary> /// Testable AllMainRows /// </summary> /// <typeparam name="M"></typeparam> /// <param name="db">The database.</param> /// <param name="filter">encrypt filter</param> /// <returns></returns> public static IEnumerable <Main> AllMainRows <M>(ASP_DBEntities db, Func <byte[], byte[]> filter = null) where M : class { var all = from m in db.Main select m; return(from m in all.AsEnumerable() where m.clsid == Clsid.Id(typeof(M)) && m.GetInstance <M>(filter) != null // could be encrypted select m); }
/// <summary> /// Testable SaveMain /// </summary> /// <typeparam name="M"></typeparam> /// <param name="db">The database.</param> /// <param name="instance">The instance.</param> /// <param name="session">The session.</param> /// <param name="filter">encrypt filter</param> /// <returns></returns> public static Guid SaveMain <M>(ASP_DBEntities db, M instance, Guid?session, Func <byte[], byte[]> filter = null) where M : class { var query = from m in db.Main where m.session == session select m; var main = query.FirstOrDefault(); if (main == null) { main = new Main(); main.clsid = Clsid.Id(instance); db.Main.Add(main); // INSERT } main.SetInstance(instance, filter); db.SaveChanges(); return(main.session); // get the new session guid set by the db on insert }
/// <summary> /// Returns the (unencrypted!) literal INSERT string of the loaded object /// for manually exporting session dumps. /// </summary> /// <returns> /// SQL INSERT string /// </returns> public string InsertSQL() { Trace.Assert(this.main != null, "Explicit serialization with SetInstance(controlStorage.Main) required beforehand"); var clsid = Clsid.Id(this.mainInstance); // throws if no Clsid attribute present // Let the future consumer SQL Server encode the string representation of the byte[] string hex = String.Empty; var query = "SELECT CONVERT(VARCHAR(MAX), @main, 1) AS [hex]"; using (var db = new ASP_DBEntities()) { var param = new SqlParameter("main", this.main); hex = db.Database.SqlQuery <String>(query, param).FirstOrDefault(); } // Format according to get copy-pasted into Management Studio return(String.Format("INSERT INTO Main (clsid, main) SELECT '{0}', {1}\n" + "SELECT session FROM Main WHERE mainid = @@IDENTITY\n", clsid, hex)); }