Entity Framework CodeFirst delay experienced

我们两清 提交于 2019-11-28 09:58:35

问题


I'm learning about Entity Framework and I am currently facing a problem where I take about 10 seconds to retrieve data from a database or to update a row, as if my code were actually stuck for a period of time, even though debugging it everything went normal.

The code itself, actually works as expected, besides this delay.

Searching on Google and here I did not found other people with this issue related to Entity Framework.

I think that maybe it's something to do with my CodeFirstMySQLEntities class constructor, but not sure.

If someone could provide me with a guidance, I would appreciate.

This is the main code:

namespace CodeFirstMySQL
{
    class Program
    {
        static void Main(string[] args)
        {
            UserRepository userRepository = new UserRepository();

            userRepository.Update("Klein", "OtherName");

            //delay experienced here

            Console.WriteLine("done");
            Console.ReadLine();
        }
    }
}

This is the DbContext code:

namespace CodeFirstMySQL.Database
{
    public class CodeFirstMySQLEntities : DbContext
    {
        public CodeFirstMySQLEntities() : base("CodeFirstMySQLEntities") { }

        public DbSet<UserModel> Users { get; set; }
    }
}

This is the UserModel code:

namespace CodeFirstMySQL.Database.Models
{
    public class UserModel
    {
        [Key, StringLength(100)]
        public string firstName { get; set; }

        [StringLength(100)]
        public string lastName { get; set; }
    }
}

This is the repository code:

namespace CodeFirstMySQL.Database.Repositories
{
    public class UserRepository
    {
        public void Insert(UserModel user)
        {
            using (var context = new CodeFirstMySQLEntities())
            {
                context.Users.Add(user);
                context.SaveChanges();
            }
        }

        public void Delete(string firstName)
        {
            using (var context = new CodeFirstMySQLEntities())
            {
                UserModel user = context.Users.FirstOrDefault(x => x.firstName == firstName);
                context.Users.Remove(user);
                context.SaveChanges();
            }
        }

        public void Update(string lastNameOld, string lastNameNew)
        {
            using (var context = new CodeFirstMySQLEntities())
            {
                UserModel user = context.Users.FirstOrDefault(x => x.lastName == lastNameOld);
                user.lastName = lastNameNew;
                context.SaveChanges();
            }
        }

        public IList<UserModel> GetUsers()
        {
            using (var context = new CodeFirstMySQLEntities())
            {
                return context.Set<UserModel>().ToList();
            }
        }
    }
}

Connection String:

<connectionStrings>
    <add name="CodeFirstMySQLEntities" connectionString="Server=localhost; Database=CodeFirst; Uid=root; Pwd=" providerName="MySql.Data.MySqlClient"/>
  </connectionStrings>

回答1:


The delay is almost certainly due to the time Entity Framework takes to fire up. You can confirm this by trying a second update before exiting your code.

The following excerpt explains what is going on

Model Caching

There is some cost involved in discovering the model, processing Data Annotations and applying fluent API configuration. To avoid incurring this cost every time a derived DbContext is instantiated the model is cached during the first initialization. The cached model is then re-used each time the same derived context is constructed in the same AppDomain.



来源:https://stackoverflow.com/questions/15507067/entity-framework-codefirst-delay-experienced

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!