简体   繁体   中英

Why is the Entity Framework System.Data.Entity.Core.Objects.RelationshipEntry Wrong? ( For Change Tracking)

All:

Our company is working on a ASP.NET application that uses Entity Framework. The Entity Framework Model First approach was taken to create the database tables and POCOs Moreover, we are using Effort Testing Tool for Entity Framework and NUnit Framework in order to conduct Unit Testing.

At present, we are trying some CRUD operations on Customer and Account Business Entities ASP.NET application.

Every Customer has the option of having at most one account. Here is how the relationship between the 2 tables looks like in a Database Diagram: 在此处输入图片说明

Our application also keeps track of changed tracking of entities and relationships between entities.

We have the following code example with populated Customer and Account business entries:

        Customer custFour = new Customer { CustomerID = "4    ", Orders = new HashSet<Order>(), CustomerDemographics = new HashSet<CustomerDemographic>(), CompanyName = "FC Barcelona", ContactName = "Lionel Messi", ContactTitle = "current top basket ball player", Address = "344 testing street", City = "Buenos Aires", Region = "Oklahoma", PostalCode = "4498", Country = "Argentina", Phone = "2344949494", Fax = "33325" };

        Account acctFour = new Account { UserName = "lmessi", Password = "myteamlostworldcupmatch", Customer = custFour, AccountRoleMaps = new HashSet<AccountRoleMap>() };

To keep it brief, we also invoke a line of code like the following so that the Entity Framework DbContext can save the Customer and Account entities mentioned above, and also the relationship between the Customer and Account entities:

_context.Set<Account>().Add(acctFour);

_context.SaveChanges();

As the point of execution goes on, we ultimately reach the following excerpt of code:

            List<System.Data.Entity.Core.Objects.ObjectStateEntry> allAddModDelItems = ((IObjectContextAdapter)_context).ObjectContext.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified | EntityState.Deleted).ToList();

            foreach (var osEnt in allAddModDelItems)
            {

            }

The above excerpt of code retrieves all the Objects and Relationships between Objects that have been Added, Modified and Deleted.

I already checked the System.Data.Entity.Core.Objects.EntityEntry for the Customer and the System.Data.Entity.Core.Objects.EntityEntry for the Account, and they have Valid Business Data. 在此处输入图片说明

Sadly, the System.Data.Entity.Core.Objects.RelationshipEntry has Entity as null and the EntityKey as null which seems to be wrong because the System.Data.Entity.Core.Objects.RelationshipEntry should reflect the relationship between the Account and Customer objects that I created. 在此处输入图片说明 Why is the System.Data.Entity.Core.Objects.RelationshipEntry Wrong? In other words, Why are the System.Data.Entity.Core.Objects.RelationshipEntry's Entity and the EntityKey properties null?

http://msdn.microsoft.com/en-us/library/system.data.objects.objectstateentry.entity%28v=vs.100%29.aspx

The Entity property returns null when the ObjectStateEntry is associated with a RelationshipSet entry instead of an entity type instance.

Therefore, it makes sense for Entity and the EntityKey properties to be null

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM