by Anonymous » 08 Mar 2025, 16:08
Ich verwende Entity Framework. In meinem Modell habe ich einen fremden Schlüssel nullbar gemacht, und es scheint in der Datenbank korrekt reflektiert zu werden. Hier ist ein Teil meiner Asset -Modellklasse: < /p>
Code: Select all
public class Asset
{
[Key]
public int AssetID { get; set; }
[Required]
[MaxLength(100)]
public string AssetName { get; set; }
[Required]
public int AssetTypeID { get; set; }
[ForeignKey("AssetTypeID")]
public AssetType AssetType { get; set; }
public int? SubtypeID { get; set; }
[ForeignKey("SubtypeID")]
public Subtype? Subtype { get; set; }
}
Wie Sie sehen können, sind die Subtypen und die Navigationseigenschaft sowohl auf nullbar festgelegt, und die Spalte in der Tabelle ist auch korrekt auf nullbar eingestellt. Wenn Sie einen neuen Asset einfügen, akzeptiert es Subtypen als NULL. Wenn Sie jedoch ein bereits vorhandenes Asset für den Subtypen aktualisieren, erhalte ich die folgende Fehler:
"Fk_assets_subypes_subyped". Der Konflikt ereignete
Code: Select all
var updatedAsset = viewModel.Asset;
using (var scope = App.ServiceProvider.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService();
SelectedAsset.AssetName = updatedAsset.AssetName;
SelectedAsset.AssetTypeID = updatedAsset.AssetTypeID;
SelectedAsset.AssetType = updatedAsset.AssetType;
if (updatedAsset.SubtypeID == -1)
{
SelectedAsset.SubtypeID = null;
SelectedAsset.Subtype = null;
}
else
{
SelectedAsset.SubtypeID = updatedAsset.SubtypeID;
SelectedAsset.Subtype = updatedAsset.Subtype;
}
SelectedAsset.Owner = updatedAsset.Owner;
SelectedAsset.Location = updatedAsset.Location;
SelectedAsset.PurchaseDate = updatedAsset.PurchaseDate;
SelectedAsset.Value = updatedAsset.Value;
SelectedAsset.Status = updatedAsset.Status;
SelectedAsset.Description = updatedAsset.Description;
context.Assets.Update(SelectedAsset);
var log = new AssetLog
{
AssetID = SelectedAsset.AssetID,
Action = "Updated",
Timestamp = DateTime.Now,
PerformedBy = AuthenticationService.Instance.CurrentUser.GetTenantProfiles().ElementAt(0).Oid
};
context.AssetLogs.Add(log);
await context.SaveChangesAsync();
}
Die Ausnahme wird auf den SavechangesaSync -Methodeaufruf geworfen. Konnte nicht verstehen, warum dies passieren könnte. Was kann ich als nächstes versuchen?
Ich verwende Entity Framework. In meinem Modell habe ich einen fremden Schlüssel nullbar gemacht, und es scheint in der Datenbank korrekt reflektiert zu werden. Hier ist ein Teil meiner Asset -Modellklasse: < /p>
[code]public class Asset
{
[Key]
public int AssetID { get; set; }
[Required]
[MaxLength(100)]
public string AssetName { get; set; }
[Required]
public int AssetTypeID { get; set; }
[ForeignKey("AssetTypeID")]
public AssetType AssetType { get; set; }
public int? SubtypeID { get; set; }
[ForeignKey("SubtypeID")]
public Subtype? Subtype { get; set; }
}
[/code]
Wie Sie sehen können, sind die Subtypen und die Navigationseigenschaft sowohl auf nullbar festgelegt, und die Spalte in der Tabelle ist auch korrekt auf nullbar eingestellt. Wenn Sie einen neuen Asset einfügen, akzeptiert es Subtypen als NULL. Wenn Sie jedoch ein bereits vorhandenes Asset für den Subtypen aktualisieren, erhalte ich die folgende Fehler:
"Fk_assets_subypes_subyped". Der Konflikt ereignete[code]var updatedAsset = viewModel.Asset;
using (var scope = App.ServiceProvider.CreateScope())
{
var context = scope.ServiceProvider.GetRequiredService();
SelectedAsset.AssetName = updatedAsset.AssetName;
SelectedAsset.AssetTypeID = updatedAsset.AssetTypeID;
SelectedAsset.AssetType = updatedAsset.AssetType;
if (updatedAsset.SubtypeID == -1)
{
SelectedAsset.SubtypeID = null;
SelectedAsset.Subtype = null;
}
else
{
SelectedAsset.SubtypeID = updatedAsset.SubtypeID;
SelectedAsset.Subtype = updatedAsset.Subtype;
}
SelectedAsset.Owner = updatedAsset.Owner;
SelectedAsset.Location = updatedAsset.Location;
SelectedAsset.PurchaseDate = updatedAsset.PurchaseDate;
SelectedAsset.Value = updatedAsset.Value;
SelectedAsset.Status = updatedAsset.Status;
SelectedAsset.Description = updatedAsset.Description;
context.Assets.Update(SelectedAsset);
var log = new AssetLog
{
AssetID = SelectedAsset.AssetID,
Action = "Updated",
Timestamp = DateTime.Now,
PerformedBy = AuthenticationService.Instance.CurrentUser.GetTenantProfiles().ElementAt(0).Oid
};
context.AssetLogs.Add(log);
await context.SaveChangesAsync();
}
[/code]
Die Ausnahme wird auf den SavechangesaSync -Methodeaufruf geworfen. Konnte nicht verstehen, warum dies passieren könnte. Was kann ich als nächstes versuchen?