Tag: entity framework

如何计算关联实体而不在entity framework中获取它们

我一直在思考这个问题,所以我认为这将是值得使用我的第一个堆栈溢出后询问。 想象一下,我有一个关联消息列表的讨论: DiscussionCategory discussionCategory = _repository.GetDiscussionCategory(id); discussionCategory.Discussions是当前未加载的讨论实体列表。 我想要的是能够迭代讨论类别中的讨论,并说出每个讨论中有多less个消息,而不用提取消息数据。 当我必须加载讨论和消息之前,我已经尝试了这个,所以我可以做这样的事情: discussionCategory.Discussions.Attach(Model.Discussions.CreateSourceQuery().Include("Messages").AsEnumerable()); foreach(Discussion discussion in discussionCategory.Discussions) { int messageCount = discussion.Messages.Count; Console.WriteLine(messageCount); } 这对我来说似乎相当低效,因为我从数据库中获取数百个可能的消息体,并将其保留在内存中,当我只想为表示目的计数它们的数量时。 我看到了一些涉及这个问题的问题,但他们似乎没有直接解决这个问题。 提前感谢您对这个主题的任何想法。 更新 – 一些更多的代码请求: public ActionResult Details(int id) { Project project = _repository.GetProject(id); return View(project); } 然后在视图中(只是为了testing): Model.Discussions.Load(); var items = from d in Model.Discussions select new { Id = d.Id, Name […]

在EF4中为表列名称指定一个不同的名称

我正在使用CodeFirst的EF4 public class People : DbContext { public DbSet<Human> Humans { get; set; } public DbSet<Child> Children { get; set; } } 目前,EF在数据库中查找Human表。 我怎样才能指定它来寻找Humans呢?

为什么我的entity framework代码第一个代理收集为空,为什么我不能设置它?

我正在使用DBContext并有两个类的属性都是虚拟的。 我可以看到在debugging器中,当我查询上下文时,我正在获取代理对象。 但是,当我尝试添加它时,集合属性仍为空。 我认为代理将确保集合被初始化。 因为我的Poco对象可以在其数据上下文之外使用,所以我在构造函数中添加了一个空集合的检查,并在必要时创build它: public class DanceStyle { public DanceStyle() { if (DanceEvents == null) { DanceEvents = new Collection<DanceEvent>(); } } … public virtual ICollection<DanceEvent> DanceEvents { get; set; } } 这工作之外的数据上下文,但如果我检索一个对象使用查询,虽然testing是真实的,当我尝试设置它时,我得到以下exception:'属性'DanceEvents'types'DanceStyle_B6089AE40D178593955F1328A70EAA3D8F0F01DDE9F9FBD615F60A34F9178B94'不能被设置,因为集合已经被设置为一个EntityCollection。 我可以看到它是空的,我不能添加到它,但我也不能将其设置为一个集合,因为代理说它已经设置。 所以我不能使用它。 我很困惑。 这里是DanceEvent类: public class DanceEvent { public DanceEvent() { if (DanceStyles == null) { DanceStyles = new Collection<DanceStyle>(); } […]

ToList()。ForEach in Linq

我是Linq的新手。 我想要像这样在foreach语句中设置两个值 我的实际代码是这样的 foreach (Employee emp in employees) { foreach(Department dept in emp.Departments) { dept.SomeProperty = null; } collection.AddRange(emp.Departments); } 很less有重构把上面的东西变成了这个 foreach (Employee emp in employees) { emp.Departments.ToList().ForEach(u => u.SomeProperty = null)) collection.AddRange(emp.Departments); } 但是我想要这样的东西 employees.ToList().Foreach(collection.AddRange(emp.Departments), emp.Departments.ToList().ForEach(u => u.SomeProperty = null))

如何清理entity framework对象上下文?

我将几个实体添加到对象上下文中。 try { forach (var document in documents) { this.Validate(document); // May throw a ValidationException. this.objectContext.AddToDocuments(document); } this.objectContext.SaveChanges(); } catch { // How to clean-up the object context here? throw; } 如果某些文档通过了validation,而其中一个文档通过validation,则所有通过validation的文档都会添加到对象上下文中。 我必须清理对象上下文,因为它可能被重用,并且可能发生以下情况。 var documentA = new Document { Id = 1, Data = "ValidData" }; var documentB = new Document { Id = 2, […]

ObjectContext实例已经处理完毕,不能再用于需要连接的操作了

我有这样的看法: @model MatchGaming.Models.ProfileQuery @{ ViewBag.Title = "Index"; } <h2>Index</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm("Results", "Profiles")) { @Html.ValidationSummary(true) <fieldset> <legend>ProfileQuery</legend> @Html.EditorFor(model=>model.SearchString) <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div> 我有这个控制器的HttpPost: [HttpPost] public ActionResult Results(ProfileQuery profileQuery) { Debug.Write(profileQuery.SearchString); using(var db = new MatchGamingEntities()) { var SearchUserName = db.Users.SingleOrDefault(a=> […]

DropDownlist或DropDownListFor Html助手之间的区别

这似乎很奇怪,我无法find这两个助手之间的区别的解释,所以我会认为这是明显的,但我错过了。 基本上我正在试图决定哪一个我应该用我的情况下,以下简单的模型: public class Booking { public int ID { get; set; } public Room Room { get; set; } public DateTime StartTime { get; set; } public DateTime EndTime { get; set; } public ICollection<Equipment> Equipments { get; set; } public string Who { get; set; } } 我想要显示一个简单的Room DropDownlist来添加和编辑预订logging。 在做了大量的Google之后,似乎我需要一个DropDopwListFor,但不知道为什么以及如何?

更新entity framework模型后,Visual Studio不会看到更改

如果我对EF 5.0模型做了任何更改,VS似乎没有看到更改。 我已经尝试添加一个新的表格,在模型中显示出来,但是如果我尝试在某处使用它,表格不会显示在intellisense中,我不能使用它。 我也试着改变列的数据types,如果我看.edmx模型文件再次显示,但Visual Studio仍然会给我编译错误,因为我试图分配不正确的数据types。 如果我彻底删除EDMX,并重新创build它,那么更改工作正常。 但每次我应该能够从数据库更新模型并重build时,从数据库重新生成是很麻烦的。 之前我从来没有遇到EF的这些问题。 有谁知道这个问题的原因或解决办法? 谢谢。

在PagedList中使用@ Html.DisplayNameFor()

我一直在尝试使用PagedList包来获取我的索引视图的分页。 一切进展顺利,在控制器层面,一切工作正常,每页只显示5条logging,并根据查询string显示适当的页面。 我的问题是在看法。 我改变了@Model到PagedList.IPagedList所以我可以访问Model.HasNextPage和其他属性,但现在@Html.DisplayNameFor(model => model.ItemName)不再工作。 我得到这个错误: PagedList.IPagedList<Dossier.Models.Item>' does not contain a definition for 'ItemName' and no extension method 'ItemName' accepting a first argument of type 'PagedList.IPagedList<Dossier.Models.Item>' could be found (are you missing a using directive or an assembly reference?) 以下是视图的相关部分: @model PagedList.IPagedList<Dossier.Models.Item> @using Dossier.Models.Item … <th> @Html.DisplayNameFor(model => model.ItemName) </th> 看来IPagedList与DisplayNameFor()不兼容。 任何想法为什么发生这种情况,以及我如何解决这个问题? 我知道我可以手动input列名,但我希望这些信息可以在模型中保留(并且可以改变)。

错误:未find具有不变名称“System.Data.SqlClient”的ADO.NET提供程序的entity framework提供程序

我最近升级/更新了旧的项目中的entity framework从版本4或5到版本6.现在我得到这个exception: 在EntityFramework.dll中发生types“System.InvalidOperationException”的exception,但未在用户代码中处理 附加信息:找不到具有不变名称“System.Data.SqlClient”的ADO.NET提供程序的entity framework提供程序。 确保提供程序在应用程序configuration文件的“entityFramework”部分中注册。 有关更多信息,请参阅http://go.microsoft.com/fwlink/?LinkId=260882 。 我GOOGLE了错误,并遇到了几个SO线程,但没有包含一个解决scheme,为我工作。 这就是我的App.config的样子: <?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <!– For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 –> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" /> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration> 我已经从我的项目中卸载了entity framework,并重新安装它,删除所有对旧EF文件的引用,并重新安装,但没有为我工作。 我不断收到这个错误。