标签:guid extension cto optional ret 索引 type loading soft
ef6转化为efcore
1.ef6 sql service先修改为 ef6 pgsql
1.1 安装 EntityFramework6.Npgsql 包
1.2 修改config 文件中的连接串
新建一个pagsql的数据库 JustTest
<connectionStrings>
<add name="ZCParking/Statistics" connectionString="Server=10.168.*.*;port=5432;Database=JustTest;User Id=****;Password=****;" providerName="Npgsql" />
</connectionStrings>
1.3 config 添加pgsql 相关配置
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
<!-- setting the default connection factory is optional -->
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Provider" invariant="Npgsql" description=".NET Framework Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Version=4.0.10.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
</DbProviderFactories>
</system.data>
1.4 删除原先迁移和修复代码报错
删除时间类型的精度
[Column(TypeName = "datetime2")]
1.5 生成新迁移
注意点 :原先数据库有guid 默认值的去除
原先时间戳类型的字段去除
生成出迁移脚本 到justtest库执行
2.ef6升级到efcore 3.1
2.1 卸载 ef6包 增加efcore包 修复报错
卸载
EntityFramework,EntityFramework6.Npgsql 包
新增
Npgsql.EntityFrameworkCore.PostgreSQL.NetTopologySuite
2.2 通过net core power tools 工具 反向生成 codefirst
选择数据库源为postgreSQL Database

填写需要反向生成的数据库 ip等信息

勾选需要生成的表

勾选相应的配置

2.3 生成出的代码与之前的codefirst对比 并添加注释
context文件中 entity.Property(e => e.AmountActually).HasPrecision(18, 2);
可以替换成
默认生成出来的索引和外键在各自的实体类中
可定义在context文件中
支持注入
using Microsoft.EntityFrameworkCore;
using ZCParking.Entities.Statistics;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddZCParkingEntitiesStatistics(this IServiceCollection services, string connectionString)
{
return services.AddDbContextPool<ZCParkingEntities_Statistics>(options =>
{
options.UseNpgsql(connectionString, x =>
{
x.UseNetTopologySuite();
});
});
}
}
}
自己在转化的过程中的步骤 记录一下 仅供参考
Sql Service Code First转化为 Pgsql CodeFirst
标签:guid extension cto optional ret 索引 type loading soft
原文地址:https://www.cnblogs.com/rdqa/p/14030206.html