Skip to content

Commit cb97d8e

Browse files
authored
Merge pull request #3 from simonmau/owned-entities
Added support for owned entities
2 parents 5c23e44 + 8de3fdf commit cb97d8e

File tree

8 files changed

+214
-6
lines changed

8 files changed

+214
-6
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Enum.Ext.Tests.Shared;
2+
using Microsoft.EntityFrameworkCore;
3+
using System.ComponentModel.DataAnnotations;
4+
5+
namespace Enum.Ext.EFCore.Tests.DbContext.Entities
6+
{
7+
public class EntityWithOwnedProperty
8+
{
9+
public int Id { get; set; }
10+
11+
[Required]
12+
public OwnedStuff OwnedStuff { get; set; }
13+
}
14+
15+
[Owned]
16+
public class OwnedStuff
17+
{
18+
public Weekday Weekday { get; set; }
19+
}
20+
}

Enum.Ext/Enum.Ext.EFCore.Tests/DbContext/TestDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
2424
}
2525

2626
public DbSet<SomeEntity> SomeEntities { get; set; }
27+
28+
public DbSet<EntityWithOwnedProperty> EntitiesWithOwnedProperties { get; set; }
2729
}
2830
}

Enum.Ext/Enum.Ext.EFCore.Tests/Migrations/20220730194709_OwnedEntity.Designer.cs

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace Enum.Ext.EFCore.Tests.Migrations
6+
{
7+
public partial class OwnedEntity : Migration
8+
{
9+
protected override void Up(MigrationBuilder migrationBuilder)
10+
{
11+
migrationBuilder.CreateTable(
12+
name: "EntitiesWithOwnedProperties",
13+
columns: table => new
14+
{
15+
Id = table.Column<int>(type: "INTEGER", nullable: false),
16+
OwnedStuff_Weekday = table.Column<int>(type: "INTEGER", nullable: true)
17+
},
18+
constraints: table =>
19+
{
20+
table.PrimaryKey("PK_EntitiesWithOwnedProperties", x => x.Id);
21+
});
22+
}
23+
24+
protected override void Down(MigrationBuilder migrationBuilder)
25+
{
26+
migrationBuilder.DropTable(
27+
name: "EntitiesWithOwnedProperties");
28+
}
29+
}
30+
}

Enum.Ext/Enum.Ext.EFCore.Tests/Migrations/TestDbContextModelSnapshot.cs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using Microsoft.EntityFrameworkCore.Infrastructure;
55
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
66

7+
#nullable disable
8+
79
namespace Enum.Ext.EFCore.Tests.Migrations
810
{
911
[DbContext(typeof(TestDbContext))]
@@ -12,20 +14,54 @@ partial class TestDbContextModelSnapshot : ModelSnapshot
1214
protected override void BuildModel(ModelBuilder modelBuilder)
1315
{
1416
#pragma warning disable 612, 618
15-
modelBuilder
16-
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
17+
modelBuilder.HasAnnotation("ProductVersion", "6.0.0");
18+
19+
modelBuilder.Entity("Enum.Ext.EFCore.Tests.DbContext.Entities.EntityWithOwnedProperty", b =>
20+
{
21+
b.Property<int>("Id")
22+
.ValueGeneratedOnAdd()
23+
.HasColumnType("INTEGER");
24+
25+
b.HasKey("Id");
26+
27+
b.ToTable("EntitiesWithOwnedProperties");
28+
});
1729

1830
modelBuilder.Entity("Enum.Ext.EFCore.Tests.DbContext.Entities.SomeEntity", b =>
1931
{
2032
b.Property<int>("Id")
21-
.ValueGeneratedOnAdd();
33+
.ValueGeneratedOnAdd()
34+
.HasColumnType("INTEGER");
2235

23-
b.Property<int?>("Weekday");
36+
b.Property<int?>("Weekday")
37+
.HasColumnType("INTEGER");
2438

2539
b.HasKey("Id");
2640

2741
b.ToTable("SomeEntities");
2842
});
43+
44+
modelBuilder.Entity("Enum.Ext.EFCore.Tests.DbContext.Entities.EntityWithOwnedProperty", b =>
45+
{
46+
b.OwnsOne("Enum.Ext.EFCore.Tests.DbContext.Entities.OwnedStuff", "OwnedStuff", b1 =>
47+
{
48+
b1.Property<int>("EntityWithOwnedPropertyId")
49+
.HasColumnType("INTEGER");
50+
51+
b1.Property<int?>("Weekday")
52+
.HasColumnType("INTEGER");
53+
54+
b1.HasKey("EntityWithOwnedPropertyId");
55+
56+
b1.ToTable("EntitiesWithOwnedProperties");
57+
58+
b1.WithOwner()
59+
.HasForeignKey("EntityWithOwnedPropertyId");
60+
});
61+
62+
b.Navigation("OwnedStuff")
63+
.IsRequired();
64+
});
2965
#pragma warning restore 612, 618
3066
}
3167
}

Enum.Ext/Enum.Ext.EFCore.Tests/TestDbContext.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,43 @@ public void Test_SaveData()
4444
}
4545
}
4646
}
47+
48+
[Test]
49+
public void Test_SaveData_WithOwnedEntity()
50+
{
51+
// In-memory database only exists while the connection is open
52+
using (var connection = new SqliteConnection("DataSource=:memory:"))
53+
{
54+
connection.Open();
55+
56+
var options = new DbContextOptionsBuilder<TestDbContext>()
57+
.UseSqlite(connection)
58+
.Options;
59+
60+
using (var db = new TestDbContext(options))
61+
{
62+
db.Database.EnsureCreated();
63+
64+
db.EntitiesWithOwnedProperties.Add(new Enum.Ext.EFCore.Tests.DbContext.Entities.EntityWithOwnedProperty()
65+
{
66+
OwnedStuff = new Enum.Ext.EFCore.Tests.DbContext.Entities.OwnedStuff()
67+
{
68+
Weekday = Weekday.Thursday
69+
}
70+
});
71+
72+
db.SaveChanges();
73+
}
74+
75+
using (var db = new TestDbContext(options))
76+
{
77+
var entities = db.EntitiesWithOwnedProperties.ToList();
78+
79+
entities.Count.Should().Be(1);
80+
entities.Single().OwnedStuff.Should().NotBeNull();
81+
entities.Single().OwnedStuff!.Weekday.Should().Be(Weekday.Thursday);
82+
}
83+
}
84+
}
4785
}
4886
}

Enum.Ext/Enum.Ext.EFCore/Enum.Ext.EFCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
55
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Version>1.0.7</Version>
6+
<Version>1.1.0</Version>
77
<Authors>Mauracher Simon, Juen Marcel</Authors>
88
<PackageProjectUrl>https://github.com/simonmau/enum_ext</PackageProjectUrl>
99
<RepositoryUrl>https://github.com/simonmau/enum_ext</RepositoryUrl>

Enum.Ext/Enum.Ext.EFCore/Extensions.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
23
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
34
using System;
45
using System.Linq;
@@ -27,7 +28,18 @@ public static void ConfigureEnumExt(this ModelBuilder modelBuilder)
2728

2829
var converter = (ValueConverter)Activator.CreateInstance(converterType);
2930

30-
modelBuilder.Entity(entityType.Name).Property(property.Name).HasConversion(converter);
31+
if (entityType.IsOwned())
32+
{
33+
#pragma warning disable EF1001 // Internal EF Core API usage, might change without notice -> have not found a better way till now
34+
var navigationBuilder = new OwnedNavigationBuilder(entityType.FindOwnership());
35+
#pragma warning restore EF1001
36+
37+
navigationBuilder.Property(property.Name).HasConversion(converter);
38+
}
39+
else
40+
{
41+
modelBuilder.Entity(entityType.Name).Property(property.Name).HasConversion(converter);
42+
}
3143
}
3244
}
3345
}

0 commit comments

Comments
 (0)