Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit 76bbc7f

Browse files
author
Sergey Komisarchik
authored
EF Core 5 views support (#89)
* EF Core 5 views support * typo fix
1 parent 41b69fa commit 76bbc7f

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public T[] GetAttributes<T>(Type type, bool inherit = true) where T : Attribute
5555
{
5656
if (typeof(T) == typeof(TableAttribute))
5757
{
58-
return new[] { (T)(Attribute)new TableAttribute(et.GetTableName()) { Schema = et.GetSchema() } };
58+
var storeObjectId = GetStoreObjectIdentifier(et);
59+
return new[] { (T)(Attribute)new TableAttribute(storeObjectId!.Value.Name) { Schema = storeObjectId!.Value.Schema } };
5960
}
6061
if (typeof(T) == typeof(QueryFilterAttribute))
6162
{
@@ -156,7 +157,7 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
156157
.FirstOrDefault(v => CompareProperty(v.p, memberInfo))?.index ?? 0;
157158
}
158159

159-
var storeObjectId = StoreObjectIdentifier.Create(et, StoreObjectType.Table);
160+
var storeObjectId = GetStoreObjectIdentifier(et);
160161

161162
return new T[]{(T)(Attribute) new ColumnAttribute
162163
{
@@ -337,6 +338,15 @@ public override int GetHashCode()
337338
}
338339
}
339340

341+
private StoreObjectIdentifier? GetStoreObjectIdentifier(IEntityType entityType)
342+
{
343+
return entityType.GetTableName() switch
344+
{
345+
not null => StoreObjectIdentifier.Create(entityType, StoreObjectType.Table),
346+
null => StoreObjectIdentifier.Create(entityType, StoreObjectType.View),
347+
};
348+
}
349+
340350
private Sql.ExpressionAttribute? GetDbFunctionFromMethodCall(Type type, MethodInfo methodInfo)
341351
{
342352
if (_dependencies == null || _model == null)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace LinqToDB.EntityFrameworkCore.PostgreSQL.Tests.Models.NpgSqlEntities
2+
{
3+
public class EventView
4+
{
5+
public string Name { get; set; } = null!;
6+
}
7+
}

Tests/LinqToDB.EntityFrameworkCore.PostgreSQL.Tests/Models/NpgSqlEntities/NpgSqlEnititesContext.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
1414
modelBuilder.Entity<Event>(entity =>
1515
entity.Property(e => e.Duration).HasColumnType("tsrange")
1616
);
17+
18+
modelBuilder.Entity<EventView>(entity =>
19+
{
20+
entity.HasNoKey();
21+
entity.ToView("EventsView", "views");
22+
});
1723
}
1824

1925
public virtual DbSet<Event> Events { get; set; } = null!;

Tests/LinqToDB.EntityFrameworkCore.PostgreSQL.Tests/NpgSqlTests.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,20 @@ public NpgSqlTests()
2929
_options = optionsBuilder.Options;
3030
}
3131

32-
private NpgSqlEnititesContext CreateNpgSqlExntitiesContext()
32+
private NpgSqlEnititesContext CreateNpgSqlEntitiesContext()
3333
{
3434
var ctx = new NpgSqlEnititesContext(_options);
3535
ctx.Database.EnsureDeleted();
3636
ctx.Database.EnsureCreated();
37+
ctx.Database.ExecuteSqlRaw("create schema \"views\"");
38+
ctx.Database.ExecuteSqlRaw("create view \"views\".\"EventsView\" as select \"Name\" from \"Events\"");
3739
return ctx;
3840
}
3941

40-
4142
[Test]
4243
public void TestFunctionsMapping()
4344
{
44-
using (var db = CreateNpgSqlExntitiesContext())
45+
using (var db = CreateNpgSqlEntitiesContext())
4546
{
4647
var date = DateTime.Now;
4748

@@ -54,6 +55,17 @@ public void TestFunctionsMapping()
5455
}
5556
}
5657

57-
58+
[Test]
59+
public void TestViewMapping()
60+
{
61+
using (var db = CreateNpgSqlEntitiesContext())
62+
{
63+
var query = db.Set<EventView>().Where(e =>
64+
e.Name.StartsWith("any"));
65+
66+
var efResult = query.ToArray();
67+
var l2dbResult = query.ToLinqToDB().ToArray();
68+
}
69+
}
5870
}
5971
}

0 commit comments

Comments
 (0)