I have a WebApi powered by .NET Core 3.0, EF Core 3.0 and using MediatR. Two of its domains are Food and FoodType with their corresponding DataSets, having a 1-to-1 relationship. Executing the below line of code hangs/fails (I'm not sure what exactly happens but it jumps to the return line of code). Please note that querying the Foods dataset works just fine. Also note that FoodTypes table has records in it.
public async Task<Unit> Handle(CreateFoodType request, CancellationToken cancellationToken)
{
var foodType = await _context.FoodTypes.FirstOrDefaultAsync(type => type.Id == request.FoodTypeId);
if (foodType == null)
throw new Exception();
//...bla bla bla
return new Unit();
}
Below is a snippet of the code/configuration. Any help would be appreciated.
public class Food
{
public Guid Id { get; set; }
public int AverageWeight { get; set; }
//...
public virtual int FoodTypeId { get; set; }
public virtual FoodType FoodType { get; set; }
}
public class FoodType
{
public int Id { get; set; }
public string Name { get; set; }
public virtual Food Food { get; set; }
}
...and the DBContext class
public class DataContext : DbContext
{
public DbSet<Food> Foods { get; set; }
public DbSet<FoodType> FoodTypes { get; set; }
public DataContext(DbContextOptions<DataContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Food>()
.HasOne(f => f.FoodType)
.WithOne(ft => ft.Food)
.HasForeignKey<Food>(f => f.FoodTypeId);
}
}
Edit: Adding the Exception Stack Trace:
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.FirstOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.FirstOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Features.Foods.CreateFoodCommand.CreateRequestHandler.Handle(CreateFoodCommand request, CancellationToken cancellationToken) in [...redacted]