实体框架CodeFirst延迟经历 [英] Entity Framework CodeFirst delay experienced

查看:149
本文介绍了实体框架CodeFirst延迟经历的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解实体框架和我目前面临的,我需要大约10秒从数据库中检索数据或更新行,就好像我的代码实际上卡住了一段时间出了问题,即使调试它一切都正常了。



代码本身,其实如预期,除了这种延迟的作品。



搜索在谷歌和这里我并没有发现其他人有这个问题涉及到实体框架。



我想,也许它的东西与我的 CodeFirstMySQLEntities 类的构造函数,但不能肯定。



如果有人能为我提供指导,我将不胜感激。



这是主要代码:

 命名空间CodeFirstMySQL 
{
类节目
{
静态无效的主要(字串[] args)
{
userRepository userRepository =新userRepository();

userRepository.Update(克莱恩,中文别名);

//延迟经历这里

Console.WriteLine(完成);
到Console.ReadLine();
}
}
}

这是的DbContext代码:

 命名空间CodeFirstMySQL.Database 
{
公共类CodeFirstMySQLEntities:的DbContext
{
酒店的公共CodeFirstMySQLEntities():基地(CodeFirstMySQLEntities){}

公共DbSet<&的usermodel GT;用户{搞定;组; }
}
}

这是的usermodel代码:

 命名空间CodeFirstMySQL.Database.Models 
{
公共类的usermodel
{
[键, StringLength(100)]
公共字符串的firstName {搞定;组; }

[StringLength(100)]
公共字符串的lastName {搞定;组; }
}
}

这是库代码:

 命名空间CodeFirstMySQL.Database.Repositories 
{
公共类UserRepository
{
公共无效插入使用(用户的usermodel)
{
(VAR背景=新CodeFirstMySQLEntities())
{
context.Users.Add(用户);
context.SaveChanges();
}
}

公共无效使用删除(字符串的firstName)
{
(VAR背景=新CodeFirstMySQLEntities())
{
的usermodel用户= context.Users.FirstOrDefault(X => x.firstName ==的firstName);
context.Users.Remove(用户);
context.SaveChanges();
}
}

公共无效更新(字符串lastNameOld,串lastNameNew)
{
使用(VAR上下文=新CodeFirstMySQLEntities())
{
的usermodel用户= context.Users.FirstOrDefault(X => x.lastName == lastNameOld);
user.lastName = lastNameNew;
context.SaveChanges();
}
}

公众的IList<&的usermodel GT; GetUsers()
{
使用(VAR上下文=新CodeFirstMySQLEntities())
{
返回context.Set<&的usermodel GT;()了ToList()。
}
}
}
}



连接字符串:

 <&是connectionStrings GT; 
<添加名称=CodeFirstMySQLEntities的connectionString =服务器=本地主机;数据库= CodeFirst; UID =根; PWD =的providerName =MySql.Data.MySqlClient/>
< /&是connectionStrings GT;


解决方案

延迟几乎可以肯定是由于时间的实体框架需要火起来。您可以通过尝试退出代码之前第二次更新证实了这一点。



以下摘录说明了这是怎么回事




示范缓存



有参与在发现的模型,处理数据注解和应用流畅API配置一定的成本。为了避免每一个派生的DbContext实例化模型中的第一初始化过程中缓存时间承担这笔费用。缓存模型,然后再使用各相同的派生上下文中相同的AppDomain构造时间



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>

解决方案

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.

这篇关于实体框架CodeFirst延迟经历的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆