简体   繁体   English

在实体框架中更改关系/外键-不保留

[英]Changing a relationship / foreign key in Entity Framework - not being persisted

Ive got a WPF app, following the Model View ViewModel pattern, using the Entity Framework to interact with a SQL 2008 database. Ive遵循模型视图ViewModel模式,使用实体框架与SQL 2008数据库进行交互,得到了一个WPF应用程序。

Problem : 问题

My problem is i can't change a foreign key value in the databse using EF. 我的问题是我无法使用EF更改数据库中的外键值。 I have two entities: Site and Key. 我有两个实体:站点和密钥。 A Site can have many Keys on it. 一个站点上可以有许多密钥。 I would like to move a Key from one Site to another. 我想将密钥从一个站点移动到另一个站点。 I'm trying to do this using the following code: 我正在尝试使用以下代码执行此操作:

keyToMove.Site = newSite;
dataAccess.SaveChanges(); // this method just does: this.entities.SaveChanges();

What I've Observed 我观察到的

It appears in memory everything is fine as the rest of the app reflects this change (the Key will appear under the new Site). 它出现在内存中,一切正常,因为应用程序的其余部分反映了此更改(密钥将出现在新站点下)。 However, the foreign key in the database does not change, and upon closing and restarting the app, all changes are lost. 但是,数据库中的外键不会更改,并且在关闭并重新启动应用程序后,所有更改都会丢失。

After "keyToMove.Site = newSite;", keyToMove, newSite and the old Site instance all have an EntityState value of "Unchanged" which can't be right. 在“ keyToMove.Site = newSite;”之后,keyToMove,newSite和旧的Site实例都具有EntityState值“ Unchanged”,这是不正确的。 Also SaveChanges is returning 2, which is just confusing me even further. SaveChanges也返回2,这让我更加困惑。

The setter implementation for the "Site" property looks like other navigation properties in my model: “站点”属性的setter实现与模型中的其他导航属性类似:

set
{
    ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference<Site>("CmsModel.SiteKeyAssociation", "Site").Value = value;
}

SQL Profiler shows that nothing resembling an UPDATE is sent to the db engine, only a very, very long SELECT. SQL Profiler显示没有类似于UPDATE的内容发送到db引擎,只有非常长的SELECT。

Things which may have an effect 可能会产生影响的事物

The "Key" entity inherits from another entity based on a condition applied to one of the columns. “键”实体基于应用于列之一的条件从另一个实体继承。

I'm using the AutoFac IOC container to supply any ViewModels who want to do some data access with a reference to an instance of my data access class ("dataAccess" in the example). 我正在使用AutoFac IOC容器来提供想要进行某些数据访问的任何ViewModel,这些ViewModel都引用了我的数据访问类的实例(示例中为“ dataAccess”)。

An ObjectContext instance is a member of this class (shown in comments as "this.entities"). ObjectContext实例是此类的成员(在注释中显示为“ this.entities”)。

The dataAccess instance is Singleton Scoped so there should only be one instance being shared among the View Models ei both "keyToMove" and "newSite" are attached to the same context. dataAccess实例是Singleton范围的,因此“ keyToMove”和“ newSite”都将附加到同一上下文的视图模型之间只能共享一个实例。

Sorry for the length of this post, but i seriously have no idea whats going on here and ive tried to provide as many details which may help as possible. 很抱歉,这篇文章的长度,但是我真的不知道这里发生了什么,并且我试图提供尽可能多的细节,可能会有所帮助。 Please ask if any further info would help. 请询问是否还有其他信息会有所帮助。

Thanks in advance. 提前致谢。

I'm in the process of teaching myself the entity framework myself, so I may be missing the point of your question, and am certainly no expert. 我正在自学实体框架的过程,因此我可能遗漏了您的问题,当然也不是专家。 However, that being said, you may be running into a problem I had to work through myself. 但是,话虽如此,您可能会遇到我不得不自己解决的问题。

You didn't post the code where you are creating your "newSite" variable, but when setting foreign keys in EF you do it differently than you would in Linq or just working in the database. 您没有将代码发布到创建“ newSite”变量的位置,但是在EF中设置外键时,您所做的操作与在Linq中或在数据库中进行操作时有所不同。 With EF, you have to select IN your foreign key value that you want to set on your object and then set it. 使用EF,您必须选择要在对象上设置的IN外键值,然后再对其进行设置。

This will not work: 这将不起作用:

NewSite = Site.CreateSite("PrimaryKeyValue");
keyToMove.Site = NewSite;

Instead, you do something like this: 相反,您可以执行以下操作:

NewSite = MyEntities.Site.Where(site=>site.PrimaryKey==PrimaryKeyValue)
                                .FirstOrDefault();
keyToMove.Site = NewSite;

I hope I understood your question! 希望我能理解您的问题!

Regards, Mike 问候,迈克

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

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