entity-framework-4.1

Integration Testing Entity Framework code first with in-memory database

岁酱吖の 提交于 2019-11-27 12:57:31
I'd like to run actual integration tests of my EF4.1 repositories against an in-memory database a la ayende's nhibernate version . I have a code first model, against a legacy database (old table and column names need mapping to my entites using code configurations). I'd like to be able to use Sqlite (or other) to: Generate an in-memory database from my model Create a DBContext for my model with this in-memory database I have already in place IoC/DI of a IDBContextFactory which gets constructed with my (Generic) Repositories (also using a GenericRepository pattern) There's bits and bobs on-line

Entity Framework 5 - DbContext Has Changes?

青春壹個敷衍的年華 提交于 2019-11-27 12:46:18
问题 I'm trying to find a way to determine of any changes have been made to a database context (DbContext). Note: I'm using Visual Studio 2012 with Entity Framework 5 on a Windows 7, 64-bit box. Back when I used to use ObjectContext instead of DbContext, I could do something like: public partial class MyObjectContext { public Boolean HasUnsavedChanges() { return (this.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted).Any()); } } Now that I'm

Why does EF 4.1 not support complex queries as well as linq-to-sql?

五迷三道 提交于 2019-11-27 12:32:36
问题 I am in the process of converting our internal web application from Linq-To-Sql to EF CodeFirst from an existing database. I have been getting annoyed with Linq-To-Sql's limitations more and more lately, and having to update the edmx after updating a very intertwined database table finally frustrated me enough to switch to EF. However, I am encountering several situations where using linq with Linq-To-Sql is more powerful than the latest Entity Framework, and I am wondering if anyone knows

EF 4.1, Code-First: Is there an easy way to remove ALL conventions?

放肆的年华 提交于 2019-11-27 12:28:23
问题 We can remove single conventions this way: modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); modelBuilder.Conventions.Remove<ConcurrencyCheckAttributeConvention>(); // and 31 conventions more But I miss something like modelBuilder.Conventions.RemoveAll() . Is there an easy way to remove ALL of them? (I am even not sure if I really want to remove all conventions finally. But with my growing object model I have difficulties to distinguish clearly which parts of the mapping to

Entity Framework CodeFirst many to many relationship with additional information

五迷三道 提交于 2019-11-27 11:56:10
I have the following model : class Contract { string ContractID{get;set;} ICollection<Part> Parts{get;set;} } class Part { string PartID{get;set;} ICollection<Contract> Contracts{get;set;} } the problem is that the relationship between Part and Contract also contains the following additional information : class ContractParts { Contract{get;set;} Part{get;set;} Date{get;set;} //additional info Price{get;set;} //additional info } How would I write the Entity Context for this ? Ladislav Mrnka In such case you must model your entities this way: public class Contract { public virtual string

mapping multiple tables to a single entity class in entity framework

巧了我就是萌 提交于 2019-11-27 11:55:48
Before this is marked as a duplicate, I have checked the other related posts and they do not answer my question. I am working on a legacy database that has 2 tables that have a 1:1 relationship. Currently, I have one type (1Test:1Result) for each of these tables defined I would like to merge these particular tables into a single class. The current types look like this public class Result { public string Id { get; set; } public string Name { get; set; } public string Text { get; set; } public string Units { get; set; } public bool OutOfRange { get; set; } public string Status { get; set; }

Entity Framework Validation confusion - maximum string length of '128'

元气小坏坏 提交于 2019-11-27 11:47:54
I'm faced with a confusing problem where in my Edit or Create action result methods, EF4 will throw a DbEntityValidationException with the inner message stating: The field Body must be a string or array type with a maximum length of '128'. The model in question looks like this: [Table("tblArticles")] public class Article { [Key] public int ID { get; set; } [Required(ErrorMessage="Title must be included")] public string Title { get; set; } [AllowHtml] public string Body { get; set; } [Required(ErrorMessage="Start Date must be specified")] [Display(Name="Start Date")] [DisplayFormat

Self referencing / parent-child relationship in Entity Framework

给你一囗甜甜゛ 提交于 2019-11-27 11:44:25
问题 I read quite a number of posts of programmers that run into the Unable to determine a valid ordering for dependent operations. Dependencies may exist due to foreign key constraints, model requirements, or store-generated values -exception when using a self-referencing relationship in Entity Framework. I am trying to get a parent-child relationship to work: public class Category { public int CategoryId { get; set; } public string Name { get; set; } public int ParentId { get; set; } public

How to update not every fields of an object using Entity Framework and EntityState.Modified

[亡魂溺海] 提交于 2019-11-27 11:20:59
I need to update all fields except property1 and property2 for the given entity object. Having this code: [HttpPost] public ActionResult Add(object obj) { if (ModelState.IsValid) { context.Entry(obj).State = System.Data.EntityState.Modified; context.SaveChanges(); } return View(obj); } How to change it to add an exception to obj.property1 and obj.property2 for not being updated with this code? Arthur Vickers Let's assume that you have a collection of the properties to be excluded: var excluded = new[] { "property1", "property2" }; With EF5 on .NET 4.5 you can do this: var entry = context.Entry

Entity Framework: Querying Child Entities

≯℡__Kan透↙ 提交于 2019-11-27 11:19:07
I'm learning about Entity Framework at the mo, and am having problems!! Can someone clarify if I am right in thinking that I can't get a parent and a subset of it's children from the db? For example... db.Parents .Include(p => p.Children) .Where(p => p.Children.Any(c => c.Age >= 5)) This will return all Parents that have a child aged 5+, but if I iterate through the Parents.Children collection, all children will be present (not just those over 5 years old). Now the query does make sense to me (I've asked to include children and I've got them!), but can imagine that I would like to have the