简体   繁体   English

为什么实体框架System.Data.Entity.Core.Objects.RelationshipEntry错误? (用于变更跟踪)

[英]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. 我们公司正在研究使用实体框架的ASP.NET应用程序。 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. 采取了实体框架模型优先方法来创建数据库表和POCO。此外,我们使用用于实体框架和NUnit框架的工作量测试工具来进行单元测试。

At present, we are trying some CRUD operations on Customer and Account Business Entities ASP.NET application. 目前,我们正在尝试对客户和帐户业务实体ASP.NET应用程序进行某些CRUD操作。

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: 为了简短起见,我们还调用了类似于以下代码的一行,以便实体框架DbContext可以保存上述的Customer和Account实体以及Customer和Account实体之间的关系:

_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. 我已经为客户检查了System.Data.Entity.Core.Objects.EntityEntry和为帐户检查了System.Data.Entity.Core.Objects.EntityEntry,它们具有有效的业务数据。 在此处输入图片说明

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. 不幸的是,System.Data.Entity.Core.Objects.RelationshipEntry的Entity为null,而EntityKey为null,这似乎是错误的,因为System.Data.Entity.Core.Objects.RelationshipEntry应该反映客户和客户之间的关系。我创建的对象。 在此处输入图片说明 Why is the System.Data.Entity.Core.Objects.RelationshipEntry Wrong? 为什么System.Data.Entity.Core.Objects.RelationshipEntry错误? In other words, Why are the System.Data.Entity.Core.Objects.RelationshipEntry's Entity and the EntityKey properties null? 换句话说,为什么System.Data.Entity.Core.Objects.RelationshipEntry的Entity和EntityKey属性为空?

http://msdn.microsoft.com/en-us/library/system.data.objects.objectstateentry.entity%28v=vs.100%29.aspx http://msdn.microsoft.com/zh-cn/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. 当ObjectStateEntry与RelationshipSet条目而不是实体类型实例相关联时,Entity属性返回null。

Therefore, it makes sense for Entity and the EntityKey properties to be null 因此,将Entity和EntityKey属性设置为null是有意义的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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