Search code examples
.netentity-frameworkforeign-keys

EF Core , sets value null after saving


I have a strange issue. I have table with 2 foreign keys which leads to same table

public class Orders
{
    /// <summary>
    /// ID
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// Creation date
    /// </summary>
    public DateTime UploadDate { get; set; }

    /// <summary>
    /// Driver identificator
    /// </summary>
    public int DriverId { get; set; }

    /// <summary>
    /// Start store id
    /// </summary>
    public int? FromStoreId { get; set; }

    /// <summary>
    /// End store id
    /// </summary>
    public int? ToStoreId { get; set; }

    /// <summary>
    /// Order Weight
    /// </summary>
    public double Weight { get; set; }

    /// <summary>
    /// Order volume
    /// </summary>
    public double Volume { get; set; }

    /// <summary>
    /// Order status
    /// </summary>
    public OrderStatuses Status { get; set; }

    /// <summary>
    /// Order public id
    /// </summary>
    public string OrderId { get; set; }

    /// <summary>
    /// Organization identifier
    /// </summary>
    public int OrganizationId { get; set; }

    /// <summary>
    /// Order amount
    /// </summary>
    public int Amount { get; set; }

    /// <summary>
    /// Commentary
    /// </summary>
    public string Commentary { get; set; }
}

FromStoreId and ToStoreId connected with Stores table

public class Stores
{
    /// <summary>
    /// Store id
    /// </summary>
    public int Id { get; set; }

    /// <summary>
    /// Store address
    /// </summary>
    public string Address { get; set; }

    /// <summary>
    /// Organization id
    /// </summary>
    public int OrganizationId { get; set; }
}

And when i save list in database, it sets some values to null BUT IT'S ISN'T NULL

BUT IT'S ISN'T NULL

My EntityTypeConfiguration

public class OrdersConfiguration : IEntityTypeConfiguration<Orders>
{
    public void Configure(EntityTypeBuilder<Orders> builder)
    {
        builder.HasKey(x => x.Id);
        builder.HasIndex(x => x.Id).IsUnique(false);
        builder.HasIndex(x => x.OrganizationId).IsUnique(false);
        builder.HasIndex(x => x.FromStoreId).IsUnique(false);
        builder.HasIndex(x => x.ToStoreId).IsUnique(false);
        builder.HasOne<Organizations>().WithOne().HasForeignKey<Orders>(x => x.OrganizationId).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne<Stores>().WithOne().HasForeignKey<Orders>(x => x.FromStoreId).OnDelete(DeleteBehavior.NoAction);
        builder.HasOne<Stores>().WithOne().HasForeignKey<Orders>(x => x.ToStoreId).OnDelete(DeleteBehavior.NoAction);
    }
}

But when i delete FK it works fine. May be i missing something? Because i don't inderstand why during save , ef sets one of values to null.

What i am doing wrong?


Solution

  • After few days, i figured out that problem was in AddRangeAsync , i don't know why , but it doesn't save FK , i solved this problem by Dapper insert query in cycle