简体   繁体   English

如果我在类中更改了父实体的导航属性,是否需要手动更改外键?

[英]If I change the Navigation Property for the Parent Entity in a class, do I need to manually change the Foreign Key?

I have an Entity ; 我有一个Entity ;

public class ChildEntity
{
    /// <summary>
    /// Foreign key for this entity.
    /// </summary>
    public virtual int ParentId { get; set; }

    /// <summary>
    /// Navigation Property for this entity.
    /// </summary>
    public virtual TParentEntity Parent { get; set; }
}

If I am changing the Parent property to a different ParentEntity in the Database, (updating both Parents Collections of Children Entities ) do I then need to change the ParentId from one Parent Entity to the other manually? 如果我改变了Parent属性不同ParentEntity在数据库中,(更新两个Parents CollectionsChildren Entities ),我则需要改变ParentId从一个父实体手动其他?

Thanks! 谢谢!

If you change the navigation property of an entity, the corresponding changes will be made to the foreign key column in the database. 如果更改实体的导航属性,则将对数据库中的外键列进行相应的更改。

source: https://docs.microsoft.com/en-us/ef/core/saving/related-data#changing-relationships 来源: https : //docs.microsoft.com/en-us/ef/core/saving/related-data#changing-relationships

Here's a sample code to observe the said behavior. 这是观察上述行为的示例代码。

using (var context = new Context())
{
    var child = new ChildEntity();
    child.Parent = new TParentEntity();
    context.Add(child);

    context.SaveChanges();
    Console.WriteLine(child.ParentId); // ParentId == 1

    child.Parent = new TParentEntity();
    Console.WriteLine(child.ParentId); // ParentId == 1

    context.SaveChanges();
    Console.WriteLine(child.ParentId); // ParentId == 2
}

暂无
暂无

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

相关问题 EF6:使用外键关系修改实体属性 - 我是否需要更改Id或相关对象或两者? - EF6: Modifying an entity property with a foreign key relation - Do I need to change the Id or the related object or both? 实体框架,更改外键,但导航属性不变 - entity framework, change foreign key but navigation property not changing 如何使用实体框架更改外键关联? - How do I change a foreign key association using Entity Framework? 使用实体框架时,设置 FK 时应该设置导航属性还是外键属性? - When using Entity Framework, should I set the navigation property or foreign key property, when setting the FK? 如果我有非标准的外键名称,我是否需要手动设置关系的两面? - If I have a non standard Foreign Key name do I need to manually setup both sides of a relationship? (首先是EF6模型)如何在不访问导航属性的情况下获取外键的值,从而无需加载整个实体? - (EF6 Model first) How can I get the value of a foreign key without accessing the navigation property so that I don't need to load the full entity? 在什么情况下我需要实体框架中的外键和导航属性 - In what scenarios do I need foreign keys AND navigation properties in entity framework 具有复合外键的实体框架导航属性 - Entity Framework Navigation Property with Composite Foreign Key 我是否需要使用实体框架将属性 int 显式添加到组合对象的外键中? - Do I need to explicitly add a property int to foreign keys for composed objects using Entity Framework? 我是否需要在逻辑请求端手动将 AsyncLocal 变量的值更改为“Dispose”/“Release” - Do I need to manually change the value of AsyncLocal variable to “Dispose”/“Release” it on logical request end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM