admin管理员组

文章数量:1433531

I have an Entity Framework Core 8 project. Below is model class.

public class Item
{
   public string Text1 { get; set; }
   public string Text2 { get; set; }
   public string Text3 { get; set; }
   public string Text4 { get; set; }
   public string Text5 { get; set; }
   //...
}

And it uses FromSqlRaw to call database procedure to query data.

string sql = " EXEC xxx @SearchCriteria1, @SearchCriteria2 ";
List<Item> list = await this.DbContext.Items.FromSqlRaw(sql, paramValues.ToArray()).ToListAsync();

The issue is this procedure only returns columns Text1,Text2. It throws below error message.

InvalidOperationException: The required column 'Text3' was not present in the results of a 'FromSql' operation.

I know I can resolve this issue by adding [NotMapped] to this property. But this is a legacy model class, and I have marked many properties.

Besides, other class may use this model with calling different procedure, which may include Text3. If I mark it as [NotMapped], it may affect other people.

So, I am thinking: is there any way that I can keep the property here. If its value is included in the database query result, then use this database value. If its value is not presented in the database query result, just ignore this property or give it a default value.

Is there any way to achieve this?

I have an Entity Framework Core 8 project. Below is model class.

public class Item
{
   public string Text1 { get; set; }
   public string Text2 { get; set; }
   public string Text3 { get; set; }
   public string Text4 { get; set; }
   public string Text5 { get; set; }
   //...
}

And it uses FromSqlRaw to call database procedure to query data.

string sql = " EXEC xxx @SearchCriteria1, @SearchCriteria2 ";
List<Item> list = await this.DbContext.Items.FromSqlRaw(sql, paramValues.ToArray()).ToListAsync();

The issue is this procedure only returns columns Text1,Text2. It throws below error message.

InvalidOperationException: The required column 'Text3' was not present in the results of a 'FromSql' operation.

I know I can resolve this issue by adding [NotMapped] to this property. But this is a legacy model class, and I have marked many properties.

Besides, other class may use this model with calling different procedure, which may include Text3. If I mark it as [NotMapped], it may affect other people.

So, I am thinking: is there any way that I can keep the property here. If its value is included in the database query result, then use this database value. If its value is not presented in the database query result, just ignore this property or give it a default value.

Is there any way to achieve this?

Share Improve this question edited Dec 4, 2024 at 1:57 Ken White 126k15 gold badges236 silver badges466 bronze badges asked Nov 18, 2024 at 13:32 Robin SunRobin Sun 1,6403 gold badges28 silver badges52 bronze badges 4
  • I assume this Item is used other queries, so better execute SP using Dapper. It will load data without additional eforts. – Svyatoslav Danyliv Commented Nov 18, 2024 at 15:09
  • Instead of using [NotMapped] are you trying saying that the property is nullable? learn.microsoft/en-us/ef/core/modeling/… – Aluan Haddad Commented Nov 18, 2024 at 22:17
  • Hi @AluanHaddad, the property can be nuallable. What I am saying is whether I can add extra properties in Model which is not used in this db call. For example, I have one property Text100, but this is not a column of result of db procedure call. It will throw error. – Robin Sun Commented Nov 19, 2024 at 2:26
  • @RobinSun I do not know if my case will get into conflict with existing annotations, and I do not know how the conflict will be resolved if it does. (say you annotated it as required, and you configured is ignored). But I think it is worth to give it a try. – H.A.H. Commented Nov 20, 2024 at 7:38
Add a comment  | 

1 Answer 1

Reset to default 0

I suggest alternative solution to your problem.

You can switch from using annotations to using fluent API to configure your models.

I have similar to your situation, where I cannot modify the Entity files. What I can do is configure the EF to load configuration from different assembly files.

https://learn.microsoft/en-us/ef/core/modeling/#use-fluent-api-to-configure-a-model

The idea is to have two or more assemblies that have described classes as in the documentation, and call:

modelBuilder.ApplyConfigurationsFromAssembly(...);

Depending on what configuration you want to use.

This in my case leads to Properties in the model getting populated with data in one case, and left completely empty in another.

(I know it is a mess, but when people want to run the same code over different data, it is not the worst decision)

本文标签: