© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•17mo ago•
24 replies
heyoka955

Why does Ef Core not save my data in the database?

I have four models that are saved in sql server with Ef-Core and these four are Order, Contact, Address and BillingInfo.
Each Order has Contact and BillingInfo.
Each Contact has an Address.
Each BillingInfo has an Andress, too.

The problem is whenever i try to save it in the database i receive an error like this:


{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_WSOrderAddresses_WebshopBillingInfoPocos_WebshopBillingInfoPocoID\". The conflict occurred in database \"DatabaseService_Name\", table \"dbo.WebshopBillingInfoPocos\", column 'Id'.\r\nThe statement has been terminated."}
{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_WSOrderAddresses_WebshopBillingInfoPocos_WebshopBillingInfoPocoID\". The conflict occurred in database \"DatabaseService_Name\", table \"dbo.WebshopBillingInfoPocos\", column 'Id'.\r\nThe statement has been terminated."}



and this is my code:

public async Task<WebshopPoco?> CreateOrderAsync(WebshopPoco order)
{
    this.Logger.TraceMethodBegin();

    if (order == null)
    {
        throw new ArgumentNullException(nameof(order));
    }
    this.Logger.LogDebug($"Method parameter 'objectPoco': {order}");

    try
    {
        this.Context.Entry(order).State = EntityState.Added;
        this.Context.Entry(order.Contact).State = EntityState.Added;
        this.Context.Entry(order.Contact?.Address).State = EntityState.Added;
        this.Context.Entry(order.WebshopBillingInfoPoco).State = EntityState.Added;
        this.Context.Entry(order.WebshopBillingInfoPoco?.Address).State = EntityState.Added;

        if (order.Items != null)
        {
            foreach (var item in order.Items)
            {
                order.Id = 0;

                this.Context.Entry(item).State = EntityState.Added;
            }
        }

        return await this.Context.SaveChangesAsync() >= 1 ? order : null;
    }

    catch (Exception e)
    {
        this.Logger.LogError(e.Message);
        return null;
    }
    finally
    {
        Logger.TraceMethodEnd();
    }
}
public async Task<WebshopPoco?> CreateOrderAsync(WebshopPoco order)
{
    this.Logger.TraceMethodBegin();

    if (order == null)
    {
        throw new ArgumentNullException(nameof(order));
    }
    this.Logger.LogDebug($"Method parameter 'objectPoco': {order}");

    try
    {
        this.Context.Entry(order).State = EntityState.Added;
        this.Context.Entry(order.Contact).State = EntityState.Added;
        this.Context.Entry(order.Contact?.Address).State = EntityState.Added;
        this.Context.Entry(order.WebshopBillingInfoPoco).State = EntityState.Added;
        this.Context.Entry(order.WebshopBillingInfoPoco?.Address).State = EntityState.Added;

        if (order.Items != null)
        {
            foreach (var item in order.Items)
            {
                order.Id = 0;

                this.Context.Entry(item).State = EntityState.Added;
            }
        }

        return await this.Context.SaveChangesAsync() >= 1 ? order : null;
    }

    catch (Exception e)
    {
        this.Logger.LogError(e.Message);
        return null;
    }
    finally
    {
        Logger.TraceMethodEnd();
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Ef core seed (data)
C#CC# / help
12mo ago
Data retrieval EF-core
C#CC# / help
15mo ago
✅ [EF Core] Why does my database entity have a one-directional FK Connection?
C#CC# / help
3y ago
Save button does not save the inserted data
C#CC# / help
16mo ago