简体   繁体   English

实体框架4.0更新POCO关系

[英]Entity Framework 4.0 Update POCO relationship

I have created the POCO class and it's equivalent in edmx file (Category entity with only one way navigation property Parent) 我创建了POCO类,它是在EDMX文件当量(类别实体只有一个办法导航属性Parent)

public class Category {
    public int ID {get;set;}
    public string Name {get;set;}
    public Category Parent {get;set;}
} 

And I have an issue with CRUD operations affecting Parent property: 我对影响父级属性的CRUD操作有一个问题:

Retrieve looks like: 检索如下:

public void CanRetrieve() {
var category = context.Categories.Where(x => x.ID == id).FirstOrDefault();
cotext.LoadProperty<Category> (category, c => c.Parent);

}

ant is working fine (i get Category object with Parent property filled) 蚂蚁工作正常(我得到填充了Parent属性的Category对象)

Add looks like: 添加外观如下:

public void CanAdd() {
    Category cat = new Category();
    cat.Name = "cat 1";    
    cat.Parent = new Category() {ID = 12}; //assuming that in the database there is a record with ID 12 
    context.Categories.Attach(cat);
    context.SaveChanges();
}

Is also working fine (in the DB new record appears with Parent_ID field set to 12) 也可以正常工作(在数据库新记录中,Parent_ID字段设置为12)

And Update: 并更新:

public void CanUpdate() {
    Category cat = new Category();
    cat.Name = "cat 1";    
    cat.Parent = new Category() {ID = 12}; //assuming that in the database there is a record with ID 12 
    context.Categories.Attach(cat);
    //XXX    
    context.SaveChanges()
}

does anything. 什么都做。 If I replace the //XXX line with the following line: 如果我用以下行替换// XXX行:

Context.ObjectStateManager.ChangeObjectState(cat, EntityState.Modified); 

the value of Name property is updated form the source object (I guess other scalar properties would also be) but the Parent is ignored and the Parent_ID field in the base is preserved. Name属性的值从源对象更新(我想其他标量属性也将更新),但是Parent被忽略,并且保留了基础中的Parent_ID字段。

My question is: How in this scenario can I tell the context I want the Parent property value also reflected to the database and can it be done without passing all the values of Parent property (only an ID, just like in Adding scenario)? 我的问题是:在这种情况下,如何告诉上下文我也希望将Parent属性值也反映到数据库中,并且可以在不传递Parent属性的所有值的情况下完成此操作(仅使用ID,就像在添加场景中一样)?

Best regards, 最好的祝福,

Andrzej 安杰伊

Unfortunately, I had to add the ParentId (or whatever is the name of the column) property to make it work. 不幸的是,我必须添加ParentId(或列的名称)属性才能使其正常工作。 I know it sucks, but I didn't figure it out other way. 我知道这很糟糕,但是我没有其他办法。

So, change the property and null the parent property, attach the instance to the context, mark it as modified and call SaveChanges() ; 因此,更改属性并将父属性设置为null,将该实例附加到上下文,将其标记为已修改,然后调用SaveChanges() and that's it. 就是这样。

IMO, this should be resolved by EF. IMO,这应该由EF解决。 I use EntityTypeConfiguration for each Class<->Table Mapping in order to keep the database and classes standard unchanged, so it should be straight forward to know that what I marked as primary key is what the EF must use. 我用EntityTypeConfiguration为每个类< - >映射表,以保证数据库和类标准不变,所以它应该是直截了当地知道,我标记为主要关键是EF必须使用什么。

Are you really sure that your CanAdd code really works? 你真的确定你CanAdd代码真的有效? You are attaching two new objects to context which means that they are in unchanged state. 您要将两个新对象附加到上下文,这意味着它们处于未更改状态。 By calling SaveChanges they should not be inserted. 通过调用SaveChanges,不应将其插入。 If you use AddObject instead of Attach both new and parent categories will be set to Added state and both will be inserted in SaveChanges. 如果使用AddObject而不是Attach,则新类别和父类别都将设置为“已添加”状态,并且两者都将插入SaveChanges中。

Your CanUpdate method doesn't work because you are just setting updated category to modified state but you have attached three objects - category, parent category and relations between them. CanUpdate方法不起作用,因为您只是将更新的类别设置为已修改状态,但附加了三个对象-类别,父类别以及它们之间的关系。 You also have to change state of the relation. 您还必须更改关系的状态。

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

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