entity-framework-4.1

How do I detach objects in Entity Framework Code First?

吃可爱长大的小学妹 提交于 2019-11-26 17:16:43
There is no Detach(object entity) on the DbContext . Do I have the ability to detach objects on EF code first? Ladislav Mrnka If you want to detach existing object follow @Slauma's advice. If you want to load objects without tracking changes use: var data = context.MyEntities.AsNoTracking().Where(...).ToList(); As mentioned in comment this will not completely detach entities. They are still attached and lazy loading works but entities are not tracked. This should be used for example if you want to load entity only to read data and you don't plan to modify them. This is an option: dbContext

ADO.NET DbContext Generator vs. ADO.NET Poco Entity Generator (ObjectContext)

亡梦爱人 提交于 2019-11-26 17:14:45
I am about to start implementing the data access infrastructure of a project that was architected with an approach to DDD ( it's my first attempt on DDD, so be gentle ;-) ). I will be using Entity Framework. Until now, I was looking into the method teached by Julie Lerman on her great book, Programming Entity Framework , where ADO.NET POCO Entity Generator is used, with some changes to the T4 templates and some more custom code. Today I started reading articles on EF4.1 and the ADO.NET DbContext Generator , using Database First approach, and I'm trying to decide with which one should I go.

Using the Entry<TEntity>().CurrentValues.SetValues() is not updating collections

∥☆過路亽.° 提交于 2019-11-26 16:58:23
问题 I have not run into this before, because I usually handled collections by them selves instead of modifying them directly on the entity. public class Schedule: BaseEntity { public Guid Id {get;set;} public virtual int? DayOfTheWeekTypeId { get; set; } public virtual DayOfTheWeekType DayOfTheWeekType { get; set; } public virtual ICollection<Instructor> Instructors { get; set; } public DateTime? StartDateTime { get; set; } public DateTime? EndDateTime { get; set; } public string

Foreign keys in entity framework 4.1

*爱你&永不变心* 提交于 2019-11-26 16:27:04
问题 I am working on Entity Framework 4.1 and using data annotations for foreign keys. I want to know how can we define one to many relationship between product and categories. I want to map category. categoryId with product.cid public class Category { public string CategoryId { get; set; } public string Name { get; set; } public virtual ICollection<Product> Products { get; set; } } public class Product { public int ProductId { get; set; } public string Name { get; set; } public string CId { get;

Understanding code first virtual properties

╄→гoц情女王★ 提交于 2019-11-26 16:13:26
问题 Hi I am just learning to work with Entity Framework Code First and I can not seem to understand something.I have created three models based on a tutorial: public class Course { public int CourseID { get; set; } public string Title { get; set; } public int Credits { get; set; } public virtual ICollection<Enrollment> Enrollments{ get; set; } } public class Enrollment { public int EnrollmentID { get; set; } public int CourseID { get; set; } public int StudentID { get; set; } public decimal?

Entity Framework CodeFirst many to many relationship with additional information

半城伤御伤魂 提交于 2019-11-26 15:48:07
问题 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 ? 回答1: In such case

mapping multiple tables to a single entity class in entity framework

≯℡__Kan透↙ 提交于 2019-11-26 15:48:03
问题 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

Should I enable or disable dynamic proxies with entity framework 4.1 and MVC3?

家住魔仙堡 提交于 2019-11-26 15:47:02
Could someone offer up some advice or point out some blogs/articles that could help with making this decision? The proxies seem very foreign to me and I'm hesitant to use them. I like the ability to control Lazy Loading by using virtual properties in my model, but that's pretty much all the benefits that I can see. My application is a simple MVC web application and I don't need to wire up any hooks into the context for when the entities experience a changed state. Anyway, here's my very limited list of pros and cons right now, let me know if I'm off base with any of this. Pros On 'Save' or

Entity Framework 4.1 Virtual Properties

允我心安 提交于 2019-11-26 15:43:13
If i have declared entity relationship in my model as virtual then there is no need to use the Include statement in my LINQ query, right ??- For ex: This is my model class : public class Brand { public int BrandID { get; set; } public string BrandName { get; set; } public string BrandDesc { get; set; } public string BrandUrl { get; set; } public virtual ICollection<Product> Products { get; set; } } Now, for the above model class, i dont need to use the var brandsAndProduct = pe.Brands.Include("Products").Single(brand => brand.BrandID == 22); . Instead, I can just use the simple var

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

£可爱£侵袭症+ 提交于 2019-11-26 15:32:25
问题 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? 回答1: Let's assume that you have a collection of the properties to be excluded: var excluded = new[]