标签: entity-framework-core-migrations

Docker容器中的EF Core迁移

我在.NET Core 2.0中设置WebApi.我将使用Entitty Framework Core作为ORM.整个应用程序将部署为Docker Container.让我感到不安的是在这种情况下处理数据库迁移的方式.我的意思是PRODUCTION环境.以下是我设法研究的内容:

  • 我们只是在应用程序启动时触发Database.Migrate()忘记整个世界 - 嗯不知何故我不喜欢它;-)
  • 由命令行参数驱动的Database.Migrate()(使用指定的param运行docker container一次以迁移DB)
  • 登录到应用程序容器并执行 dotnet ef database update
  • 基于迁移生成普通的旧SQL并从DB管理工具执行它.似乎oldschool但有效.我讨厌的是混乱执行我自己的脚本.
  • Perpare是一个数据库容器,它已经有从上面的代码生成的脚本,并且会自动执行它们.

还有其他建议吗?或者什么是最好,最合适的解决方案?

问候

docker entity-framework-core asp.net-core-2.0 entity-framework-core-migrations

35
推荐指数
1
解决办法
4438
查看次数

ef 核心迁移不能使用秘密管理器

当我创建 .net core web 应用程序时,我在测试期间使用了秘密管理器。我通常能够创建一个新的 web 项目(mvc 和 web api),右键单击该项目并选择“管理用户机密”。这将打开一个 json 文件,我在其中添加了机密。然后我在我的 startup.cs 中使用它,如下所示:

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseMySql(Configuration["connectionString"]));
Run Code Online (Sandbox Code Playgroud)

该网站在此方面运行良好,并且可以很好地连接到数据库。但是,当我尝试使用诸如 ef 核心迁移命令时add-migration,它们似乎无法从秘密管理器访问连接字符串。我收到错误消息“连接字符串不能为空”。当我Configuration["connectionString"]用实际字符串硬编码时,错误消失了。我已经在线检查并检查了 .csproj 文件,它们已经包含以下几行:

<UserSecretsId>My app name</UserSecretsId>
Run Code Online (Sandbox Code Playgroud)

然后:

<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.1" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
Run Code Online (Sandbox Code Playgroud)

是否需要添加任何内容以便迁移可以访问连接字符串?

更新

我在上下文类中只有一个构造函数:

public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options) : base(options)
{
}
Run Code Online (Sandbox Code Playgroud)

secret-manager visual-studio-2017 asp.net-core-2.0 entity-framework-core-migrations

11
推荐指数
2
解决办法
3417
查看次数

如何处理大型efcore迁移设计器文件,这会降低构建和IDE的速度

我目前有一个efcore 2.1项目,包含大约230个实体和大约350个迁移.每次添加efcore迁移时,都会创建一个设计器文件.此文件大约为535 kb并且正在增长(对于alle设计器文件总计为150mb).这使得IDE缓慢且无响应,重构是不行的,它也使构建过程变慢.如果我删除所有设计器文件,则构建从110秒减少到20秒,IDE再次变得快速.

但是,一旦删除所有设计器文件,我就无法使用"dotnet ef database"命令.

我之前也合并了所有迁移.这是有效的,除了在团队设置中有一些问题这样做(必须在每个开发人员机器上运行手动命令,没有团队成员可以进行任何未同步的迁移等)并且这只是暂时的,因为迁移在一段时间后再次开始堆积.

我很好奇是否有其他项目有同样的问题,以及他们如何解决这个问题?

entity-framework ef-migrations entity-framework-core entity-framework-core-migrations entity-framework-core-2.1

11
推荐指数
1
解决办法
195
查看次数

EF Core 2.0如何使用SQL存储过程

我是EF Core 2.0的新手,具有存储过程.

任何人都可以帮助我在EF Core 2.0代码优先方法中使用存储过程吗?

在我以前的项目中,我有一个.edmx模型文件,我正在使用如下的上下文:

public IEnumerable<UserResult> GetUserResults(Entities context)
{
    if (context == null) return new List<UserResult>();
    return context.spGetUsers().Where(u => u.IsDeleted == false);
}
Run Code Online (Sandbox Code Playgroud)

上下文是:

public virtual ObjectResult<UserResult> spGetUsers()
{
    return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<UserResult>("spGetUsers");
}
Run Code Online (Sandbox Code Playgroud)

谢谢

stored-procedures entity-framework-core asp.net-core-2.0 entity-framework-core-migrations

9
推荐指数
2
解决办法
2万
查看次数

EF Core中的SQL Server排序规则

如何使用注释或Fluent API指定列,表或数据库的排序规则?我知道MySql Provider有一些简洁的方法.但是,除了执行原始SQL命令之外,我找不到SQL Server的任何方法.

asp.net-mvc ef-code-first entity-framework-core ef-core-2.0 entity-framework-core-migrations

9
推荐指数
2
解决办法
762
查看次数

尝试使用 EF Core 7 添加迁移时,“片段”没有实现异常

当我尝试使用 EF Core (7.0.0-rc.1.22426.7) 添加迁移时,我在控制台中看到以下错误:

System.TypeLoadException: Method 'Fragment' in type 'Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper' from assembly 'Microsoft.EntityFrameworkCore.Design, Version=6.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation.
   at Microsoft.EntityFrameworkCore.Design.DesignTimeServiceCollectionExtensions.<>c__DisplayClass0_0.<AddEntityFrameworkDesignTimeServices>b__0(ServiceCollectionMap services)
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.TryAddProviderSpecificServices(Action`1 serviceMap)
   at Microsoft.EntityFrameworkCore.Design.DesignTimeServiceCollectionExtensions.AddEntityFrameworkDesignTimeServices(IServiceCollection services, IOperationReporter reporter, Func`1 applicationServiceProviderAccessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.CreateServiceCollection(DbContext context)
   at Microsoft.EntityFrameworkCore.Design.Internal.DesignTimeServicesBuilder.Build(DbContext context)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Method 'Fragment' in type 'Microsoft.EntityFrameworkCore.Design.Internal.CSharpHelper' from assembly 'Microsoft.EntityFrameworkCore.Design, Version=6.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core entity-framework-core-migrations ef-core-7.0

9
推荐指数
2
解决办法
7971
查看次数

使用外键关系创建的 EF Core 2 重复列

我正在尝试使用 EF 核心 2 代码优先方法添加迁移。问题是,具有外键关系的实体是使用末尾带有“1”后缀的外键 id 和具有相同名称但末尾没有 1 的冗余列创建的,这不是外键。

示例是我的 2 个类,Store 和 StoreVisit,如下所示:

店铺

[Table("Store")]
public class Store
{
    public Store()
    {
        StoreVisits = new HashSet<StoreVisit>();
    }
    [Key]
    public int StoreId { get; set; }

    [StringLength(30)] 
    public string ShopName { get; set; }

    [StringLength(50)]
    public string ShopKeeper { get; set; } 

    public string ContactNo { get; set; }

    [StringLength(70)]
    public string Address { get; set; }

    [StringLength(20)]
    public string Street { get; set; }

    [StringLength(50)] 
    public string City { …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core asp.net-core-2.0 entity-framework-core-migrations

8
推荐指数
1
解决办法
4642
查看次数

EF Core 迁移 - 多个数据库

有没有办法在具有相同表集的多个数据库上运行 EF Core 迁移。这适用于多租户架构,其中有一个主数据库(具有所有租户数据库的元数据,包括租户数据库连接字符串)和每个租户一个具有相同数据库对象集的数据库。我们需要能够在 SaaS 模型中自动创建新的租户数据库时运行这些迁移,并且在数据库结构发生更改(新列、数据类型更改、新索引等)时也运行这些迁移。

multi-tenant entity-framework-core-migrations

8
推荐指数
1
解决办法
2072
查看次数

添加迁移值不能为空.参数名称:语言

我试图创建一个新的迁移,但我得到一个System.ArgumentNullException说:

System.ArgumentNullException: Value cannot be null.
Parameter name: language
    at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, 
    String parameterName)
    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations..ctor
    (IOperationReporter reporter, Assembly assembly, Assembly 
    startupAssembly, String projectDir, String rootNamespace, String 
    language)
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.<>c__DisplayClass4_0.<.ct
    or>b__4()
    at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Value cannot be null.
Parameter name: language
Run Code Online (Sandbox Code Playgroud)

c# entity-framework-core entity-framework-core-migrations

6
推荐指数
2
解决办法
4100
查看次数

调试 EF Core Add-Migrations 调用的代码

我使用 IDesignTimeDbContextFactory<> 模式在单独的程序集中定义了一个 Entity Framework Core 数据库(即,我定义一个派生自 IDesignTimeDbContextFactory 的类,该类具有一个名为 CreateDbContext 的方法,该方法返回数据库上下文的实例)。

由于 EF Core 数据库所属的应用程序使用 AutoFac 依赖注入,因此 IDesignTimeDbContextFactory<> 工厂类在其构造函数中创建 AutoFac 容器,然后解析 DbContextOptionsBuilder<> 派生类,该类被馈送到数据库的构造函数中(我这样做是为了根据配置文件设置,控制是否将本地数据库或基于 Azure 的 SqlServer 数据库作为目标,并将密码存储在 Azure KeyVault 中):

public class TemporaryDbContextFactory : IDesignTimeDbContextFactory<FitchTrustContext>
{
    private readonly FitchTrustDBOptions _dbOptions;

    public TemporaryDbContextFactory()
    {
        // OMG, I would >>never<< have thought to do this to eliminate the default logging by this
        // deeply-buried package. Thanx to Bruce Chen via 
        // /sf/ask/3358753611/#48016958
        LoggerCallbackHandler.UseDefaultLogging = false;

        var builder = new ContainerBuilder();

        builder.RegisterModule<SerilogModule>();
        builder.RegisterModule<KeyVaultModule>(); …
Run Code Online (Sandbox Code Playgroud)

entity-framework-core entity-framework-core-migrations

6
推荐指数
1
解决办法
2891
查看次数