Skip to content

Commit da42075

Browse files
committed
# Conflicts: # src/Example/Program.cs
2 parents 61b4c7f + d8b5e3e commit da42075

File tree

68 files changed

+4501
-327
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+4501
-327
lines changed

.vscode/launch.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
// 使用 IntelliSense 了解相关属性。
3+
// 悬停以查看现有属性的描述。
4+
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": ".NET Core Launch (console)",
9+
"type": "coreclr",
10+
"request": "launch",
11+
"preLaunchTask": "build",
12+
"program": "${workspaceFolder}/src/Example/bin/Debug/net7.0/Example.dll",
13+
"args": [],
14+
"cwd": "${workspaceFolder}/src/Example",
15+
"console": "internalConsole",
16+
"stopAtEntry": false
17+
},
18+
{
19+
"name": ".NET Core Attach",
20+
"type": "coreclr",
21+
"request": "attach"
22+
}
23+
]
24+
}

.vscode/tasks.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "build",
6+
"command": "dotnet",
7+
"type": "process",
8+
"args": [
9+
"build",
10+
"${workspaceFolder}/EFCore.Taos.sln",
11+
"/property:GenerateFullPaths=true",
12+
"/consoleloggerparameters:NoSummary"
13+
],
14+
"problemMatcher": "$msCompile"
15+
},
16+
{
17+
"label": "publish",
18+
"command": "dotnet",
19+
"type": "process",
20+
"args": [
21+
"publish",
22+
"${workspaceFolder}/EFCore.Taos.sln",
23+
"/property:GenerateFullPaths=true",
24+
"/consoleloggerparameters:NoSummary"
25+
],
26+
"problemMatcher": "$msCompile"
27+
},
28+
{
29+
"label": "watch",
30+
"command": "dotnet",
31+
"type": "process",
32+
"args": [
33+
"watch",
34+
"run",
35+
"--project",
36+
"${workspaceFolder}/EFCore.Taos.sln"
37+
],
38+
"problemMatcher": "$msCompile"
39+
}
40+
]
41+
}

src/EFCore.Taos.Core/Extensions/TaosServiceCollectionExtensions.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
using Microsoft.EntityFrameworkCore.Storage;
2020
using Microsoft.EntityFrameworkCore.Update;
2121
using Microsoft.EntityFrameworkCore.Utilities;
22+
using Microsoft.EntityFrameworkCore.Metadata;
23+
using System.Linq;
24+
using Microsoft.EntityFrameworkCore.Query.Internal;
2225

2326
// ReSharper disable once CheckNamespace
2427
namespace Microsoft.Extensions.DependencyInjection
@@ -52,6 +55,7 @@ public static IServiceCollection AddEntityFrameworkTaos([NotNull] this IServiceC
5255
var builder = new EntityFrameworkRelationalServicesBuilder(serviceCollection)
5356
.TryAdd<LoggingDefinitions, TaosLoggingDefinitions>()
5457
.TryAdd<IDatabaseProvider, DatabaseProvider<TaosOptionsExtension>>()
58+
.TryAdd<IDatabase, TaosDatabase>()//没有用。。
5559
.TryAdd<IRelationalTypeMappingSource, TaosTypeMappingSource>()
5660
.TryAdd<ISqlGenerationHelper, TaosSqlGenerationHelper>()
5761
.TryAdd<IMigrationsAnnotationProvider, TaosMigrationsAnnotationProvider>()
@@ -63,15 +67,34 @@ public static IServiceCollection AddEntityFrameworkTaos([NotNull] this IServiceC
6367
.TryAdd<IMigrationsSqlGenerator, TaosMigrationsSqlGenerator>()
6468
.TryAdd<IRelationalDatabaseCreator, TaosDatabaseCreator>()
6569
.TryAdd<IHistoryRepository, TaosHistoryRepository>()
70+
.TryAdd<IBatchExecutor, TaosBatchExecutor>()
71+
.TryAdd<ICommandBatchPreparer, TaosCommandBatchPreparer>()
72+
.TryAdd<IQueryContextFactory, TaosQueryContextFactory>()
73+
.TryAdd<IModelCustomizer, TaosModelCustomizer>()
74+
.TryAdd<IRelationalCommandBuilderFactory, TaosEFCommandBuilderFactory>()
6675

6776
// New Query Pipeline
6877
.TryAdd<IMethodCallTranslatorProvider, TaosMethodCallTranslatorProvider>()
6978
.TryAdd<IMemberTranslatorProvider, TaosMemberTranslatorProvider>()
7079
.TryAdd<IQuerySqlGeneratorFactory, TaosQuerySqlGeneratorFactory>()
7180
.TryAdd<IQueryableMethodTranslatingExpressionVisitorFactory, TaosQueryableMethodTranslatingExpressionVisitorFactory>()
7281
.TryAdd<IRelationalSqlTranslatingExpressionVisitorFactory, TaosSqlTranslatingExpressionVisitorFactory>()
82+
.TryAdd<IRelationalParameterBasedSqlProcessorFactory, TaosParameterBasedSqlProcessorFactory>()
83+
7384
.TryAddProviderSpecificServices(
74-
b => b.TryAddScoped<ITaosRelationalConnection, TaosRelationalConnection>());
85+
b => b
86+
.TryAddScoped<IQueryProvider, TaosQueryProvider>()
87+
.TryAddScoped<ITaosRelationalConnection, TaosRelationalConnection>()
88+
.TryAddScoped<IRelationalCommandBuilder, TaosEFCommandBuilder>()//配置无效,未创建
89+
.TryAddScoped<IRelationalCommand, TaosEFCommand>()//配置无效,未创建
90+
)
91+
92+
.TryAdd<ISqlExpressionFactory, TaosSqlExpressionFactory>()//表达式的生成
93+
.TryAdd<IRelationalQueryStringFactory, TaosRelationalQueryStringFactory>()
94+
95+
96+
97+
;
7598

7699
builder.TryAddCoreServices();
77100

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.EntityFrameworkCore.Infrastructure;
10+
using Microsoft.EntityFrameworkCore.Internal;
11+
12+
namespace IoTSharp.EntityFrameworkCore.Taos.Infrastructure.Internal
13+
{
14+
public class TaosModelCustomizer : RelationalModelCustomizer
15+
{
16+
public TaosModelCustomizer(ModelCustomizerDependencies dependencies) : base(dependencies)
17+
{
18+
}
19+
20+
public override void Customize(ModelBuilder modelBuilder, DbContext context)
21+
{
22+
var taostabs = context.GetType().GetProperties().Where(w =>
23+
{
24+
var isDbSet = w.PropertyType.IsGenericType && w.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>);
25+
if (isDbSet)
26+
{
27+
isDbSet &= w.PropertyType.GenericTypeArguments.All(a => a.GetCustomAttribute<TaosAttribute>() != null);
28+
}
29+
30+
31+
return isDbSet;
32+
}).Select(s => s.PropertyType).ToList();
33+
foreach (var tab in taostabs)
34+
{
35+
foreach (var arg in tab.GenericTypeArguments)
36+
{
37+
modelBuilder.Entity(arg);
38+
}
39+
}
40+
base.Customize(modelBuilder, context);
41+
42+
}
43+
}
44+
}

src/EFCore.Taos.Core/IoTSharp.EntityFrameworkCore.Taos.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
<Compile Include="..\Shared\*.cs" />
2626
</ItemGroup>
2727
<ItemGroup>
28-
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.3" />
29-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
30-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.3">
28+
<PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="7.0.10" />
29+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.10" />
30+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.10">
3131
<PrivateAssets>all</PrivateAssets>
3232
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3333
</PackageReference>
34-
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.3" />
35-
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="6.0.0" />
34+
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.10" />
35+
<PackageReference Include="Microsoft.Extensions.DependencyModel" Version="7.0.0" />
3636
</ItemGroup>
3737
<ItemGroup>
3838
<None Include="..\..\docs\logo.png">
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (c) Maikebing. All rights reserved.
2+
// Licensed under the MIT License, See License.txt in the project root for license information.
3+
4+
using System.Reflection;
5+
6+
using IoTSharp.EntityFrameworkCore.Taos;
7+
8+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
9+
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
10+
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal;
11+
12+
// ReSharper disable once CheckNamespace
13+
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
14+
{
15+
public class TaosAttributeConvention : EntityTypeAttributeConventionBase<TaosAttribute>
16+
{
17+
public TaosAttributeConvention(ProviderConventionSetBuilderDependencies dependencies) : base(dependencies)
18+
{
19+
}
20+
21+
protected override void ProcessEntityTypeAdded(IConventionEntityTypeBuilder entityTypeBuilder, TaosAttribute attribute, IConventionContext<IConventionEntityTypeBuilder> context)
22+
{
23+
var tableName = attribute.TableName;
24+
if (string.IsNullOrWhiteSpace(tableName))
25+
{
26+
var type = entityTypeBuilder.Metadata.ClrType;
27+
tableName = type.Name;
28+
}
29+
30+
entityTypeBuilder.ToTable(tableName, fromDataAnnotation: true);
31+
}
32+
public override void ProcessEntityTypeAdded(IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext<IConventionEntityTypeBuilder> context)
33+
{
34+
var type = entityTypeBuilder.Metadata.ClrType;
35+
36+
var attributes = type.GetCustomAttributes<TaosAttribute>(true);
37+
foreach (var attribute in attributes)
38+
{
39+
ProcessEntityTypeAdded(entityTypeBuilder, attribute, context);
40+
}
41+
}
42+
}
43+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// Copyright (c) Maikebing. All rights reserved.
2+
// Licensed under the MIT License, See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.ComponentModel.DataAnnotations.Schema;
7+
using System.Linq;
8+
using System.Reflection;
9+
10+
using IoTSharp.EntityFrameworkCore.Taos;
11+
12+
using Microsoft.EntityFrameworkCore.Diagnostics;
13+
using Microsoft.EntityFrameworkCore.Metadata.Builders;
14+
using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure;
15+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
16+
17+
// ReSharper disable once CheckNamespace
18+
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
19+
{
20+
/// <summary>
21+
/// IPropertyAddedConvention, IConvention, IPropertyFieldChangedConvention
22+
/// </summary>
23+
public class TaosColumnAttributePropertyAttributeConvention : PropertyAttributeConventionBase<TaosColumnAttribute>, IModelFinalizingConvention, IEntityTypeAddedConvention, IEntityTypeBaseTypeChangedConvention
24+
{
25+
public TaosColumnAttributePropertyAttributeConvention(ProviderConventionSetBuilderDependencies dependencies) : base(dependencies)
26+
{
27+
}
28+
29+
public void ProcessEntityTypeAdded(IConventionEntityTypeBuilder entityTypeBuilder, IConventionContext<IConventionEntityTypeBuilder> context)
30+
{
31+
32+
var entityType = entityTypeBuilder.Metadata;
33+
var taosAtt = entityType.ClrType.GetCustomAttribute<TaosAttribute>();
34+
var members = entityType.GetRuntimeProperties().Values.Cast<MemberInfo>()
35+
//.Concat(entityType.GetRuntimeFields().Values)
36+
.Select(s =>
37+
{
38+
var pmeta = entityType.FindProperty(s);
39+
var attr = s.GetCustomAttribute<TaosColumnAttribute>();
40+
if (pmeta == null && (attr != null && !attr.IsSubTableName))
41+
{
42+
pmeta = entityType.AddProperty(s);
43+
}
44+
45+
return (Menber: s, Attr: attr, Meta: pmeta);
46+
});
47+
if (taosAtt.IsSuperTable)
48+
{
49+
var haveSubTableName = members.Where(w => w.Attr != null && w.Attr.IsSubTableName).Count() > 0;
50+
if (!haveSubTableName)
51+
{
52+
throw new Exception("super table model need subtable name property");
53+
}
54+
}
55+
56+
foreach (var m in members)
57+
{
58+
if (m.Attr.IsSubTableName)
59+
{
60+
entityTypeBuilder.Ignore(m.Menber.GetSimpleMemberName(), fromDataAnnotation: true);
61+
}
62+
63+
}
64+
var keyMembers = members.Where(w => w.Attr != null && w.Attr.ColumnType == TaosDataType.TIMESTAMP || w.Attr.IsTag);
65+
if (keyMembers != null && keyMembers.Count() > 0)
66+
{
67+
var keyProps = keyMembers.Select(s =>
68+
{
69+
//var name = string.IsNullOrWhiteSpace(s.Attr.ColumnName) ? s.Menber.Name : s.Attr.ColumnName;
70+
return entityTypeBuilder.Metadata.FindProperty(s.Menber);
71+
}).Where(w => w != null).ToList();
72+
73+
if (keyProps.Count > 0)
74+
{
75+
entityTypeBuilder.PrimaryKey(keyProps, true);
76+
}
77+
}
78+
79+
80+
}
81+
82+
83+
public void ProcessEntityTypeBaseTypeChanged(IConventionEntityTypeBuilder entityTypeBuilder, IConventionEntityType newBaseType, IConventionEntityType oldBaseType, IConventionContext<IConventionEntityType> context)
84+
{
85+
if (oldBaseType == null)
86+
{
87+
return;
88+
}
89+
90+
}
91+
92+
protected override void ProcessPropertyAdded(IConventionPropertyBuilder propertyBuilder, TaosColumnAttribute attribute, MemberInfo clrMember, IConventionContext context)
93+
{
94+
95+
var property = propertyBuilder.Metadata;
96+
//var member = property.GetIdentifyingMemberInfo();
97+
98+
99+
100+
if (!string.IsNullOrWhiteSpace(attribute?.ColumnName))
101+
{
102+
if (attribute?.ColumnName == "value")
103+
{
104+
throw new Exception("column name value not allow");
105+
}
106+
propertyBuilder.HasColumnName(attribute.ColumnName, true);
107+
}
108+
else if (property.Name == "value")
109+
{
110+
throw new Exception("column name value not allow");
111+
}
112+
113+
propertyBuilder.HasColumnType(attribute.ColumnType.ToString(), true);
114+
if (attribute.ColumnLength > 0)
115+
{
116+
propertyBuilder.HasMaxLength(attribute.ColumnLength, true);
117+
}
118+
//switch (attribute.ColumnType)
119+
//{
120+
// case TaosDataType.BINARY:
121+
// case TaosDataType.NCHAR:
122+
// case TaosDataType.VARCHAR:
123+
// case TaosDataType.GEOMETRY:
124+
// case TaosDataType.VARBINARY:
125+
// propertyBuilder.HasColumnType($"{attribute.ColumnType}({attribute.ColumnLength})", true);
126+
// break;
127+
// default:
128+
// propertyBuilder.HasColumnType(attribute.ColumnType.ToString(), true);
129+
// break;
130+
//}
131+
if (attribute.IsTag)
132+
{
133+
propertyBuilder.IsRequired(true, true);
134+
}
135+
136+
}
137+
138+
public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConventionContext<IConventionModelBuilder> context)
139+
{
140+
141+
142+
}
143+
144+
145+
}
146+
}

0 commit comments

Comments
 (0)