2

I have a EF code-first DbContext:

public class Db : IdentityDbContext<User>
{
    public Db(): base("Db")
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<Db, Migrations.Configuration>());
    }

    //content
    public DbSet<UrlEntity> ContentUrls { get; set; }
    public DbSet<Content> Contents { get; set; }
    public DbSet<Banner> Banners { get; set; }

    //item
    public DbSet<ItemCat> ItemCats { get; set; }
    public DbSet<Item> Items { get; set; }
    public DbSet<ItemImage> ItemImages { get; set; }

    //gallery
    public DbSet<GalleryCat> GalleryCats { get; set; }
    public DbSet<Gallery> Galleries { get; set; }

    //news 
    public DbSet<New> News { get; set; }

    protected override void OnModelCreating(DbModelBuilder mb)
    {
        //base
        mb.Configurations.Add(new TreeBaseConfig());
        mb.Configurations.Add(new CatConfig());
        mb.Configurations.Add(new UrlEntityConfig());

        //content
        mb.Configurations.Add(new ContentConfig());
        mb.Configurations.Add(new BannerConfig());

        //item 
        mb.Configurations.Add(new ItemCatConfig());
        mb.Configurations.Add(new ItemConfig());
        mb.Configurations.Add(new ItemImageConfig());

        //gallery
        mb.Configurations.Add(new GalleryCatConfig());
        mb.Configurations.Add(new GalleryConfig());

        //new
        mb.Configurations.Add(new NewConfig());

        //link

        //membership
        mb.Configurations.Add(new UserConfig());
        // mb.Configurations.Add(new UserInfoConfig());

        base.OnModelCreating(mb);
    }
}

and initialize and dispose db context in every controller:

public class HomeController : Controller
{
    Db _db = new Db();

    public ActionResult Aboutus() { return View(); }
    public ActionResult Contactus() { return View(new ContactusDTO()); }

    [...] // else action methods

    protected override void Dispose(bool disposing)
    {
        if (disposing)
            _db.Dispose();
        base.Dispose(disposing);
    }

Am I right in utilize and disposing db context correctly and effectively?

Actually my question is why http://www.hamkar-tamin.com take too time to loading?

  • a few data are in my db
  • technology I use: .net 4.5 EF 6.2 MVC 5.
  • my web site work on IIS Express well.

When I want to load pages or inserting, or some simple CRUD (on one table), but it it take sometimes about thirty second or more to do.

Update:

I also ping the domain. average round trip time: 623ms, maximum 830ms, minimum 390ms

update:

Loading page is fine. response from server is slow, not for first request. every take too long? witch things are here that i must consider?

**update: **

i find out the problem is on every queries on Db. sql server version is 2008 R2.

Mohammadreza
  • 3,139
  • 8
  • 35
  • 56
  • You are correct to dispose DbContext, can I ask if this is on Azure? Unless you have a premium setup on azure, it will have a cold start each time its accessed which causes delay. However, that said. It did not seem bad to me. Maybe be more specific about what is loading slow for you? – Lotok Jul 12 '14 at 13:21
  • Here is some tips to speed up EF, [Perfomance issue - .Count in MVC view](http://stackoverflow.com/questions/14370722/perfomance-issue-count-in-mvc-view/24467663#24467663) – Mohsen Esmailpour Jul 12 '14 at 13:25
  • tnx james, it's not on Azure, loading in every page take time i can't find out why, maybe it's because of my internet connection :(. but i browse it other location. the loading time was same – Mohammadreza Jul 12 '14 at 13:28
  • Can you show your EF initialization code? It should be in or referenced in global.asax. Also you said it was fine on your IIS Express. Where is it hosted now? Have you tried setting up IIS on your dev machine and test it away from the VS IIS Express? – Lotok Jul 12 '14 at 14:40
  • How long is too long? What is the time taken for a second call to the site? – Hargrovm Jul 12 '14 at 18:39
  • 1
    Is it slow only the first load, or every load on every page? I'm also suspicious of that MigrateToLatestVersion Initializer.. you probably don't want that in production all the time... – Erik Funkenbusch Jul 12 '14 at 19:36
  • I've just noticed your initializer is in your dbcontext constructor. It should only call once so belongs in your global.asax or in a class called by global.asax. I will part a code sample when I get to my laptop – Lotok Jul 13 '14 at 06:41
  • Dear @ErikFunkenbusch tnx, naturally is first time because of initializing application domain take more, but it's bad on every request. Yes, i actually don't need MigratyeToLastVersion Initializer in production, i exclud it – Mohammadreza Jul 13 '14 at 06:41
  • tnx @James, tnx got it. yes right is with you. – Mohammadreza Jul 13 '14 at 06:58
  • @Hargrovm about 20 second. – Mohammadreza Jul 13 '14 at 07:07

2 Answers2

1

As mentioned in the comments you need to remove your initializer from your constructor

public Db(): base("Db")
{
    Database.SetInitializer(new MigrateDatabaseToLatestVersion<Db,
                                      Migrations.Configuration>());
}

can be changed to

public Db(): base("Db")
{
}

Then you need to put your initializer into global.asax

    Database.SetInitializer(new MigrateDatabaseToLatestVersion<Db,
                                       Migrations.Configuration>());

in the application start.The initializer only needs to run once and it has an associated warm up time. By having it there on every call to DbContext each request has to initialize all over again.

Lotok
  • 4,517
  • 1
  • 34
  • 44
0

My database server,IIS Server is on one server so I change the Data Source from this:

ConnectionString="Data Source=[mydomain name].com ...

to:

ConnectionString="Data Source=(local) ...

and it works. when we determine domain name for datasource, finding the ip for that domain from a dns server take time

Mohammadreza
  • 3,139
  • 8
  • 35
  • 56