Search This Blog

Thursday, July 7, 2016

Dealing with EF Connection Timeouts/Connection lost issues using Connection Resilience/Retry Logic built in support (EF onwards)

Upon facing connection timeouts/connection lost issues in working with EF, instead of trying brute force methods such as wrapping your connection instance in a for statement or alike, the best practice is to use Connection Resilience/Retry Logic (EF onwards) support, which provides more sophisticated retry strategy implementations, shipped in.

Rethrowing exceptions to preserve the stack while changing the exception's message?

A (deleted) question I posted yesterday, on the topic.

http://stackoverflow.com/q/38228681/1219280



Sunday, July 3, 2016

Adding objects from different schemas using Entity Framework with Oracle

At first, it looked like there was is no Entity Framework support to adding objects (say, tables) created in schemas other than the one used in the database connection.

But turned out it is possible. Stackoverflow will list some "workarounds" , but there is no need to - because SO lists one post that gives the exact solution for this problem, and it looks great.

Summarizing it:

You can either add more schemas under which search for objects to add during the creation of the Ado.net object (while adding the connection) - or by modifying the existing connection in the Server Explorer in Visual Studio.



If you are adding the ADO.NET object, you need the connection definition window above - and if you already have your ADO.NET object there, go to Visual Studio -> View -> Server Explorer, search for the desired connection under Data Connections and right click it. Then choose Modify. You will get the window above.

The steps to follow are:
  1. Select the Filter tab
  2. Click the 3 dots singled out, and add the schemas you want to be included by EF when searching for objects to add to the model.
  3. After you are done with step 1, click the Update button singled out in the screenshot.
That's it. You can right click your edmx file, click Update Model From Database, and you will now have, under Tables, the tables listed by their schema.

Tuesday, June 7, 2016

Deep clone example

Deep-cloning example:

 public class RechivHafrasha : IEqualityComparer<RechivHafrasha>
    {
        public decimal Schum { getset; }
        public decimal? Shiur { getset; }
 
        //public int? Status { get; private set; }
        public int Sug { getset; }
 
        public decimal Sachar { getprivate set; }
        public string MisparMezaheReshuma { getset; }
 
        public string MisparMezaheReshumaKodem { getset; }
 
        public RechivHafrasha()
        {
        }
 
        public RechivHafrasha(decimal schumHafrasha, decimal? shiurHafrasha, /*int? status,*/ int sugHafrasha, decimal sachar, string misparMezaheReshuma, string misparMezaheReshumaKodem)
        {
            Schum = schumHafrasha;
            Shiur = shiurHafrasha;
            //Status = status;
            Sug = sugHafrasha;
            Sachar = sachar;
            MisparMezaheReshuma = misparMezaheReshuma;
            MisparMezaheReshumaKodem = misparMezaheReshumaKodem;
        }
 
        /*
        * לצורך השוואת בין רשומות מאוחדות, כדי לא לשלוח כפילויות לפירעון
        */
 
        public string ID
        {
            get
            {
                return string.Format("{0}{1}{2}{3}{4}{5}",
                    this.Schum,
                    this.Shiur,
                    this.Sug,
                    this.Sachar,
                    this.MisparMezaheReshuma,
                    this.MisparMezaheReshumaKodem);
            }
        }
 
        public bool Equals(RechivHafrasha x, RechivHafrasha y)
        {
            return x.ID.Equals(y.ID);
        }
 
        public int GetHashCode(RechivHafrasha obj)
        {
            return obj.ID.GetHashCode();
        }
 
        public RechivHafrasha ShallowCopy()
        {
            var shallowCopy = this.MemberwiseClone() as RechivHafrasha;
            return shallowCopy;
        }
 
        public override string ToString()
        {
            return string.Format("[{0},{1},{2},{3},{4},{5}]", Schum, Shiur, Sug, Sachar, MisparMezaheReshuma, MisparMezaheReshumaKodem);
        }
    }
 
 
 
 
            List<RechivHafrasha> list = new List<RechivHafrasha>
            {
                new RechivHafrasha(10, 0.5m, 1, 1000, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(100, 0.7m, 2, 900, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(1000, 0.2m, 5, 80, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(10000, 0.41m, 4, 500, "7d2d272d7268326""2d2862g2g2d"),
                new RechivHafrasha(100000, 0.11m, 1, 15, "7d2d272d7268326""2d2862g2g2d")
            };
 
            List<RechivHafrasha> c1 = list.ToList();
            List<RechivHafrasha> c2 = new List<RechivHafrasha>(list);
            List<RechivHafrasha> c3 = list.ConvertAll<RechivHafrasha>(z => z.ShallowCopy());
 
            list[1].Schum = 0;
 
            Console.WriteLine(string.Join(",\n", list));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c1));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c2));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c3)); 
Output:

Sunday, May 22, 2016

Improving performance in working with EF



I stumbled across a SO post about performance, where the main hindrance was Entity Framwork/DB operations.

Here's a summary of the suggested approaches for performance improvement:

  •  Using Dapper or raw ADO.NET components.
  • Disabling EF change auto detection and validation on save flags.
  • Using BulkInsert
  • Instead of adding new entities inside a loop, collect all the entity objects to be added into a collection - and one you are finished issue a single [entity].AddRange(collection) to push all the new entities into the EF object (and afterwards performing a SaveChanges). It is stated that this is performance-wiser.
I myself was familiar with the 2nd and 3rd suggestions. Should give Dapper and AddRange a try.

Some performance comparison tests for the above can be found in the web, here is a nicely presented one.

See also Performance considerations for EF 4, 5 and 6.

Monday, April 4, 2016

Web Development Fundamental Concepts

I found today concise yet very good descriptions of fundamental concepts in web development, by the Mozilla Developer Network.
A must for any web developer (I stumbled across this way too late, I admit).

https://developer.mozilla.org/en-US/Learn/Common_questions

You will find great articles clarifying what is the internet, a web server, web pages, web sites and more.

Update:
Web Fundamentals from Google.