简体   繁体   中英

Updating child entity EF core

A have this entity called Service. This entity have a child entity called TypeService. TypeService could be null in database (MySQL).

public class Service
{
    public int Id { get; set; }
    public string Name{ get; set; }
    public string Department { get; set; }
    public TypeService TypeService { get; set; }
}

MySQL:

 `TypeServiceId` bigint(20) default NULL //(Colunm of Service table)

When I try to create a Service without TypeService (TypeService == null) it's works fine. When I try to edit a Service and include the TypeService it's works fine. When I try to edit a Service and change the TypeService it's works fine.

My problem is: when I try to update the Service and set TypeService to null, the EF does not update this TypeServiceId

Why ef core update to change the TypeServiceId fild to another value correctly but don't to null value?

There is no error message.

Thanks and best regards!

From what I understood, you want to set TypeServiceId to null and don't care whether the entity is still present in any table? In this case you can add property

[Column(TypeName = "bigint(20)")]
public Int64? TypeServiceId { get; set; }

And simply change TypeServiceId to null, instead of the entity itself. Make sure to review the changes in the migration, so that EntityFramework doesn't create new column, instead of using the created one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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