Skip to content

Commit

Permalink
fix build ef core schema DynamicDataContext #48
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Voronov committed Aug 8, 2020
1 parent 0b8b5a1 commit 7dfd417
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 12 deletions.
12 changes: 6 additions & 6 deletions dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
<ComponentModelAnnotations>4.7.0</ComponentModelAnnotations>
<CoreFxVersion>4.7.2</CoreFxVersion>
<CSharpVersion>8.0</CSharpVersion>
<EfCoreVersion>3.1.4</EfCoreVersion>
<EfCoreVersion>3.1.6</EfCoreVersion>
<FrameworkVersion>net48</FrameworkVersion>
<JsonNetVersion>12.0.3</JsonNetVersion>
<NetStandardVersion>netstandard2.1</NetStandardVersion>
<NetCoreAppVersion>netcoreapp3.1</NetCoreAppVersion>
<NetCoreVersion>3.1.4</NetCoreVersion>
<NpgsqlVersion>3.1.3</NpgsqlVersion>
<ODataLibVersion>7.6.4</ODataLibVersion>
<OdataToEntityVersion>2.5.0</OdataToEntityVersion>
<NetCoreVersion>3.1.6</NetCoreVersion>
<NpgsqlVersion>3.1.4</NpgsqlVersion>
<ODataLibVersion>7.7.0</ODataLibVersion>
<OdataToEntityVersion>2.5.1</OdataToEntityVersion>
<SystemInteractiveAsyncVersion>4.1.1</SystemInteractiveAsyncVersion>
<TestSdkVersion>16.6.1</TestSdkVersion>
<TestSdkVersion>16.7.0</TestSdkVersion>
<Version>$(OdataToEntityVersion)</Version>
<XunitVersion>2.4.1</XunitVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.EntityFrameworkCore.Metadata.Conventions;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace OdataToEntity.EfCore.DynamicDataContext.ModelBuilder
{
public sealed class DynamicConventionSetPlugin : IConventionSetPlugin
{
public ConventionSet ModifyConventions(ConventionSet conventionSet)
{
KeyDiscoveryConvention keyDiscoveryConvention = conventionSet.EntityTypeAddedConventions.OfType<KeyDiscoveryConvention>().Single();
foreach (PropertyInfo propertyInfo in typeof(ConventionSet).GetProperties())
if (propertyInfo.GetValue(conventionSet) is IList list)
list.Remove(keyDiscoveryConvention);
return conventionSet;
}
}

public sealed class DynamicConventionSetBuilder : RuntimeConventionSetBuilder
{
public DynamicConventionSetBuilder(IProviderConventionSetBuilder providerConventionSetBuilder, IEnumerable<IConventionSetPlugin> plugins)
: base(providerConventionSetBuilder, new[] { new DynamicConventionSetPlugin() })
{
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private EntityType CreateEntityType(Microsoft.EntityFrameworkCore.ModelBuilder m
if (property.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity)
efProperty.SetValueGenerated(ValueGenerated.OnAdd, ConfigurationSource.Explicit);
else if (property.DatabaseGeneratedOption == DatabaseGeneratedOption.Computed)
efProperty.SetValueGenerated(ValueGenerated.OnAddOrUpdate, ConfigurationSource.Explicit);
efProperty.SetBeforeSaveBehavior(PropertySaveBehavior.Ignore);
else
efProperty.SetValueGenerated(ValueGenerated.Never, ConfigurationSource.Explicit);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ public override bool IsDatabaseGenerated(PropertyInfo propertyInfo)
{
IProperty efProperty = efEntityType.FindProperty(propertyInfo.Name);
if (efProperty != null)
return efProperty.ValueGenerated != ValueGenerated.Never;
return efProperty.ValueGenerated != ValueGenerated.Never ||
efProperty.GetBeforeSaveBehavior() == PropertySaveBehavior.Ignore;
}

throw new InvalidOperationException("property " + propertyInfo.Name + " not found");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class OeLinq2DbEdmModelMetadataProvider : OeEdmModelMetadataProvid
public override PropertyInfo[]? GetForeignKey(PropertyInfo propertyInfo)
{
var association = (AssociationAttribute)propertyInfo.GetCustomAttribute(typeof(AssociationAttribute));
if (association == null || association.IsBackReference)
if (association == null || association.IsBackReference || association.ThisKey == null)
return null;

PropertyInfo property = propertyInfo.DeclaringType.GetPropertyIgnoreCase(association.ThisKey);
Expand Down
2 changes: 1 addition & 1 deletion source/OdataToEntity.Linq2Db/OeLinq2DbOperationAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected override String GetProcedureName(Object dataContext, String operationN
protected override IReadOnlyList<OeOperationConfiguration>? GetOperationConfigurations(MethodInfo methodInfo)
{
var dbFunction = (Sql.FunctionAttribute)methodInfo.GetCustomAttribute(typeof(Sql.FunctionAttribute));
if (dbFunction == null)
if (dbFunction == null || dbFunction.Name == null)
return base.GetOperationConfigurations(methodInfo);

return new[] { new OeOperationConfiguration(dbFunction.Configuration, dbFunction.Name, methodInfo, true) };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EfCoreVersion)" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(NpgsqlVersion)" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
using OdataToEntity.EfCore.DynamicDataContext.InformationSchema;
using OdataToEntity.EfCore.DynamicDataContext.ModelBuilder;
using System;

namespace OdataToEntity.EfCore.DynamicDataContext
Expand Down Expand Up @@ -35,18 +37,21 @@ private DbContextOptions<DynamicDbContext> CreateOptionsSqlServer(bool useRelati
{
var optionsBuilder = new DbContextOptionsBuilder<DynamicDbContext>();
optionsBuilder = optionsBuilder.UseSqlServer(_connectionString, opt => opt.UseRelationalNulls(useRelationalNulls));
//optionsBuilder.ReplaceService<IConventionSetBuilder, DynamicConventionSetBuilder>();
return optionsBuilder.Options;
}
private DbContextOptions<DynamicDbContext> CreateOptionsPostgreSql(bool useRelationalNulls)
{
var optionsBuilder = new DbContextOptionsBuilder<DynamicDbContext>();
optionsBuilder.UseNpgsql(_connectionString, opt => opt.UseRelationalNulls(useRelationalNulls));
//optionsBuilder.ReplaceService<IConventionSetBuilder, DynamicConventionSetBuilder>();
return optionsBuilder.Options;
}
private DbContextOptions<DynamicDbContext> CreateOptionsMySql(bool useRelationalNulls)
{
var optionsBuilder = new DbContextOptionsBuilder<DynamicDbContext>();
optionsBuilder.UseMySql(_connectionString, opt => opt.UseRelationalNulls(useRelationalNulls));
//optionsBuilder.ReplaceService<IConventionSetBuilder, DynamicConventionSetBuilder>();
return optionsBuilder.Options;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="$(EfCoreVersion)" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="$(EfCoreVersion)" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="$(NpgsqlVersion)" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.1" />
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="3.1.2" />
<PackageReference Include="xunit" Version="$(XunitVersion)" />
<PackageReference Include="xunit.runner.visualstudio" Version="$(XunitVersion)" />
</ItemGroup>
Expand Down

0 comments on commit 7dfd417

Please sign in to comment.