entity-framework-5

How to get foreign key value for independent association without hitting database?

馋奶兔 提交于 2019-12-06 03:24:47
I am using independent associations (with lazy loading) to access related entities in my code first model e.g. public class Aaa { public int AaaId {get;set;} public string SomeValue {get;set;} } public class Bbb { public int BbbId {get;set;} public string SomeValue {get;set;} public virtual Aaa MyIndependentAssociation {get;set;} } but I am wondering how to access the foreign key value of MyIndependentAssociation without actually loading the record. I am assuming the Aaa_AaaId value is actually loaded when I retrieve a Bbb entity from the database (according the debug visualizer on the entity

Entity Framework 5.0 Beta - will a DbContext code generation template be provided?

半城伤御伤魂 提交于 2019-12-06 02:47:35
问题 I was working a bit ahead and planning for transition from EF 4.2 CTP to EF 5.0 and when "adding a code generation template" in VS11 + .NET 4.5 the DbContext template is no longer available. Any heads up on if one will be available and if not why (just curious)? Update: I see references to a DbContext template in the Enum tutorial, but yet I don't see the template in my VS11 project targeting .NET 4.5 with EF5.0 -pre installed. I see the other two V5.0 templates (EntityObject and Self

One-to-one relationship in CF Entity Framework using mapping

天涯浪子 提交于 2019-12-06 02:37:06
I'm using Entity Framework 5, Code-First. I've two domain objects (or tables). 1st is User , and 2nd is UserProfile . One user can have only one profile, and one profile belongs to only one user. That is 1-1 relationship. Here are the classes.... (I simplified the code to make it understandably, It is actually more complex) User public class User { public virtual Int64 UserId { get; set; } public virtual UserProfile UserProfile { get; set; } public virtual String Username{ get; set; } public virtual String Email { get; set; } public virtual String Password { get; set; } } UserProfile public

EF 5.0 Model First - How To Make Not Mapped Properties

被刻印的时光 ゝ 提交于 2019-12-06 01:39:45
Code First has the attribute [NotMapped] for properties which are not to be mapped: how would one achieve the same thing in model-first mode? NotMapped properties are not part of your mapping. When the model first approach is used EDMX represents the mapping so every property defined in the diagram is mapped. If you want to have non-mapped property it must not be defined in the diagram. All classes generated by EF's code generation are partial so you only need to create your own partial part of the class (it must be in the same namespace and assembly as generated part) and define your non

Filtering navigation property in eager loading

▼魔方 西西 提交于 2019-12-06 01:35:31
I have been working with soft delete and now i want to load the navigation properties of my entity that are not "deleted". I have found a way, my problem this way is not to clear for me, there is another way to do this. Context.CreateSet().Include("Salary").Select(u => new {User= u, Salary = u.Salarys.Where(s => !s.Deleted)}).AsQueryable().Select(a => a.User).AsQueryable(); Eager loading doesn't support filtering. Your code can be simplified to: var users = Context.CreateSet<User>() .Select(u => new { User = u, Salary = u.Salaries.Where(s => !s.Deleted) }) .AsEnumerable() .Select(a => a.User);

How do I create a primary key using two foreign keys in Entity Framework 5 code first?

我的未来我决定 提交于 2019-12-06 01:31:53
I have an entity where the primary key consists of two foreign keys to two other tables. I have the configuration working with the following but the table is generated with two FK references. The table: domain.Entity1 MorePK (PK, FK, int, not null) Entity2_Id (PK, FK, int, not null) Entity3_Id (PK, FK, int, not null) OtherData (varchar, null) Entity2_Id1 (FK, int, null) Entity3_Id1 (FK, int, null) is generated from: public Entity1 { public int MorePK { get; set; } public int Entity2_Id { get; set; } public int Entity3_Id { get; set; } public string OtherData { get; set; } public virtual

EF5 Many To Many update disconnected scenario

爱⌒轻易说出口 提交于 2019-12-06 01:26:23
I didn't find yet a good solution to this problem (similar ticket here EF5 update disconnected graph (n-tier) one-to-many ). I found and followed this example: http://entityframeworktutorial.net/update-many-to-many-entities-in-entity-framework.aspx#.UTBeTDBhif8 . This works just fine, but it creates a duplicate of the related entities (with sql profiler I saw the Insert into RelatedEntities .. I would expected just an insert / update on the MyEntities_Related join table and I don't understand why EF makes an insert on the RelatedEntities table :( And here is my code: public void AddOrUpdate

Mocking DbEntityEntry

安稳与你 提交于 2019-12-06 00:17:57
I am writing unit tests for my Generic Repository layer but i have some problems with DbEntityEntry . My Update method is like below. public virtual void Update(TEntity entityToUpdate) { var entity = dbSet.Find(context.Entry<ITrackedEntity>(entityToUpdate).Entity.Id); context.Entry(entity).CurrentValues.SetValues(entityToUpdate); } As you can see, context.Entry<TEntity>(TEntity entity) method is invoked by caller. So while unit tests this code throws null exception. I had tried to mock context.Entry method but i couldn't because i couldn't provide return value. My current test method is below

EF Database first how to update model for database changes?

这一生的挚爱 提交于 2019-12-05 23:07:25
In a class library Ado.net Entity Data Model is has generated POCO classes. These were generated fine for the first time. But database changes are not being reflected. In edmx diagram right clicking and choosing Update Model from Database show newly created table but it do not add table even after selecting it to add. I tried running .tt (by right click and Run custom tool) but even it did not regenerated the Poco classes as per latest DB changes. Help please Not a fix but a workaround: Is it not an option to simply remove and regenerate the EDMX and the generated classes? That's what I do, it

Entity Framework 5 does not clear navigation property

半城伤御伤魂 提交于 2019-12-05 22:45:43
问题 I've got this strange issue with Entity Framework 5 where I have a navigation property in one of my entities that I would like to set to null . But for some reason the property only gets cleared the second time I call this property: using (var db = new Entities()) { var vehicle = db.Vehicles.Single(v => v.Id == vehicleId); // After this call, ParkingBay is still set. vehicle.ParkingBay = null; // Only after this call, ParkingBay becomes null. vehicle.ParkingBay = null; db.SaveChanges(); } The