简体   繁体   English

实体框架更新嵌套列表

[英]Entity Framework Update nested list

I use Entity Framework 6 (Code First).我使用实体框架 6(代码优先)。 I have a class:我有一堂课:

public class DialogSession {...}

And another class with a list of DialogSession objects:另一个带有DialogSession对象列表的DialogSession

public class DialogUser
{
    public int Id { get; set; }
    public List<DialogSession> DialogSessions { get; set; }
}

I add DialogSession object to the list and then execute context.SaveChanges() as follows:我将DialogSession对象添加到列表中,然后执行context.SaveChanges()如下:

dialogUser.DialogSessions.Add(dialogSession);
context.SaveChanges();

But the foreign key of the dialogSession record still Null:但是dialogSession记录的外键还是Null:

在此处输入图片说明

I've tried using many methods on the web like the follows but withoyt success:我已经尝试在网络上使用许多方法,如下所示,但没有成功:

context.DialogUsers.Attach(dialogUser);
context.Entry(dialogUser).State = EntityState.Modified;
context.SaveChangesExtention();

Does anyone know how to save inner objects (like the list) in the database using Entity Framework (6)?有谁知道如何使用实体框架 (6) 在数据库中保存内部对象(如列表)?

From your question is not clear which relationship type do you have, so I guess you have One-to-Many and something like this should works: 从您的问题尚不清楚您拥有哪种关系类型,因此我猜您拥有一对多的关系,并且这样的方法应该有效:

public class DialogSession
{
    public int DialogSessionId { get; set; }
    public virtual DialogUser DialogUser { get; set; }
}

public class DialogUser
{
    public int DialogUserId { get; set; }
    public virtual ICollection<DialogSession> DialogSessions { get; set; }
}

Take a look at example how properly configure this type of relationship in this article . 看一下示例,如何在本文中正确配置这种类型的关系。

If I am not wrong you should add 如果我没记错,您应该添加

dialogUser.DialogSessions.Add(dialogSession);
context.Entry(dialogUser).State = EntityState.Modified; 
context.SaveChanges();

This will mark the entity as modified and then the changes should be reflected on the db. 这会将实体标记为已修改,然后更改应反映在数据库上。

This could be done a more efficiently by marking singular properties as modified 通过将奇异属性标记为已修改,可以更有效地完成此操作

dialogUser.DialogSessions.Add(dialogSession);
context.Entry(dialogUser).Property(u => u.dialogSession).IsModified = true; 
context.SaveChanges();

Give it a try :) 试试看 :)

Please see: https://msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx 请参阅: https : //msdn.microsoft.com/en-us/library/jj591583(v=vs.113).aspx

You should use a virtual list for the child entity, and ensure that the DialogSessions class also refers back to its parent with a DialogUserId property (so named by convention) 您应该为子实体使用一个虚拟列表,并确保DialogSessions类还使用DialogUserId属性(按惯例命名)引用其父对象。

The below works for me.以下对我有用。

using System.ComponentModel.DataAnnotations;

public class DialogSession
{
    [Key]
    public int DialogSessionId { get; set; }
    public int DialogUser { get; set; }
}

public class DialogUser
{
    [Key]
    public int DialogUserId { get; set; }
    public virtual ICollection<DialogSession> DialogSessions { get; set; }
}

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

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