mikibanan
mikibanan
Explore posts from servers
CC#
Created by mikibanan on 1/14/2024 in #help
@oninput in blazor does not trigger
Hi, I'm trying to trigger some function everytime user type something in input.
<input class="clean-input" type="number" value="@item.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == 1)?.Value"
@oninput="@(args => UpdateAttributeValue(item, 1, (string)args.Value!))"/>
<input class="clean-input" type="number" value="@item.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == 1)?.Value"
@oninput="@(args => UpdateAttributeValue(item, 1, (string)args.Value!))"/>
This is my code. Am I doing something wrong?
private async Task UpdateAttributeValue(Product product, int attributeId, string value)
{
Console.WriteLine($"Product: {product.Name}, AttributeId: {attributeId}, Value: {value}");
var attribute = product.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == attributeId);

if (attribute is not null)
{
attribute.Value = value;
DbContext.ProductAttributes.Update(attribute);
await DbContext.SaveChangesAsync();
}
else
{
DbContext.ProductAttributes.Add(new ProductAttribute
{
ProductId = product.ProductId,
AttributeId = attributeId,
Value = value
});
await DbContext.SaveChangesAsync();
}
}
private async Task UpdateAttributeValue(Product product, int attributeId, string value)
{
Console.WriteLine($"Product: {product.Name}, AttributeId: {attributeId}, Value: {value}");
var attribute = product.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == attributeId);

if (attribute is not null)
{
attribute.Value = value;
DbContext.ProductAttributes.Update(attribute);
await DbContext.SaveChangesAsync();
}
else
{
DbContext.ProductAttributes.Add(new ProductAttribute
{
ProductId = product.ProductId,
AttributeId = attributeId,
Value = value
});
await DbContext.SaveChangesAsync();
}
}
This is function. When I'm triggering it manually everythin works fine.
8 replies
CC#
Created by mikibanan on 1/12/2024 in #help
@oninput problem
input class="clean-input" type="number" value="@item.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == 1)?.Value"
@oninput="@(args => UpdateAttributeValue(item, 1, (string)args.Value!))"/>
input class="clean-input" type="number" value="@item.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == 1)?.Value"
@oninput="@(args => UpdateAttributeValue(item, 1, (string)args.Value!))"/>
private async Task UpdateAttributeValue(Product product, int attributeId, string value)
{
Console.WriteLine($"Product: {product.Name}, AttributeId: {attributeId}, Value: {value}");
var attribute = product.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == attributeId);

if (attribute is not null)
{
attribute.Value = value;
DbContext.ProductAttributes.Update(attribute);
await DbContext.SaveChangesAsync();
}
else
{
DbContext.ProductAttributes.Add(new ProductAttribute
{
ProductId = product.ProductId,
AttributeId = attributeId,
Value = value
});
await DbContext.SaveChangesAsync();
}
}
private async Task UpdateAttributeValue(Product product, int attributeId, string value)
{
Console.WriteLine($"Product: {product.Name}, AttributeId: {attributeId}, Value: {value}");
var attribute = product.ProductAttributes.FirstOrDefault(pa => pa.AttributeId == attributeId);

if (attribute is not null)
{
attribute.Value = value;
DbContext.ProductAttributes.Update(attribute);
await DbContext.SaveChangesAsync();
}
else
{
DbContext.ProductAttributes.Add(new ProductAttribute
{
ProductId = product.ProductId,
AttributeId = attributeId,
Value = value
});
await DbContext.SaveChangesAsync();
}
}
My @Oninput does not work. What may cause this?
2 replies
CC#
Created by mikibanan on 1/11/2024 in #help
Dapper BulkInsert
Hi, I have code like this (attachment). Why ProductQuantities are not inserting?
1 replies
CC#
Created by mikibanan on 1/11/2024 in #help
Perform BulkInsert with multiple tables in dapper
Hi, I have simple DB with 4 tables. Products, Manufacturers, Databases and ProductsQuantity. I have one csv with Name, Manufacturer and multiple quantities from different databases. How to bulk insert new products with all the relations?
4 replies
CC#
Created by mikibanan on 1/11/2024 in #help
Dapper Upsert
No description
1 replies
CC#
Created by mikibanan on 1/11/2024 in #help
Dapper problem
No description
10 replies
CC#
Created by mikibanan on 1/5/2024 in #help
Refresh object in EntityFramework
Hi, I have the situation where multiple users can work on the same order. There is a possibility that the order will be finalized and other user can still make changes, because I'm checking Order.IsFinalized, but it is not updated before. How do I refresh object with current data?
public async Task UpdateAsync(Order updatedOrder)
{

var order = await dbContext.Orders
.Include(order => order.OrderDates)
.Include(order => order.OrderPositions)
.FirstOrDefaultAsync(x => x.OrderId == updatedOrder.OrderId)
?? throw new DbException("Order not found!");

await dbContext.Entry(order).ReloadAsync();

if (updatedOrder.IsFinalized) throw new DbException("Cannot update finalized updatedOrder!");

updatedOrder.DraftModificationDate = DateTime.Now;

dbContext.Entry(updatedOrder).State = EntityState.Modified;

try
{
await dbContext.SaveChangesAsync();
}
catch (Exception e)
{
throw new DbException("Cannot update updatedOrder!", e);
}
}
public async Task UpdateAsync(Order updatedOrder)
{

var order = await dbContext.Orders
.Include(order => order.OrderDates)
.Include(order => order.OrderPositions)
.FirstOrDefaultAsync(x => x.OrderId == updatedOrder.OrderId)
?? throw new DbException("Order not found!");

await dbContext.Entry(order).ReloadAsync();

if (updatedOrder.IsFinalized) throw new DbException("Cannot update finalized updatedOrder!");

updatedOrder.DraftModificationDate = DateTime.Now;

dbContext.Entry(updatedOrder).State = EntityState.Modified;

try
{
await dbContext.SaveChangesAsync();
}
catch (Exception e)
{
throw new DbException("Cannot update updatedOrder!", e);
}
}
I tried doing something like this, but the order is not updating.
32 replies
CC#
Created by mikibanan on 12/28/2023 in #help
SqlTableDependency problems...
Hi, I have a list of orders. When I click on each of the items, properties of it shows up. I made auto save, so every time user changes name, description etc. it is automatically saved to db. Now I made this list "live", so everytime new order is created it is automatically added to the list. But now there is a problem... When I'm trying to update some properties of the order, this error shows up.
Could not save changes because the target table has database triggers. Please configure your table accordingly, see https://aka.ms/efcore-docs-sqlserver-save-changes-and-output-clause for more information.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): The target table 'Orders' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.
Could not save changes because the target table has database triggers. Please configure your table accordingly, see https://aka.ms/efcore-docs-sqlserver-save-changes-and-output-clause for more information.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): The target table 'Orders' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause.
I'm using SqlTableDependency package. Is there any way I can fix it?
6 replies