文档指出,我可以像这样打开日志记录:
using (var context = new BlogContext())
{
context.Database.Log = Console.Write; // like this
context.Database.Log = logInfo => Debug.WriteLine(logInfo); // or like this
// Your code here...
// How can I turn off logging here ?
}
Run Code Online (Sandbox Code Playgroud)
我没有有关如何关闭它的任何信息。有人可以告诉我该怎么做吗?
我需要将以下C#代码转换为有效的Entity Framework 6表达式:
(f => f.GetType().GetProperty(stringParamter).GetValue(f).ToString() == anotherStringParameter)
Run Code Online (Sandbox Code Playgroud)
这个人为"Order By"部分做了这件事,但我似乎无法弄清楚"哪里"部分......
一般来说,我想在这里实现的是一种动态查询形式,用户将"选择"属性在"dropbox"中过滤,提供过滤器值和命中查询...通常人们喜欢,f => f.TargetProp == userValue但我可以'当我不知道它是哪一个时,这样做...
我有一个简单的实体,我的SQL Sever 2012数据库中有100,000个实体:
public class Entity
{
public int Id { get; set; }
public string Field1 { get; set; }
public string Field2 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想在网格中显示这些内容,分页为100,000,太多而无法在一个屏幕上显示(并且效率不高).网格应该允许排序和过滤 - 显然所有这些操作中的3个最好在服务器上完成,EF应该翻译它们.
所以,让我们按字段1排序第二页500:
var items = context.Entities.OrderBy(e => e.Field1).Skip(500).Take(500);
Run Code Online (Sandbox Code Playgroud)
执行此查询时,需要12秒!所以我挖了它,发现它的翻译如下:
SELECT TOP (500) [Extent1].[Id] AS [Id],
[Extent1].[Field1] AS [Field1],
[Extent1].[Field2] AS [Field2]
FROM (SELECT [Extent1].[Id] AS [Id],
[Extent1].[Field1] AS [Field1],
[Extent1].[Field2] AS [Field2],
row_number() OVER (ORDER BY [Extent1].[Field1] ASC) AS [row_number]
FROM [dbo].[Costs] AS [Extent1]) AS [Extent1]
WHERE …Run Code Online (Sandbox Code Playgroud) 当我尝试实例化DbContext时,我收到此消息:
System.NotSupportedException:没有与原始类型"DateTimeOffset"的概念边类型"DateTimeOffset"对应的商店类型.
我在SQL Server上使用Entity Framework版本6.
DbContext(带有抛出异常的行)的构造函数如下所示:
internal TestHubContext(string connectionStringName) : base(connectionStringName)
{
var objectContext = (this as IObjectContextAdapter).ObjectContext;
...
}
Run Code Online (Sandbox Code Playgroud)
使用代码优先创建实体,如下所示:
public class Order
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public System.Guid Id { get; set; }
[MaxLength(60)]
[Required]
public string CreatedBy { get; set; }
[Required]
public System.DateTimeOffset CreatedUtcDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
数据库迁移运行正常并生成如下表:
CREATE TABLE [dbo].[Orders](
[Id] [uniqueidentifier] NOT NULL,
[CreatedBy] [nvarchar](60) NOT NULL,
[CreatedUtcDate] [datetimeoffset](7) NOT NULL,
Run Code Online (Sandbox Code Playgroud)
因此数据库和C#代码中存在相关的数据类型.关于为什么会这样,我有点失落.
这是我得到的堆栈跟踪:
System.Data.Entity.ModelConfiguration.Edm.Services上的System.Data.Entity.SqlServer.SqlProviderManifest.GetStoreType(TypeUsage edmType)中的System.Data.Entity.SqlServer.SqlProviderManifest.GetStorePrimitiveTypeIfPostSql9(String storeTypeName,String nameForException,PrimitiveTypeKind primitiveTypeKind) System.SData.Entity.ModelConfiguration上的System.Data.Entity.ModelConfiguration.Edm.Services.PropertyMappingGenerator.Generate(EntityType entityType,IEnumerable
1 …
我可以在TSQL中执行此操作
SELECT
@TotalDays = COUNT(Days),
@TotalHours = SUM(Hours)
FROM
Schedule
WHERE
GroupID = 1
Run Code Online (Sandbox Code Playgroud)
如何在单个查询中实现这一点,我当前的代码;
var totalDays = 0;
var totalHours = 0;
totalDays = _schedule.Count(c => c.GroupID == 1);
totalHours = _schedule.Where(w => w.GroupID == 1).Sum(s => s.Hours);
Run Code Online (Sandbox Code Playgroud)
这是无效的,因为它在数据库中调用2个单独的查询
一个棘手的(令人沮丧的问题) - 也许你们大家可能很聪明地解决它:
问题
我希望能够使用Entity Frameworks读取/写入我的数据库.我有一个在Heroku(简单的脚手架)上运行的简单app rails.我想连接到这个数据库并操纵记录.好消息是我可以使用npgsql成功连接到该数据库.坏消息是我不能用实体框架来做.这是我收到的错误:
System.Data.Entity.Core.ProviderIncompatibleException:从数据库获取提供程序信息时发生错误.这可能是由实体框架使用不正确的连接字符串引起的.检查内部异常以获取详细信息,并确保连接字符串正确.---> System.Data.Entity.Core.ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串.---> System.IO.FileLoadException:无法加载文件或程序集'Npgsql,Version = 3.1.2.0,Culture = neutral,PublicKeyToken = 5d8b90d52f46fda7'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)
这是堆栈跟踪:
at Npgsql.NpgsqlServices.GetDbProviderManifestToken(DbConnection connection)
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
--- End of inner exception stack trace ---
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
--- End of inner exception stack trace ---
at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection)
at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple`3 k)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at …Run Code Online (Sandbox Code Playgroud) 我正在使用带有EntityFramework 6 DataAnnotations的asp.net MVC 5。我想知道是否有一种方法来获取所有DisplayName对象并将它们保存在变量中到Controller类中。
例如,考虑以下类:
public class Class1
{
[DisplayName("The ID number")]
public int Id { get; set; }
[DisplayName("My Value")]
public int Value { get; set; }
[DisplayName("Label name to display")]
public string Label { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
如何获取DisplayName所有属性的值?例如,如何创建一个返回a的函数,该函数Dictionary< string,string >的键具有属性名称和值为的键DisplayName,如下所示:
{ "Id": "The ID name", "Value": "My Value", "Label": "Label name to display"}.
Run Code Online (Sandbox Code Playgroud)
我已经看到了这个主题stackoverflow-获取DisplayName属性的值,但是我不知道扩展此代码的方法。
预先感谢您的帮助.我对使用Entity Framework 6include()方法时遇到的情况感到有些困惑.据我所知,include方法的作用就像封闭的对象一样,当对象匹配时也是如此.LEFT JOINNULLOUTER JOIN
我将通过发生在我身上的例子,以便你帮我理解发生了什么.
我的表有以下模型:
public class Booking
{
[Key]
public int ID{ get; set; }
public string Description{ get; set; }
public decimal Amount{ get; set; }
public decimal AmoutPaid{ get; set; }
public DateTime? Checkin { get; set; }
public DateTime? Checkout { get; set; }
[ForeignKey("SourceBooking ")]
public int SourceBookingId { get; set; }
public SourceBooking SourceBooking { get; set; }
}
public class SourceBooking
{
[Key]
public int …Run Code Online (Sandbox Code Playgroud) 我有一个带有字段的实体:
public partial class Load
{
public DateTime CreatedOn { get; set; }
public DateTime? UpdatedOn { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我必须通过以下方式订购记录(DESC):如果UpdatedOn具有值,则"查看"此值,否则请查看CreatedOn值.怎么做?
我想使用Entity Framework(EF6)创建搜索查询。为了更好地理解,我将举例说明。
我有5个字(字符串)。
“我的名字是杰克”
我的年龄是35岁
“我的工作是开发人员”
“你叫什么名字”
“中国人口超过十亿”
现在,我想创建一个具有以下结果的EF查询搜索:
My name,结果是数字1和2以及3和4is结果为数字1和2以及3和4和5这是我的查询:
var query = (from q1 in _db.QuestionTbl where q1.questionTitle.Any(a => q1.questionTitle.Contains("search")) select q1).ToList();
Run Code Online (Sandbox Code Playgroud)
但是我找不到正确的答案。