如何在DB First方法中不指定EF Core Fluent API的HasComputedColumnSql中指定SQL表达式? [英] How to not specify SQL expression in HasComputedColumnSql of EF Core Fluent API in DB first approach?

查看:190
本文介绍了如何在DB First方法中不指定EF Core Fluent API的HasComputedColumnSql中指定SQL表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在EF的先前版本中,要指定计算列,我们将编写:

In previous version of EF, to specify a computed column we would write:

modelBuilder
.Entity<Type>()
.Property(x => x.ComuptedProperty)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);

这是有道理的,因为已计算列的SQL表达式一次写入数据库中。

This would make sense, because the SQL expression for the computed column is written once in database.

但是,在迁移到EF Core之后,我们意识到应该将语法更改为:

However, after migrating to EF Core, we realized that the syntax should be changed into:

modelBuilder
.Entity<Type>()
.Property(x => x.ComuptedProperty)
.HasComputedColumnSql("SQL Expression should be duplicated here");

当我们首先编写代码时,这很有意义。因为EF Core在创建表时会使用此SQL表达式。

This makes sense when we go code first. Because EF Core uses this SQL expression while creating the table.

但是,对于数据库优先场景,这根本没有意义。我们试图将此参数保留为空,并引发异常:

However, for DB first scenarios this doesn't make sense at all. We tried to leave this parameter empty, and it throws an exception complaining:


字符串参数'sql'不能为空

The string argument 'sql' cannot be empty

现在,当您想要一个数据访问生成器时,情况变得更加糟糕。我们如何才能忽略此参数?

Now things get even worse when you want to have a data access generator. How can we neglect this parameter?

推荐答案

实际上是在使用 HasComputedColumnSql 时使用的在为关联表生成SQL脚本时,必须指定将用于计算列的SQL查询。就像您说的那样,这仅对代码优先方法有用。

Indeed when using HasComputedColumnSql you must specfiy the SQL query that will be used for the computed column when generating SQL Script for the associated table. Like you say, this is useful only for Code First approach.

在数据库优先方法中,可以使用以下方法之一: PropertyBuilder< TProperty> 类型(描述为这些方法的XML文档):

In Database First approach, you can use one of the following methods frol PropertyBuilder<TProperty> type (description are from XML documentaiton of those methods):


  • ValueGeneratedOnAdd():配置属性只有在保存新实体时才会生成一个值,除非已设置非空非临时值,在这种情况下,将保存设置值。该值可以由客户端值生成器生成,也可以由数据库作为保存实体的一部分生成。

  • ValueGeneratedOnAddOrUpdate():将属性配置为具有在保存新实体或现有实体时生成的值。

  • ValueGeneratedOnUpdate():配置属性属性以在保存现有实体时生成一个值。

  • ValueGeneratedOnAdd(): Configures a property to have a value generated only when saving a new entity,unless a non-null, non-temporary value has been set, in which case the set value will be saved instead. The value may be generated by a client-side value generator or may be generated by the database as part of saving the entity.
  • ValueGeneratedOnAddOrUpdate(): Configures a property to have a value generated when saving a new or existing entity.
  • ValueGeneratedOnUpdate(): Configures a property to have a value generated when saving an existing entity.

在您的情况下,因为它是计算列,所以添加时可能会生成该值并保存数据,因此您必须使用 ValueGeneratedOnAddOrUpdate()方法。 EF文档再次说:

In your case because it's a computed column then the value maybe generated when adding and saving the data so you must use ValueGeneratedOnAddOrUpdate() method. Again EF documentation say that :


这只是让EF知道为添加或更新的实体生成的值,并不保证EF将设置生成值的实际机制。

This just lets EF know that values are generated for added or updated entities, it does not guarantee that EF will setup the actual mechanism to generate values.

这篇关于如何在DB First方法中不指定EF Core Fluent API的HasComputedColumnSql中指定SQL表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆