标签: entity-framework

在 DBFirst 场景中,在映射中添加 .HasIndex() 有什么好处?

我一直在搜索 EF Core 文档,如果在实体映射上添加 .HasIndex() 会给 DbFirst 场景带来任何好处,我找不到任何东西。

我有这个 20yo 数据库,其中创建了所有必要的表和索引,我正在映射一些表以使用 EF Core 查询它们。我想知道,在永远不会通过代码更新表架构的 DbFirst 场景中映射索引有什么好处?它会影响 EF 生成 SQL 查询的方式吗?

entity-framework ef-fluent-api db-first

3
推荐指数
1
解决办法
591
查看次数

.netTiers + Codesmith VS 实体框架 .Net C#

我对优点和缺点有疑问,当我比较netTiers(与 codesmith)和Entity Framework

这个问题被问了很多次netTiers(与 codesmith 一起),我发现的所有主题都非常古老,Entity Framework可能比当时变得如此流行的时间更早。

我实际上很舒服Entity framework(不精通,但我喜欢它并且我netTiers经常使用它)并且不太接近(使用 codesmith),这就是为什么我想知道当我看到netTiers复杂的项目时要决定什么。一种选择是留下来面对面处理所有我认为需要大量时间的问题。另一个 - 使用来自 Microsoft 的已管理和预先设计的精美实用程序(entity framework在我花了几天的时间后,这对我来说似乎很漂亮netTiers)。

.net c# entity-framework .nettiers codesmith

3
推荐指数
1
解决办法
860
查看次数

可以在 CodeFirst EntityFramework 中使用 NPGSQL 指定 JSONB GIN 索引吗?

我在 EFCore 项目中的 EF 代码优先对象上有一些 JSONB 列。我想为那些存储为 JSONB 属性/列的强类型对象内的属性设置一些 GIN 索引。
这可能吗?(目前使用 postgres:latest image)

谢谢。

postgresql entity-framework npgsql

3
推荐指数
1
解决办法
676
查看次数

Include / ThenInclude 与 EF Core 中的 where

我需要在 ThenInclude 中使用 where

        var templatesFields = await _context.Sections
        .Include(x => x.Subtitles)
        .ThenInclude(r => r.Fields.Where(t=>t.TemplatesFields.TemplateID==TemplateID))
        .ThenInclude(r => r.OptionSources)
        .ThenInclude(r => r.OptionsSourcesDetails)
        .ToListAsync();
Run Code Online (Sandbox Code Playgroud)

linq api entity-framework .net-core asp.net-core

3
推荐指数
1
解决办法
5466
查看次数

Entity Framework Core DbContext 继承问题与构造函数中的 DbOptions

在我们的项目中,我们有一个数据库,我们在其中使用 DB First 方法。由于我们使用不会被搭建的功能,我有第二个 DBContext,它继承自生成的 DBContext。这使我可以避免每次发生数据库更改时手动操作生成的数据库上下文。

因此 StartUp 中的类定义和用法如下所示:

  // the db context generated by scaffolding the database
  public partial class ApplicationDatabaseContextGenerated : DbContext
  {
    public ApplicationDatabaseContextGenerated() {}
    public ApplicationDatabaseContextGenerated(DbContextOptions<ApplicationDatabaseContextGenerated> options) : base(options) {}

    // the db sets scaffolded
  }

  // the db context used by the app with the extended functionality
  public class ApplicationDatabaseContext : ApplicationDatabaseContextGenerated
  {

    public ILogger<ApplicationDatabaseContext> Logger { get; protected set; }

    public ApplicationDatabaseContext() : base() {}

    public ApplicationDatabaseContext(DbContextOptions<ApplicationDatabaseContext> options, ILogger<ApplicationDatabaseContext> logger) : base(options)
    {
      Logger …
Run Code Online (Sandbox Code Playgroud)

entity-framework entity-framework-core .net-core asp.net-core-2.1 entity-framework-core-2.1

3
推荐指数
1
解决办法
2272
查看次数

无法在“ApplicationUser”上配置密钥,因为它是派生类型,但在 ApplicationUser 上没有密钥配置

我正在尝试使用 ApplicationUser 自定义 IdentityUser,我按照 microsoft 文章中的步骤操作,但是当我运行该应用程序时,我在方法上遇到此错误base.OnModelCreating(modelBuilder);

System.InvalidOperationException HResult=0x80131509 Message=无法在“ApplicationUser”上配置密钥,因为它是派生类型。必须在根类型“IdentityUser”上配置密钥。如果您不打算将“IdentityUser”包含在模型中,请确保它不包含在您的上下文的 DbSet 属性中、在对模型构建器的配置调用中引用,或从包含的类型的导航属性中引用在模型中。
Source=Microsoft.EntityFrameworkCore StackTrace:在 Microsoft.EntityFrameworkCore.Metadata.Internal.EntityType.SetPrimaryKey(IReadOnlyList 1 properties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList1 属性,ConfigurationSource configurationSource) 在 Microsoft.EntityFrameworkCore.Metadata.Internal.InternalEntityTypeBuilder.PrimaryKey(IReadOnlyList1 clrProperties, ConfigurationSource configurationSource) at Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder 1.HasKey(Expression 1 keyExpression) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityUserContext5.<>c__DisplayClass20_0.b__0(EntityTypeBuilder 1 b) at Microsoft.EntityFrameworkCore.ModelBuilder.Entity[TEntity](Action1 buildAction)) 在 Microsoft.AspNetCore.Identity.EntityFrameworkCore。5.OnModelCreating(ModelBuilder builder) at Microsoft.AspNetCore.Identity.EntityFrameworkCore.IdentityDbContext8.OnModelCreating(ModelBuilder builder) at Assistente_de_Estagio.Data.ApplicationDbContext.OnModelCreating(ModelBuilder modelBuilder) in C:\Users\gui\source\repos\Assistente De Estágio\Assistente de Estagio\Data\ApplicationDbContext.cs:line 40
在 Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) 在 Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context) 在 Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder ConventionSetBuilder , IModelValidator …

entity-framework asp.net-core-mvc asp.net-core

3
推荐指数
1
解决办法
4098
查看次数

使用包含到 EF for SQL IN() 的表达式获取 LINQ,其中实体上的子属性等于值

我有一个简单的需要从返回的集合中过滤掉所有父项,其中字段上没有匹配项,即从字符串中按名称调用,与呈现的值不匹配。我所追求的是,​​如果parent对象有child对象,并且child对象属性"foo"(由字符串调用)不或不等于 value barparent则从集合中适当地过滤该对象。

这是我的 linq ef 电话

var field = "bar";
var values = new List<string>{"foo","fuYu"};
var dataPage = _aim_context.ae_s_bld_c.AsNoTracking();
var result = dataPage.Where(x => 
                              DbHelper.byPropertyContains(x.udfs, field, values)
                           );
// NOTE `udfs` is a ONE-to-ONE with `ae_s_bld_c`
Run Code Online (Sandbox Code Playgroud)

我希望看到的是类似于 SQL 的东西

SELECT [m].[id],[m.udfs].[bar],
FROM [dbo].[ae_s_bld_c] AS [m]
    INNER JOIN [dbo].[ae_s_bld_c_udf] AS [m.udfs]
        ON ([m].[multitenant_id] = [m.udfs].[multitenant_id])
WHERE ([m].[multitenant_id] = 1.0)
    AND ([m.udfs].[bar] IN ('foo','fuYu')) --< Goal line
Run Code Online (Sandbox Code Playgroud)

我处理这个问题的方法是设置一个表达式来获取List<string>并生成 …

c# linq entity-framework entity-framework-core

3
推荐指数
1
解决办法
3637
查看次数

EF 迁移后 NPGSQL MapEnum 失败

根据文档enum,我添加了一个实体框架并将其注册到 NPGSQL 中:

static DataContext()
{
    NpgsqlConnection.GlobalTypeMapper.MapEnum<MyEnum>();
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ForNpgsqlHasEnum<MyEnum>();
    ...
}
Run Code Online (Sandbox Code Playgroud)

运行迁移并启动 Web 应用程序后,抛出以下异常:

CLR 枚举类型 MyEnum 必须在使用前向 Npgsql 注册,请参考文档

如果我再次重新启动应用程序,它工作得很好 - 这只会在MyEnum应用了添加的迁移后立即发生。

c# entity-framework npgsql entity-framework-core

3
推荐指数
1
解决办法
906
查看次数

EF Core In-Memmory Array mapping

I have a string array column in database

ALTER TABLE sample ADD languages VARCHAR[] NULL;
Run Code Online (Sandbox Code Playgroud)

which is mapped to model:

public string[] languages { get; set; }
Run Code Online (Sandbox Code Playgroud)

Using EF Core with PostgreSQL (Npgsql.EntityFrameworkCore.PostgreSQL 2.2.0) works out of the box.

However, when I want to test DbContext using In-Memmory (Microsoft.EntityFrameworkCore.InMemory 2.2.4) I got this error:

System.InvalidOperationException: 无法映射属性“sample.languages”,因为它的类型“string[]”不是受支持的基本类型或有效的实体类型。显式映射此属性,或使用 '[NotMapped]' 属性或使用 'OnModelCreating' 中的 'EntityTypeBuilder.Ignore' 忽略它。

因此,将其添加到模型构建器配置中可以解决 In-Memory 错误:

 builder.Property(p => p.languages)
                    .HasConversion(
                        v => string.Join("'", v),
                        v => v.Split(',', StringSplitOptions.RemoveEmptyEntries));
Run Code Online (Sandbox Code Playgroud)

但引发了新的(Npgsql):

无法在 Npgsql.NpgsqlDefaultDataReader.GetFieldValue[T](Int32 …

entity-framework npgsql ef-core-2.2

3
推荐指数
1
解决办法
1213
查看次数

EF Core:如何在 OnModelCreating 中按条件验证数据是否存在

我使用 EF Core 并想添加预定义的数据。可以通过以下代码完成:

            modelBuilder.Entity<UserPage>().HasData(new UserPage()
            {
                Name = "Homepage",
                Editable = true,
                Path = "path"
            });
Run Code Online (Sandbox Code Playgroud)

这部分代码验证具有确切数据的 UserPage 对象是否存在,如果不存在,则创建它但我只想在记录Name = "Homepage"不存在时添加新记录,否则不存在。即使名称为“主页”的记录已经存在,上面的代码也会添加新记录,但Editable = false例如。我想验证它,我尝试:

        if (UserPages.Where(p => p.Name.Equals("Homepage", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault() == null)
        {
            modelBuilder.Entity<UserPage>().HasData(new UserPage()
            {
                Name = "Homepage",
                Editable = true,
                Path = "path"
            });
        }
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误:

在创建模型时尝试使用该模型。DbContext 实例不能以任何使用正在创建的模型的方式在 OnModelCreating 中使用。

c# entity-framework seed entity-framework-core ef-core-2.0

3
推荐指数
1
解决办法
2206
查看次数