简体   繁体   English

C# 实体框架代码优先 - 忽略数据库中缺失的列

[英]C# Entity Framework code-first - ignore missing columns in database

I have a project that connects to many different SQL Server databases, all with different versions of the schema.我有一个项目连接到许多不同的 SQL 服务器数据库,所有数据库都具有不同版本的模式。 That means that some columns is only in some of the databases.这意味着某些列仅在某些数据库中。

I am using Entity Framework with a code-first approach, and my entity models are always up-to-date, so what I want is that when I select something from the database, I want the missing columns to be null when mapped to the model.我正在使用代码优先方法的实体框架,并且我的实体模型始终是最新的,所以我想要的是当我从数据库中获取 select 某些内容时,我希望缺失的列在映射到model。

In this example, I want "Col2" to be null, because "Col2_Missing" does not exist in the database:在此示例中,我希望“Col2”为 null,因为数据库中不存在“Col2_Missing”:

var result = dbContext.Set<DemoData>().Select(x => new
{
    Col1 = x.Col1,
    Col2 = x.Col2_Missing
}.ToList();

However, the code throws an exception:但是,代码抛出异常:

System.NotSupportedException: 'The specified type member 'Col2_Missing' is not supported in LINQ to Entities. System.NotSupportedException: '指定的类型成员 'Col2_Missing' 在 LINQ 中不受实体支持。 Only initializers, entity members, and entity navigation properties are supported.'仅支持初始值设定项、实体成员和实体导航属性。

I have tried making the property .IsOptional() , but as I understand that just makes the property nullable when the column is created.我已经尝试制作属性.IsOptional() ,但据我所知,这只是在创建列时使属性可为空。

The current solution is checking the schema version before the select, and then have a lot of ugly "if" in the code.当前的解决方案是检查 select 之前的模式版本,然后在代码中有很多难看的“if”。

You might want to try dapper.你可能想试试 dapper。 It's been a couple minutes since I used it, but it seems like dapper would automagically ignore attributes in the model that are not present in the resultset and also ignore attributes in the resultset that are not present in the model.我使用它已经有几分钟了,但看起来 dapper 会自动忽略结果集中不存在的 model 中的属性,并且还会忽略结果集中不存在于 model 中的属性。

However, the usage is different.但是,用法不同。 You wouldn't be using Linq to query.您不会使用 Linq 进行查询。 Instead, you would do something similar to an ADO.Net SQL Command.相反,您将执行类似于 ADO.Net SQL 命令的操作。 Execute a string sql statement or a string that represents a stored procedure in the database.执行字符串 sql 语句或表示数据库中存储过程的字符串。

Dapper is a big step above SQL Commands though since it automaps the results of the sql to collection of typed objects. Dapper 在 SQL 命令之上迈出了一大步,因为它将 sql 的结果自动映射到类型化对象的集合。 You don't need to loop through a SQL datareader.您不需要遍历 SQL 数据读取器。 You can also compose sql statements with curly brace variables... and dapper will automatically parameterize the statement for you... which helps prevent SQL injection.您还可以使用花括号变量编写 sql 语句……dapper 会自动为您参数化该语句……这有助于防止 SQL 注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#交互式窗口中的实体框架Code-First(Oracle) - Entity framework Code-First in c# interactive window (Oracle) 是否可以在不使用Entity Framework中以代码优先方式使用C#属性的情况下生成数据库字段? - Is it possible to generate database fields without using C# properties in Entity Framework, code-first approach? C#-实体框架-大种子数据代码优先 - C# - Entity Framework - Large seed data code-first 实体框架代码优先数据库未更新 - Entity Framework Code-First Database Not Updating 实体框架 5 代码优先不创建数据库 - Entity Framework 5 code-first not creating database 实体框架优先代码和现有数据库 - Entity Framework code-first and existing database 无法使用Entity Framework代码创建数据库 - 首先+没有数据库 - Can't create database with Entity Framework code-first + no database Entity Framework 6 code-first 不创建所有列 - Entity Framework 6 code-first not creating all columns 实体框架代码优先使用现有数据库时的好处 - Entity Framework Code-First Benefits when using existing database 无法使用实体框架代码优先使用凭据创建数据库? - Cannot Create Database with credentials using Entity Framework Code-First?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM