Question

Is there any difference when i use this code:

    public bool Insert(SomeEntity entity)
    {
        bool result = false;
        try
        {
            using (var db = new MyEntities())
            {
                db.AddToSomeEntity(entity);
                db.SaveChanges();
                result = true;
            }
        }
        catch (Exception e)
        {
            //
        }
        return result;
    }

and this:

    public bool Insert(SomeEntity entity)
    {
        bool result = false;
        using (var db = new MyEntities())
        {
            try
            {
                db.AddToSomeEntity (entity);
                db.SaveChanges();
                result = true;
            }
            catch (Exception e)
            {
                //
            }
        }
        return result;
    }

Does it affect performance?

/This string was added in order to satisfy SO submit validation rule./

Was it helpful?

Solution

I do not think it violates any rules it the idea at what point you want to send your context to garbage collection i would recommend using the second method since it more concrete and you will send the object to garbage collection when your function was totally executed otherwise you might be getting an exception where you try to use the context while it no longer available

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top