navigation-properties

Prevent EF6 from generate navigation properties

▼魔方 西西 提交于 2019-12-20 05:01:07
问题 I've recently started to use EF6 and i'm building a couple of T4 templates to generate some code automatically (on VS2012). I'm generating my model from the database and this process creates all the associations automatically based on the DB ForeignKeys. And generates too a "Navigation Property" for that field in Associations/FK. I want to get a "Flat Version" of my entities without navigation properties. Just a class with properties corresponding to table columns. Is there any way to

Using Entity Framework navigation properties without creating lots of queries (avoiding N+1)

╄→尐↘猪︶ㄣ 提交于 2019-12-19 10:18:41
问题 I've been using Entity Framework Profiler to test my data access in an MVC project and have come accross several pages where I'm making far more db queries than I need to because of N+1 problems. Here is a simple example to show my problem: var club = this.ActiveClub; // ActiveClub uses code similar to context.Clubs.First() var members = club.Members.ToList(); return View("MembersWithAddress", members); The view loops through Members and then follows a navigion property on each member to also

Load navigation property from inherited class

五迷三道 提交于 2019-12-19 08:28:22
问题 I want to get all contact entities (including Person -> Employees) Contact |<- Person | |-> Employer | |<- Organization | |-> Employees Person and Organization inherits from Contact. When i use FirstOrDefault my employees and my employer entity were not loaded. public Contact GetContactById(int id) { var contact = GetContacts().FirstOrDefault(c => c.Id == id); return contact; } private IQueryable<Contact> GetContacts() { var contacts = _contextProvider.Context.Contacts .Include("Addresses");

Load navigation property from inherited class

蓝咒 提交于 2019-12-19 08:27:57
问题 I want to get all contact entities (including Person -> Employees) Contact |<- Person | |-> Employer | |<- Organization | |-> Employees Person and Organization inherits from Contact. When i use FirstOrDefault my employees and my employer entity were not loaded. public Contact GetContactById(int id) { var contact = GetContacts().FirstOrDefault(c => c.Id == id); return contact; } private IQueryable<Contact> GetContacts() { var contacts = _contextProvider.Context.Contacts .Include("Addresses");

Foreign key values without navigation properties

孤街浪徒 提交于 2019-12-13 05:40:58
问题 Is it possible to assign foreign key values manually when inserting records? I do not want to use a TransactionScope or similar construct. But i do want to set the foreignkey value before calling SaveChanges() For example: EntityX x = new EntityX(); x.Name = "test"; ctx.AddToEntityX(x); EntityY y = new EntityY(); y.Name = "Test"; y.EntityXID = x.ID; // <--- I want this. Not using a navigation property, but its 0. ctx.AddToEntityY(y); ctx.SaveChanges(); 回答1: yes it is possible but a lot of

Explicit Loading of child navigation properties with criteria

百般思念 提交于 2019-12-12 21:16:06
问题 Using DBContext in EF5 - after filtering and partial loading based on criteria like a date range. I'm trying to produce a complete graph or tree of objects - Persons->Events where the only Events that are included are within a date range. All this whilst preserving the standard change tracking that one gets with the following: Dim Repository As Models.personRepository = New Models.personRepository Private Sub LoadData() Dim personViewModelViewSource As System.Windows.Data.CollectionViewSource

How do I configure a navigation property to the same table in Entity Framework?

青春壹個敷衍的年華 提交于 2019-12-12 03:17:04
问题 How do I configure Entity Framework using fluent configuration to behave the same way that I would do this with attributes: public class Product { public int? ParentId { get; set; } [ForeignKey("ParentId")] public virtual Product Parent { get; set; } } 回答1: Supposing that you want to create a self referencing entity, I assume that you have a Product class like this: public class Product { public int Id { get; set; } public int? ParentId { get; set; } public virtual Product Parent { get; set;

Update resources through navigation properties possible?

白昼怎懂夜的黑 提交于 2019-12-11 09:08:38
问题 I have a resource A that has a navigation property that points to resource B. The schema looks like this: Resource A: ID B (this is a one-to-many navigation property) Resource B: ID Property1 Property2 Can I update values of resource B (i.e. update property1 and property2 of B) through the navigation link of A? In other words, can I say A.B[5].Property1 = x? In the OData spec and examples, I only see descriptions of modifying which instance of B is associated with A (i.e. modifying /A(0)/

Reference a parent table for a Navigation Property in EF

半城伤御伤魂 提交于 2019-12-11 07:55:20
问题 I essentially have three tables with FK's Make MakeId MakeName Model ModelId ModelName MakeId (FK) Vehicle VehicleId DatePurchased ModelId (FK) I would like a navigation property from Vehicle to Make without modifying my database I tried: public class Make { public int MakeId {get;set;} <snip>...</snip> public virtual Vehicle Vehicles {get;set;} } <snip>Model</snip> public class Vehicle { public int VehicleId {get;set} <snip>...</snip> public virtual Make VehicleMake {get;set;} } public class

Add items to an entity collection via ChangeTracker

我与影子孤独终老i 提交于 2019-12-11 07:05:44
问题 I chose to put a complete example of my code in order to demonstrate what I exactly need. Briefly, what I would like to do, is to get a generic code in public override int SaveChanges() that could work to all entities that implements a translation rather than write a one-by-one. ENTITIES public partial class EntityOne { public long EntityOneId { get; set; } public string SomeProperty { get; set; } public virtual ICollection<EntityOneTranslation> EntityOneTranslations { get; set; } public