Tag: entity framework

EF codefirst:我应该初始化导航属性吗?

我曾经看过一些书(例如编程entity framework代码第一个Julia Lerman )定义了他们的领域类(POCO),没有像以下导航属性的初始化: public class User { public int Id { get; set; } public string UserName { get; set; } public virtual ICollection<Address> Address { get; set; } public virtual License License { get; set; } } 一些其他书籍或工具(例如Entity Framework Power Tools )在生成POCO时会初始化该类的导航属性,如: public class User { public User() { this.Addresses = new IList<Address>(); […]

我如何查询entity framework中的空值?

我想执行这样的查询 var result = from entry in table where entry.something == null select entry; 并获得生成的IS NULL 。 编辑:在前两个答案后,我觉得有必要澄清,我正在使用entity framework,而不是LINQ到SQL。 object.Equals()方法在EF中似乎不起作用。 编辑2:上述查询按预期工作。 它正确地生成IS NULL 。 我的生产代码是 value = null; var result = from entry in table where entry.something == value select entry; 生成的SQL是something = @p; @p = NULL something = @p; @p = NULL 。 EF似乎正确地翻译了常量expression式,但是如果涉及一个variables,它就像正常的比较那样对待它。 […]

entity framework:已经有一个开放的DataReader与这个Command相关联

我正在使用entity framework,偶尔我会得到这个错误。 EntityCommandExecutionException {"There is already an open DataReader associated with this Command which must be closed first."} at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands… 即使我没有做任何手动连接pipe理。 这个错误间歇发生。 触发错误的代码(为便于阅读而缩短): if (critera.FromDate > x) { t= _tEntitites.T.Where(predicate).ToList(); } else { t= new List<T>(_tEntitites.TA.Where(historicPredicate).ToList()); } 使用Dispose模式,以便每次打开新的连接。 using (_tEntitites = new TEntities(GetEntityConnection())) { if (critera.FromDate > x) { t= _tEntitites.T.Where(predicate).ToList(); } else { t= […]

EntityFramework中的ObjectSet包含多less个可用于保持性能?

我正在使用以下的LINQ查询我的个人资料页面: var userData = from u in db.Users .Include("UserSkills.Skill") .Include("UserIdeas.IdeaThings") .Include("UserInterests.Interest") .Include("UserMessengers.Messenger") .Include("UserFriends.User.UserSkills.Skill") .Include("UserFriends1.User1.UserSkills.Skill") .Include("UserFriends.User.UserIdeas") .Include("UserFriends1.User1.UserIdeas") where u.UserId == userId select u; 它有一个很长的对象图,并使用许多包括。 它现在运行的很完美,但是当网站有很多用户时,会不会影响性能呢? 我应该以其他方式做吗?

我怎样才能得到在entity framework中插入实体的Id?

我在Asp.net的entity framework中遇到问题。 每当我向数据库添加一个对象时,我想获得Id值。 我该怎么做?

entity framework:一个数据库,多个DbContexts。 这是一个坏主意吗?

我迄今为止的印象是,DbContext是为了代表你的数据库,因此,如果你的应用程序使用一个数据库,你只需要一个DbContext。 但是,有些同事想把function区分成单独的DbContext类。 我相信这是来自一个好地方 – 希望保持代码更清洁 – 但似乎是不稳定的。 我的直觉告诉我这是一个坏主意,但不幸的是我的直觉并不是devise决定的充分条件。 所以我正在寻找A)具体的例子,为什么这可能是一个坏主意,或者B)保证这一切都会运行得很好。

entity framework – 包含多级属性

Include()方法在对象列表上工作得非常好。 但是如果我需要深入两层呢? 例如,下面的方法将返回ApplicationServers与这里显示的包含的属性。 但是,ApplicationsWithOverrideGroup是另一个容纳其他复杂对象的容器。 我也可以在该属性上执行Include()吗? 或者我怎么能得到该属性完全加载? 就目前而言,这种方法: public IEnumerable<ApplicationServer> GetAll() { return this.Database.ApplicationServers .Include(x => x.ApplicationsWithOverrideGroup) .Include(x => x.ApplicationWithGroupToForceInstallList) .Include(x => x.CustomVariableGroups) .ToList(); } 只填充Enabled属性(如下),而不填充Application或CustomVariableGroup属性(如下)。 我如何做到这一点? public class ApplicationWithOverrideVariableGroup : EntityBase { public bool Enabled { get; set; } public Application Application { get; set; } public CustomVariableGroup CustomVariableGroup { get; set; } }

假的DbContext的entity framework4.1来testing

我正在使用这个教程来伪造我的DbContext并testing: http ://refactorthis.wordpress.com/2011/05/31/mock-faking-dbcontext-in-entity-framework-4-1-with-a-generic -repository / 但是我必须改变FakeMainModuleContext实现在我的控制器中使用: public class FakeQuestiona2011Context : IQuestiona2011Context { private IDbSet<Credencial> _credencial; private IDbSet<Perfil> _perfil; private IDbSet<Apurador> _apurador; private IDbSet<Entrevistado> _entrevistado; private IDbSet<Setor> _setor; private IDbSet<Secretaria> _secretaria; private IDbSet<Pesquisa> _pesquisa; private IDbSet<Pergunta> _pergunta; private IDbSet<Resposta> _resposta; public IDbSet<Credencial> Credencial { get { return _credencial ?? (_credencial = new FakeDbSet<Credencial>()); } set { […]

如何创buildLINQexpression式树来select一个匿名types

我想使用expression式树dynamic地生成下面的select语句: var v = from c in Countries where c.City == "London" select new {c.Name, c.Population}; 我已经研究出如何生成 var v = from c in Countries where c.City == "London" select new {c.Name}; 但我似乎无法find一个构造函数/重载,让我指定多个属性在我selectlambda。

EF Code First中的小数精度和小数位

我正在尝试这种代码优先的方法,但我现在发现一个System.Decimaltypes的属性映射到十进制types(18,0)的sql列。 如何设置数据库列的精度?