Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用EF Core加载未映射的属性_C#_Entity Framework_Entity Framework Core - Fatal编程技术网

C# 使用EF Core加载未映射的属性

C# 使用EF Core加载未映射的属性,c#,entity-framework,entity-framework-core,C#,Entity Framework,Entity Framework Core,我试图通过调用存储过程来加载带有EF Core的实体。 实体通过fluent映射映射到表,该映射不包含存储过程选择的所有列 实体配置中将忽略不作为表列存在的选定字段,如下所示: modelBuilder.Entity<CustomerLocationEntity>().Ignore(c => c.Latitude); modelBuilder.Entity<CustomerLocationEntity>().Ignore(c =>

我试图通过调用存储过程来加载带有EF Core的实体。 实体通过fluent映射映射到表,该映射不包含存储过程选择的所有列

实体配置中将忽略不作为表列存在的选定字段,如下所示:

        modelBuilder.Entity<CustomerLocationEntity>().Ignore(c => c.Latitude);
        modelBuilder.Entity<CustomerLocationEntity>().Ignore(c => c.Longitude);
        modelBuilder.Entity<CustomerLocationEntity>().Ignore(c => c.Radius);
        modelBuilder.Entity<CustomerLocationEntity>().Ignore(c => c.Distance);
await SalesContext.CustomerLocation
            .FromSql("GetCustomersByLocation @latitude={0}, @longitude={1}, @radius={2}", lat,
                lon, radius)
            .ToListAsync();

执行查询时,被忽略的列不会映射到实体。调用存储过程时,是否有可能将忽略的字段映射到实体,或者我是否必须为存储过程或类似的内容创建另一个实体?

在列上使用fluent api的忽略方法时,它不会在sql server中创建该列(忽略它)。
存储过程结果将根据创建的查询提供一些列 并且这些列名必须与实体上的属性名匹配。
例如,您的过程将为您提供一个包含以下列的表,以及sql server和您的类中匹配的数据类型:

  • 纬度
  • 经度
  • 半径
  • 距离
  • 然后,您应该为您的过程创建一个类:

    public class LocationProcedure {
       public string Latitude {get;set;}
       public string Longitude {get;set;}
       public string Radius {get;set;}
       public string Distance {get;set;}
    }
    
    并使用
    [NotMapped]
    属性将此类型的数据库集添加到dbContext中:

    [NotMapped]
    public DbSet<LocationProcedure> CustomerLocation {get; set:}
    

    你让我开心!这不是我的最终解决方案,但你让我走上了正确的道路。由于新实体有一个复合键,我需要通过fluent映射来声明该键,而无需将该实体绑定到表,并且映射工作正常。此外,Ef需要每个dbset都有一个名为Id的列或另一个主键。否则它会引发一个异常。使用带有
    [NotMapped]
    属性的
    DbSet
    不会忽略在应用migrationYes后在数据库中创建适当的表,
    [NotMapped]
    应该修饰
    LocationProcedure
    类,不是
    DbSet
    。如果我在动态生成中选择sql中的列会怎么样?那我就不能给我的Notmapped字段赋值了
    await SalesContext.CustomerLocation
                .FromSql("GetCustomersByLocation @latitude={0}, @longitude={1}, @radius={2}", lat,
                    lon, radius)
                .ToListAsync();