Jea*_*ean 6 entity-framework-core .net-core azure-sql-database asp.net-core
我有很多插入文件,仅用于在Azure DB上进行日志记录,并且我看到现在大多数DTU用法都被该日志记录占用。
数据库的负载不是因为插入本身,而是因为插入之后,而是因为该CreationDate
字段由数据库生成,并且EFCore在插入之后选择了它。
为了解决“ datetime2”错误转换,我将CreationDate
字段定义为entity.Property(e => e.CreationDate).HasColumnType("datetime").HasDefaultValueSql("getdate()");
因此,EF在插入后调用数据库:
(@p0 uniqueidentifier)SELECT [CreationDate]
FROM [MyLogs]
WHERE @@ROWCOUNT = 1 AND [Id] = @p0
Run Code Online (Sandbox Code Playgroud)
有没有一种方法可以避免这种行为,只是让插入后不跟踪实体?
现在,我看到的唯一方法是忽略实体模型中的CreationDate字段,因为它没有被使用。 entity.Ignore(e => e.CreationDate);
小智 0
您无需忽略该属性。创建默认构造函数并从服务器端初始化属性。
public class User
{
public User()
{
this.CreationDate= Datetime.UTC.Now;
}
public int UserId {get;set;}
public Datetime CreationDate {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
它将在您初始化类时执行。
归档时间: |
|
查看次数: |
673 次 |
最近记录: |