entity-framework-4

Why doesn't this Include work?

北慕城南 提交于 2019-12-11 01:45:11
问题 I am passing values into a method which uses a foreach loop to iterate through a collection. In the loop, an Include statement is used from entity framework to eager load. This is what I pass in: var exp = new Collection<Expression<Func<Foo,object>>>(); Why is it that when I use this: exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Position))); exp.Add(f => f.Bars.Select(b=> b.Employees.Select( e=> e.Bank))); and Employee, Position, and Bank all have the field Name that it will

Domain Modeling and Mapping with EF CPT5

百般思念 提交于 2019-12-11 01:37:31
问题 I am trying to model out a album that has a collection of photos. Each Album will have a collection of Photos and a Photo that is a thumb. This is what I have but EF does not seem to like it. I am using EF CPT5 The Model : public class Album : IEntity { private DateTime _dateCreated; public Album() { _dateCreated = SystemTime.Now(); Photos = new List<Photo>(); } public long Id { get; set; } public string Name { get; set; } public string Location { get; set; } public DateTime DateCreated { get

Concurrency in EF4 - How to conditionally create an entity

眉间皱痕 提交于 2019-12-11 01:36:59
问题 I need to be able to create a new User entity only if the provided email is unique. I've always handled this before by performing a simple if (!UserSet.Any(...)) before my AddToUserSet(...) . However, this is not a concurrent solution and will break under heavy load. I've been looking into Transactions, but AFAIK I would need to set an UPDLOCK on the SELECT too, but EF4 does not support this. How does everyone else handle this? 回答1: You can force locking by including SELECT in transaction:

EF 4.1 DBContext AutoDetectChangesEnabled

天大地大妈咪最大 提交于 2019-12-11 01:17:00
问题 OK. I have turned off AutoDetectChangesEnabled, and when I query the context, modify an entity and attempt to save changes, nothing gets updated. I would expect that. But when I mark the entity as modified, I would expect it to change. Any ideas? I am using the UnitOfWork, Repository, Service pattern. If I enable AutoDetectChangesEnabled then all is fine. What is the standard way to persist changes to attached objects? What about detached objects? Thanks in advance, Sam 回答1: It's not just

Entity Framework and caching - Changes are tracking back to cache

蹲街弑〆低调 提交于 2019-12-11 01:11:29
问题 I have some data being pulled in from an Entity model. This contains attributes of items, let's say car parts with max-speed, weight and size. Since there are a lot of parts and the base attributes never change, I've cached all the records. Depending on the car these parts are used in, these attributes might now be changed, so I setup a new car, copy the values from the cached item "Engine" to the new car object and then add "TurboCharger", which boosts max speed, weight and size of the

DbContext Unity does not call HttpContextLifetimeManager.RemoveValue() Bad thing?

馋奶兔 提交于 2019-12-11 01:03:29
问题 I'm defining my DbConntextObj _container.RegisterType<IDbConntextObj, DbConntextObj>(new HttpContextLifetimeManager<DbConntextObj>()); Unity is not calling the RemoveValue() on the lifetimemanager I have one Dbcontext for multiple repositories. My lifetimemanager looks like this: public class HttpContextLifetimeManager<T> : LifetimeManager, IDisposable { private readonly string _itemName = typeof(T).AssemblyQualifiedName; public override object GetValue() { return HttpContext.Current.Items[

Missing something in “Delete” post method in MVC (EF 4.1)

早过忘川 提交于 2019-12-11 00:50:17
问题 Following this example .. http://msdn.microsoft.com/en-us/data/gg685489 I am running into issues with Delete functionality. [HttpPost] public ActionResult Delete(int id, Blog blog) { try { using (var db = new BlogDataEntities()) { //Having issue here.. as soon as the next line is run in debug //mode .. it goes to catch.. and returns a view with null values. db.Entry(blog).State = System.Data.EntityState.Deleted; db.SaveChanges(); } return RedirectToAction("Index"); } catch { return View(); }

Schema specified is not valid. Errors: The relationship not loaded because the type is not available

风格不统一 提交于 2019-12-11 00:34:46
问题 I have the entities Dependency, Product and Access. Dependency is connected to Product and Access. When i try to create a object set of Access with: this.context.CreateObjectSet<Access>(); It's working... but when i try to create a object set of Product i get this error: Schema specified is not valid. Errors: The relationship 'Model.FK_Product_Dependency' was not loaded because the type 'Model.Dependency' is not available. Any ideas? OBS: i'm working with Database to Model, and with EF 4.0

Stack overflow exception without infinite loop (as far as I can tell)

﹥>﹥吖頭↗ 提交于 2019-12-11 00:10:10
问题 I have a stack overflow error, and I'm fairly sure I don't have any kind of infinite recursion (at least I've stared at the error for a few hours now and I can't imagine how its looping infinitely). Here is the code: public decimal? Amount { get { if (!string.IsNullOrEmpty(_savedWork.Amount)) return decimal.Parse(_savedWork.Amount); else return null; } set { if (value.HasValue) { _savedWork.Amount = value.Value.ToString(); Percent = null; } else _savedWork.Amount = ""; OnPropertyChanged(

How to access data in EntityDataSource programmatically

心不动则不痛 提交于 2019-12-10 23:47:42
问题 I have an EntityDataSource bound to many filters used by gridview data, I want to have access to the entities that was selected be the EntityDataSource to be able to export them in xml for example, how can I do that? 回答1: You get access to result of query executed in EntityDataSource by handling its Selected event and accessing Results of EntityDataSourceSelectedEventArgs . 来源: https://stackoverflow.com/questions/9565591/how-to-access-data-in-entitydatasource-programmatically