Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Creating Views using Code First #1952


User avatar
0
maharatha created

Hi All -

I am facing some serious slowness when Entityframework loads the data for the first time.

Any idea to make the initial load faster ?

Secondly I would like to use views , so would like to create a view using code first approach. Could you provide some guidance

Thanks, Partha


10 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The problem might be related to EF but it also might be related to your App pool in IIS. You can configure your app pool to always up in IIS manager. If that does not solve your problem, you can send timely keep alive request to your web app to keep it alive.

    For using views in code first approach, in order to create views you can first create an empty migration and then run your view create sql command in it.

    in order to use views in your repositories, you can search on the internet for that. You need to create a custom repository in your project. see <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Repositories#custom-repositories">http://aspnetboilerplate.com/Pages/Docu ... positories</a>

  • User Avatar
    0
    maharatha created

    Thank you sir !!!

    I am also evaluating cached db for model store as mentioned below :

    <a class="postlink" href="https://www.fusonic.net/en/blog/3-steps-for-fast-entityframework-6.1-code-first-startup-performance/">https://www.fusonic.net/en/blog/3-steps ... rformance/</a>

    can you throw some light on how I can implement this with respect to ABP

  • User Avatar
    0
    maharatha created

    i am more interested in Second Level Cache for Entity Framework 6.1

    <a class="postlink" href="https://efcache.codeplex.com/">https://efcache.codeplex.com/</a>

    Can you let me know where should I make the changes to have this implemented in my project.

    Thank you for all your help so far.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can create a configuration class and give it to your dbContext using DbConfigurationType attribute.

    [DbConfigurationType(typeof(MyDbConfiguration))]
    public class MyDbContext : AbpZeroDbContext<Tenant, Role, User>
    {
    .....
    }
    
  • User Avatar
    0
    maharatha created

    Thank You for the response.

    Now I found this ZZZ framework to be more useful for second level caching :

    // using Z.EntityFramework.Plus; // Don't forget to include this. var ctx = new EntitiesContext();

    // The first call perform a database round trip var countries1 = ctx.Countries.FromCache().ToList();

    // Subsequent calls will take the value from the memory instead var countries2 = ctx.Countries.FromCache().ToList();

    I am not sure how to use it as the repositories does provide the FromCache property.

  • User Avatar
    0
    maharatha created

    Can you help me on this ?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can create a custom repository and access dbContext in it. See <a class="postlink" href="http://aspnetboilerplate.com/Pages/Documents/Repositories#custom-repositories">http://aspnetboilerplate.com/Pages/Docu ... positories</a>. In that way, you might be able to use FromCache extension method of zzz Framework.

    If you want to use this option in generic repositories, you can try to write an extension method for generic IRepository. You can check this gist for that <a class="postlink" href="https://gist.github.com/hikalkan/74f624c0b42c78fb1619e92b3d1972f8">https://gist.github.com/hikalkan/74f624 ... 2b3d1972f8</a>.

    We haven't tried this framework with ABP or AspNet Zero, we will be very happy if you can share your results with us.

    Thanks.

  • User Avatar
    0
    maharatha created

    This is how I implemented :

    public static void BulkInsert<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository, IEnumerable<TEntity> entities) where TEntity : class, IEntity<TPrimaryKey>, new() { var enumerable = entities as IList<TEntity> ?? entities.ToList(); if (enumerable.Any()) { if (!Z.EntityFramework.Extensions.LicenseManager.ValidateLicense()) { throw new Exception("Invalid License!"); } var dbContext = repository.GetDbContext(); //dbContext.Set<TEntity>().AddRange(entities); dbContext.BulkInsert(enumerable); } }

    The BulkInsert is from Z.Entityframework and it works fine. It also has BulkInserAsync, but the moment I make the method Async and use Await operator I am losing my DbContext. Any reason ?

    I will also implement the Caching and update the thread

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share your method's async version ? Do you also get any errors with async version ? If so, please share it as well.

  • User Avatar
    0
    maharatha created

    Sure will share it in a short time. In the mean time I have been trying to write the extension method and I am kind of lost on the implementation :

    public static GetAll<TEntity> GetAllFromCache<TEntity, TPrimaryKey>(IRepository<TEntity, TPrimaryKey> repository)
              where TEntity : class, IEntity<TPrimaryKey>, new()
            {
    
                return repository.GetAll().FromCache();
            }
    

    Obviously the above one doesn't work.

    Could you help me on this.