升级到 EF Core 6 后,在添加迁移时,我遇到了这个恼人的警告:
No store type was specified for the decimal property '{property}' on entity type '{entityType}'. This will cause values to be silently truncated if they do not fit in the default precision and scale. Explicitly specify the SQL server column type that can accommodate all the values in 'OnModelCreating' using 'HasColumnType', specify precision and scale using 'HasPrecision', or configure a value converter using 'HasConversion'.
Run Code Online (Sandbox Code Playgroud)
此处定义: https: //github.com/dotnet/efcore/blob/main/src/EFCore.SqlServer/Properties/SqlServerStrings.Designer.cs
我不确定是否理解此警告的目的,因为带有标签的实体 [Keyless]并不存在于数据库中。另外添加诸如 之类的属性似乎 [Column(TypeName = "decimal(28, 6)")]并不能清除有关无密钥实体的警告。
我做了一个小测试,启用惰性loading.optionsBuilder.UseLazyLoadingProxies().UseSqlServer(ConnectionString);
(使用 EF Core 2.1.4)
我循环浏览有和没有的仪器,这是我得到的结果
情况1
var instruments = db.instruments.OrderBy(t=>t.id).Include(t=>t.NavPro1).ThenInclude(t=>t.NavPro2).Take(200);
Run Code Online (Sandbox Code Playgroud)
案例2
var instruments = db.instruments.OrderBy(t=>t.id).Include(t=>t.NavPro1).ThenInclude(t=>t.NavPro2).Take(200);
Run Code Online (Sandbox Code Playgroud)
然后
foreach (var i in instruments)
{
var props = i.NavPro1;
foreach (var prop in props)
{
sbPrintGreeks.AppendLine(prop.NavPro2.Name + " - " + prop.id + " - " + prop.Value);
}
}
Run Code Online (Sandbox Code Playgroud)
无需延迟加载,只需 7 秒即可获取 100k 行
使用延迟加载需要 160 秒才能获取 3k 行。
可以做些什么来获得良好的性能?
无法将外键 {'ClassExtId'} 添加到实体类型“ClassInt”,因为实体类型“Instrument”上已存在相同属性的外键,并且还针对“ClassExt”上的键 {'ClassExtId'}。
假设我从 ClassInt 继承了 ClassFoo 和 ClassBar。它们中的每一个都有对同一列 ClassExtId 的引用。为什么实体框架核心脚手架(v3)抱怨?
使用插值字符串发送sql服务器查询,为什么要在LINQ to SQL上使用datacontext,则需要添加单引号?
db.ExecuteCommand($"delete table where date = '{date:yyyy-MM-dd}'");
而使用EF Core,您需要删除它们吗?
db.Database.ExecuteSqlCommand($"delete table where date = {date:yyyy-MM-dd}");
Run Code Online (Sandbox Code Playgroud)
以及为什么在EF Core中,如果使用String.Format而不是插值,则需要放回单引号:
String.Format("delete table where date='{0}'", date.ToString("yyyy-MM-dd"));
Run Code Online (Sandbox Code Playgroud) 经过几年的开发,我的 C#/.Net 程序在尝试连接到 MS Sql 服务器数据库时遇到了这个问题。我不知道如何修复它。它仅在尝试在特定表上写入时发生。清理完那张桌子后,我注意到这个问题的频率降低了,但它仍然不时发生;
这是我得到的两个例外:
System.Data.SqlClient.SqlException (0x80131904):与服务器成功建立连接,但在登录前握手过程中发生错误。(提供者:SSL 提供者,错误:0 - 等待操作超时。)---> System.ComponentModel.Win32Exception (0x80004005):等待操作超时在 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) ...在 System.Data.Linq.DataQuery
1.System.Linq.IQueryProvider.Execute[S](Expression >expression) at System.Linq.Queryable.SingleOrDefault[TSource](IQueryable1 源)
2.
System.Data.SqlClient.SqlException (0x80131904):连接超时已过期。尝试使用登录前握手确认时超时时间已过。这可能是因为登录前握手失败或服务器无法及时响应。尝试连接到此服务器所花费的时间为 - [Pre-Login] 初始化 = 42511;握手=6001;---> System.ComponentModel.Win32Exception (0x80004005): System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnection ...Internal&
是否可以在我通过Visual Studio的调试工具调试程序时运行特定代码?
如果我使用#if DEBUG或Conditional(“DEBUG”)在/ Debug目录中运行.exe时仍然触发代码.