heyoka955
heyoka955
CC#
Created by heyoka955 on 10/7/2024 in #help
Remove-Migration and Add Migration not correctly working how i expected
When I remove the latest Migration and then add later a migration then changes from previus migrations is also added in the new migration altough i just deleted the latest migration. This is a behaviour which i want to avoid, what can i do to avoid such side effects?
5 replies
CC#
Created by heyoka955 on 10/4/2024 in #help
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."} 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();
}
}
25 replies
CC#
Created by heyoka955 on 9/19/2024 in #help
✅ Map json to a dto issue
I want to map my json to a dto this is my json!
{
"contact": {
"name": {
"first": "A",
"last": "K"
}
}
}
´´´

and this is my dto
{
"contact": {
"name": {
"first": "A",
"last": "K"
}
}
}
´´´

and this is my dto
public WixNameDto() { this.First = string.Empty; this.Last = string.Empty; } [JsonPropertyName("first")] public string First; [JsonPropertyName("last")] public string Last; } } ´´´ I used system.text.json
22 replies