Tag: entity framework 6

如何在Entity Framework 6中禁用模型缓存(Code First方法)

在MSDN文档之后我们可以阅读: 然后缓存该上下文的模型,该模型适用于app域中上下文的所有其他实例。 可以通过在给定的ModelBuidler上设置ModelCaching属性来禁用此缓存 ,但请注意,这会严重降低性能。 问题是模型构建器不包含任何名为ModelCaching的属性。 如何禁用模型缓存(例如,在运行时更改模型配置)?

LINQ to Entities无法识别方法’System.Object GetValue(…)’

我的问题是我需要查询generics类中属性的值。 该属性标有属性。 请参阅以下代码: var rowKeyProperty = EFUtil.GetClassPropertyForRowKey(); var tenantKeyProperty = EFUtil.GetClassPropertyForTenantKey(); var queryResult = objContext.CreateObjectSet().Single(l => (((int) tenantKeyProperty.GetValue(l, null)) == tenantKey) && (((int)rowKeyProperty.GetValue(l, null)) == KeyValue)); rowKeyProperty和tenantKeyProperty的类型为System.Reflection.PropertyInfo。 我理解为什么我会收到错误。 当linq查询转换为SQL时,它无法理解property.GetValue。 但是,我对这里的工作感到非常难过。 有没有人有任何想法如何实现这一目标? 谢谢。

使用EF6加载数据库初始化程序时出错

我一直在努力学习这个教程… http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for -an-asp-net-mvc-application但我一直收到以下错误…… system.invalidoperationexception = {“无法为应用程序配置中指定的DbContext类型’WeddingPreparations.Dal.WeddingContext’设置’WeddingPreparations.Dal.WeddingInitializer,KevinLisaWedding’类型的数据库初始化程序。有关详细信息,请参阅内部exception。”} 内在的例外是…… {“无法从程序集’EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089’加载类型’WeddingPreparations.Dal.WeddingContext’。”:“WeddingPreparations.Dal.WeddingContext”} 堆栈跟踪是…… at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) at System.Data.Entity.Internal.InitializerConfig.c__DisplayClass6.b__1(ContextElement e) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Internal.InitializerConfig.TryGetInitializerFromEntityFrameworkSection(Type contextType) at System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type contextType) at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.GetServiceFactory(Type type, String name) at System.Data.Entity.Infrastructure.DependencyResolution.AppConfigDependencyResolver.c__DisplayClass1.b__0(Tuple`2 t) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, […]

在Entity Framework 6中更新子对象

(使用Entity Framework 6.2) 我有以下两个模型/实体: public class City { public int CityId { get; set; } public string Name { get; set; } } public class Country { public Country() { Cities new HashSet(); } public int CountryId { get; set; } public string Name { get; set; } public virtual ICollection Cities { get; set; […]

如何指定从EntityFramework检索的DateTime对象应该是DateTimeKind.UTC

我有C#程序,其中所有DateTime对象都是DateTimeKind.UTC 。 将对象保存到数据库时,它按预期存储UTC。 但是,在检索它们时,它们是DateTimeKind.Unspecified 。 有没有办法告诉entity framework(代码优先)在C#中创建DateTime对象时总是使用DateTimeKind.UTC ?

使用具有entity framework6的存储库模式更新记录

我正在编写一个简单的博客应用程序,并尝试在我的通用存储库模式中建立CRUD操作,但我的更新方法出现错误,说: ‘System.Data.Entity.DbSet’不包含’Entry’的定义,并且没有扩展方法’Entry’可以找到接受类型’System.Data.Entity.DbSet’的第一个参数(你是否缺少using指令)或汇编参考?) 我跟着一篇post解释了如何通过在DbContext上添加额外的间接级别来“伪造”Entry()。 但是在MVC 5中我们inheritance自: IdentityDbContext而不是DbContext。 我确实尝试实现作者修复,但错误仍然存​​在。 我的问题 如何使用IdentityDbContext将更新方法添加到Entity Framework 6中的存储库? 如果我们不应该这样做,那么如何使用这种模式更新记录? 我应该注意到其他所有方法都按预期工作。 我的通用存储库: public class BlogEngineRepository : IRepository where T : class { protected DbSet DbSet; public BlogEngineRepository(DbContext dataContext) { DbSet = dataContext.Set(); } #region IRepository Members public void Insert(T entity) { DbSet.Add(entity); } public void Delete(T entity) { DbSet.Remove(entity); } public void Update(T […]

Visual Studio 2015 RCentity framework6.1.3迁移错误

仅当解决方案具有多个引用EntityFramework的项目时,才会发生粘贴的以下错误。 运行“启用 – 迁移”或“添加迁移”时,我收到以下错误: New-Object:提供的值无效,或者属性是只读的。 更改值,然后重试。 在D:\ Projetos \ Monitoramento \ packages \ EntityFramework.6.1.3 \ tools \ EntityFramework.psm1:757 char:13 + $ info = New-Object System.AppDomainSetup -Property @ {+ ~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidData 🙁 🙂 [New-Object],exception+ FullyQualifiedErrorId:SetValueException,Microsoft.PowerShell.Commands.NewObjectCommand在此对象上找不到属性“PrivateBinPath”。 validation该属性是否存在且可以设置。 在D:\ Projetos \ Monitoramento \ packages \ EntityFramework.6.1.3 \ tools \ EntityFramework.psm1:772 char:9 + $ info.PrivateBinPath + =’; […]

使用RemoveRange()批量删除行

我试图从表中删除多行。 在常规SQL Server中,这很简单,如下所示: DELETE FROM Table WHERE Table.Column = ‘SomeRandomValue’ AND Table.Column2 = ‘AnotherRandomValue’ 在Entity Framework 6中,他们引入了RemoveRange()方法。 但是,当我使用它时,entity framework不是使用我提供的where子句删除行,而是查询数据库以获取与where子句匹配的所有行,并使用其主键逐个删除它们。 这是EntityFramework的当前限制吗? 或者我使用RemoveRange()错误? 以下是我如何使用RemoveRange() : db.Tables.RemoveRange( db.Tables .Where(_ => _.Column == ‘SomeRandomValue’ && _.Column2 == ‘AnotherRandomValue’) );

在Entity Framework 6.1(非Core)中,如何使用IndexAttribute定义聚簇索引?

entity framework6.1(代码优先)增加了通过IndexAttribute添加索引的可能性。 该属性接受一个参数,用于指定索引应该是群集还是非群集。 同时,AFAIK,Entity Framework要求每个实体都有一个主键(使用KeyAttribute注释),并且该主键始终作为聚簇键创建。 因此,只要我使用IsClustered = true应用IndexAttribute ,就会出现错误,因为由于密钥,已经存在聚簇索引。 那么,如何使用IndexAttribute创建不是主键的聚簇索引? IsClustered属性IndexAttribute可用? (有关更多上下文:我正在映射一个仅用于通过LINQ查询进行读取的表。我不需要实际插入,更新或删除该表中的实体。因此,我不需要主键理想情况下,我想要一个没有主键的表,但是有一个非唯一的聚簇索引,专为阅读而优化。) 编辑(2014-04-11):另请参阅https://entityframework.codeplex.com/workitem/2212 。

更新EF 6中的现有数据会引发exception – “…相同类型的实体已具有相同的主键值”。

我正在尝试使用Entity Framework 6更新记录,代码优先,没有流畅的映射或像Automapper这样的工具。 实体( Employee )具有与其相关联的其他复合属性,如Addreess (集合), Department 它也从一个名为User的基础inheritance而来 save方法如下, _dbContext是DbConext实现 public bool UpdateEmployee(Employee employee) { var entity = _dbContext.Employees.Where(c => c.Id == employee.Id).AsQueryable().FirstOrDefault(); if (entity == null) { _dbContext.Employees.Add(employee); } else { _dbContext.Entry(employee).State = EntityState.Modified; // 0; } 我一直收到错误: 附加类型的实体失败,因为相同类型的另一个实体已具有相同的主键值。 如果图中的任何实体具有冲突的键值,则在使用“附加”方法或将实体的状态设置为“未更改”或“已修改”时,可能会发生这种情况。 这可能是因为某些实体是新的并且尚未收到数据库生成的键值。 在这种情况下,使用“添加”方法或“已添加”实体状态来跟踪图形,然后根据需要将非新实体的状态设置为“未更改”或“已修改”。 我尝试过以下方法: 在设置为EntityState.Modified之前附加 在查询对象是否存在时添加AsNoTracking() (没有例外,但DB没有更新) – https://stackoverflow.com/a/23228001/919426 使用基本实体_dbContext.Users而不是Employee实体进行保存 – https://stackoverflow.com/a/25575634/919426 现在这些都不适合我。 对于那些不能在我的情况下工作的解决方案,我有什么问题?