mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 04:18:16 +08:00
v0.0.13 #4 - 修复和丰富 ICodeFirst.ConfigEntity 方法;
- 增加 FreeSql.Extensions.EFCoreModelBuilder 扩展库,现实与 EFCore 实体共存; - 增加 FreeSql.RESTful.Demo 示例项目;
This commit is contained in:
64
FreeSql.Extensions.EFCoreModelBuilder/CodeFirstExtensions.cs
Normal file
64
FreeSql.Extensions.EFCoreModelBuilder/CodeFirstExtensions.cs
Normal file
@ -0,0 +1,64 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace FreeSql.Extensions.EFCoreModelBuilder {
|
||||
|
||||
public static class CodeFirstExtensions {
|
||||
|
||||
public static void ConfigEntity(this ICodeFirst codeFirst, ModelBuilder modelBuilder) {
|
||||
|
||||
foreach (var type in modelBuilder.Model.GetEntityTypes()) {
|
||||
|
||||
codeFirst.ConfigEntity(type.ClrType, a => {
|
||||
|
||||
//表名
|
||||
var relationalTableName = type.FindAnnotation("Relational:TableName");
|
||||
if (relationalTableName != null) {
|
||||
a.Name(relationalTableName.Value?.ToString() ?? type.ClrType.Name);
|
||||
}
|
||||
|
||||
foreach (var prop in type.GetProperties()) {
|
||||
|
||||
var freeProp = a.Property(prop.Name);
|
||||
|
||||
//列名
|
||||
var relationalColumnName = prop.FindAnnotation("Relational:ColumnName");
|
||||
if (relationalColumnName != null) {
|
||||
|
||||
freeProp.Name(relationalColumnName.Value?.ToString() ?? prop.Name);
|
||||
}
|
||||
|
||||
//主键
|
||||
freeProp.IsPrimary(prop.IsPrimaryKey());
|
||||
|
||||
//自增
|
||||
freeProp.IsIdentity(prop.GetAnnotations().Where(z =>
|
||||
z.Name == "SqlServer:ValueGenerationStrategy" && z.Value.ToString().Contains("IdentityColumn") //sqlserver 自增
|
||||
|| z.Value.ToString().Contains("IdentityColumn") //其他数据库实现未经测试
|
||||
).Any());
|
||||
|
||||
//可空
|
||||
freeProp.IsNullable(prop.AfterSaveBehavior != Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Throw);
|
||||
|
||||
//类型
|
||||
var relationalColumnType = prop.FindAnnotation("Relational:ColumnType");
|
||||
if (relationalColumnType != null) {
|
||||
|
||||
var dbType = relationalColumnType.ToString();
|
||||
|
||||
if (!string.IsNullOrEmpty(dbType)) {
|
||||
|
||||
var maxLength = prop.FindAnnotation("MaxLength");
|
||||
if (maxLength != null)
|
||||
dbType += $"({maxLength})";
|
||||
|
||||
freeProp.DbType(dbType);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
<Version>0.0.12</Version>
|
||||
<Authors>FreeSql</Authors>
|
||||
<Product>FreeSql</Product>
|
||||
<Description>FreeSql ICodeFirst 扩展库,现实从 EFCore FluentAPI/Attribute 读取,从而做到无缝接入已使用 EFCore 项目开发。</Description>
|
||||
<PackageProjectUrl>https://github.com/2881099/FreeSql</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/2881099/FreeSql</RepositoryUrl>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.8" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\FreeSql\FreeSql.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
Reference in New Issue
Block a user