Blazor /实体框架-将子集合添加到时更新父

时间:2020-04-08 10:18:46

标签: c# entity-framework blazor blazor-server-side

我正在将with r_cte as ( select 'test' as name, 100 as val union all select name, val + @counter from r_cte where val < ? -- define length ) insert into #temp (name, value) select rc.name, rc.val from r_cte rc; Blazor一起使用服务器端应用。

我有一个父实体Entity Framework和一个子集合Booking。我有一个页面,可在其中添加游览到Booking的Tours 收藏

Tours

我有一个主要的父预订组件,它只是预订子组件(如Tours)的外壳。我将子项集合传递给子项,就像在预订页面上一样:

public class Booking
{
   IEnumerable<Tours> Tours { get; set; }
}

但是我发现,当我添加 Tour 时,该页面不会使用新添加的Tour进行更新,除非我打电话要求再次与其子代取回Booking。

<Tours Tours="Booking.Tours" />

@code {
    protected override async Task OnInitializedAsync()
    {
        Booking = await BookingService.GetBooking(reference);
    }
}

这是我用来将实体保存到private async Task addTour(TourState state) { var newBookingTour = new BookingTour(); var newTour = await BookingTourService.CreateBookingTour(newBookingTour); // UI ONLY UPDATES WHEN I DO THIS Booking = await BookingService.GetBooking(); } 的通用method

DB

我真的应该再次打电话给public async Task<TEntity> Upsert(TEntity item) { var entity = this.Context.Update(item); await this.SaveChangesAsync(); return entity.Entity; } 吗? DB肯定应该拾取新添加的实体并将其馈送到Entity Framework吗?我觉得我在这里缺少什么!

由于我是UIEF的新手,所以有任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

我真的应该再次致电数据库吗?

否,您不应该。

当然,实体框架应该选择新添加的实体

是的

并将其提供给用户界面?

不,您必须

您只需要更新模型,而不必从数据库中获取模型,因为您已经有了新项目的引用。

假设您的服务返回了实体:

private async Task addTour(TourState state)
{
   var newBookingTour = new BookingTour();

   var newTour = await BookingTourService.CreateBookingTour(newBookingTour);        


   Booking = ExistingBookig.Tours.Add(newTour);
}