<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.5.43" />
</ItemGroup>
public class CMSDBContext : DbContext
{
public CMSDBContext(DbContextOptions<CMSDBContext> options)
: base(options)
{
}
// Dbsets
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("dbo");
base.OnModelCreating(modelBuilder);
CustomDateConverter.DateToUTC(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
}
public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
DateTime currentDateTime = DateTime.UtcNow;
foreach (var auditableEntity in ChangeTracker.Entries<IAuditable>())
{
if (auditableEntity.State == EntityState.Added || auditableEntity.State == EntityState.Modified)
{
switch (auditableEntity.State)
{
case EntityState.Added:
auditableEntity.Entity.CreatedAt = currentDateTime;
break;
case EntityState.Modified:
auditableEntity.Entity.UpdatedAt = currentDateTime;
break;
}
}
}
return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}
}
注册服务:
public static IServiceCollection AddPresistenceInfrastructure(this IServiceCollection services)
{
services.AddDbContext<CMSDBContext>(options => options.UseSqlServer(SettingManager.CMSConnectionString));
#region Repositories
services.AddTransient(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddTransient(typeof(IReadonlyRepository<>), typeof(ReadonlyRepository<>));
services.AddTransient(typeof(ISectionRepository), typeof(SectionRepository));
#endregion
#region Cache
var redisConfigSection = SettingManager.GetSection(RedisSettings.SectionName);
services.AddSingleton<IConnectionMultiplexer>(sp => ConnectionMultiplexer.Connect(new ConfigurationOptions
{
EndPoints = { $"{redisConfigSection["Server"]}:{redisConfigSection["Port"]}" },
AllowAdmin = true,
AbortOnConnectFail = false,
}));
services.AddSingleton<ICacheService, RedisCacheService>();
#endregion
return services;
}
appsettings.json
:
"CMSConnection": "Server=Sandy-NB\\LOCALSERVER;Database=cms_db;User Id=sa;Password=xxxx;"
PM> Update-database --verbose
Build started...
Build succeeded.
Unable to create an object of type 'CMSDBContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
关于将项目作为创业项目的解决方案已经尝试过了,但没有成功。
我尝试了最初创建一个空数据库,并使用数据库名称再次尝试,但仍然没有成功。
我相当确定代码没有问题,因为在其他机器上之前是可以工作的,那么可能导致这个问题的可能原因是什么呢?
我安装了Entity Framework Core、Entity Framework Core SQL Server、Entity Framework Tools的应用程序层和基础设施层版本为6.0.1。
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="StackExchange.Redis" Version="2.5.43" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Core\CMS.Application\CMS.Application.csproj" />
<ProjectReference Include="..\..\Core\CMS.Domain\CMS.Domain.csproj" />
<ProjectReference Include="..\..\Shared\CMS.Shared.Common\CMS.Common.csproj" />
</ItemGroup>