Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Updating foreign key ID does not update navigation property

$
0
0

This is my class:

public class tblProdLine : DBModel{    [MaxLength(50)]    public string? Name { get; set; }    [ForeignKey("Supervisor")]    public int? SupervisorID { get; set; }    public virtual tblUser? Supervisor { get; set; }}

In my window, Prodline.SupervisorID is bound to a combobox, so when the user chooses a new supervisor, its ID will be stored in SupervisorID:

<select @bind="plineModel.SupervisorID" class="form-control"><option value="">Choose a user...</option>    @foreach (var user in UserViewModel.Items)    {<option value="@user.ID">@user.Name</option>    }</select>

But as the supervisor itself isn't updated, Entity Framework saves the original Supervisor on "Update".

I thought about modifying the setter of SupervisorID in the Prodline class to update the Supervisor, but is that really the only way?

Edit:my "Put" code:

private readonly HttpClient _httpClient;public DbApiClientService(ApiClientOptions apiClientOptions){    _httpClient = new HttpClient();    _httpClient.BaseAddress = new Uri(apiClientOptions.ApiBaseAddress);}public async Task UpdateItem<T>(T item, int Id){    string endpoint = GetEndpointName<T>();    var response = await _httpClient.PutAsJsonAsync($"/api/{endpoint}/{Id}", item);    var responseContent = await response.Content.ReadAsStringAsync();private string GetEndpointName<T>(){    return typeof(T).Name;}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>