You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, when I try to use BulkRead to read entities from a SQL Server view, the method throws an exception 'Entity does not contain a table name'. When I look into the code, I think the IEntityType.GetTableName() form Entity Framework Core is used. This method indeed will return null for entities mapped to SQL views. IEntityType.GetViewName() should probably be used instead.
Is it possible to add support to BulkExtensions.BulkRead to read entities from a view? That would be great!
Versions being used by me at the moment:
Microsoft.EntityFrameworkCore version 7.0.11
EFCore.BulkExtensions version 7.1.6
entity definition:
public class MyViewItem
{
public long Id { get; set; }
public int TenantId { get; set; }
[Required]
[MaxLength(100)]
public string Description { get; set; }
}
DbContext:
public class MyContext : DbContext
{
public DbSet<MyViewItem> MyViewItems { get; set; }
public MyContext(DbContextOptions<MyContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<MyViewItem>(entity =>
{
entity.HasKey(item => new { item.TenantId, item.Id });
entity.ToView("MyView");
});
}
}
Code to read (which throws the exception):
public async Task<IEnumerable<MyViewItem>> ReadViewItems(List<long> ids)
{
var tenantId = 1;
var entities = ids
.Select(id => new MyViewItem { Id = id, TenantId = tenantId })
.ToList();
using (var context = _contextFactory.Create())
{
await context.BulkReadAsync(entities, config =>
{
config.UpdateByProperties = new List<string> { nameof(MyViewItem.TenantId), nameof(MyViewItem.Id) };
}).ConfigureAwait(false);
return entities;
}
}
SQL Server view
CREATE OR ALTER VIEW [MyView] AS
SELECT [TenantId]
, [Id]
, [Description]
FROM [MyTable]
The text was updated successfully, but these errors were encountered:
The support for bulk reading from a sql view should be fairly easy I think. The when the IEntityType.GetTableName() from Entity Framework Core returns null, IEntityType.GetViewName() can be used as a second check?
Hi, when I try to use
BulkRead
to read entities from a SQL Server view, the method throws an exception 'Entity does not contain a table name'. When I look into the code, I think theIEntityType.GetTableName()
form Entity Framework Core is used. This method indeed will returnnull
for entities mapped to SQL views.IEntityType.GetViewName()
should probably be used instead.Is it possible to add support to BulkExtensions.BulkRead to read entities from a view? That would be great!
Versions being used by me at the moment:
entity definition:
DbContext:
Code to read (which throws the exception):
SQL Server view
The text was updated successfully, but these errors were encountered: