Tag: entity framework 6

指定的架构无效。 错误:具有名称的多个类型

我在EF 6工作,当我尝试运行我的项目时面临以下问题。 指定的架构无效。 错误: 名称为“TableName”的多个类型存在于不同名称空间中的EdmItemCollection中。 基于约定的映射需要唯一的名称,而不考虑EdmItemCollection中的命名空间。 我在StackOverFlow和google上尝试了太多,但没有找到解决方案。 我正在使用visual studio 2012.我为整个数据库创建了一个.edmx文件。 我在整个项目中搜索了tableName,并且只出现了一次表。 请帮助我这方面

如何在从上下文中分离对象时删除EF6中的对象列表

好的我可以删除EF6中的单个项目,如下所示: public void DeleteUserGroup(MY_GROUPS ug) { using (var context = new MYConn()) { var entry = context.Entry(ug); if (entry.State == EntityState.Detached) { context.MY_GROUPS.Attach(ug); } context.MY_GROUPS.Remove(ug); context.SaveChanges(); } } 如果此方法从单个MY_GROUPS实例MY_GROUPS为List我将如何处理删除? 是否有一种更有效的方法,然后只做一个foreach并一次设置一个状态? 更新 :我已经使用了类似的方法,使用RemoveRange方法。 但是我收到一个错误: 无法删除该对象,因为在ObjectStateManager中找不到该对象。 我正在寻找将对象列表附加到上下文的最佳方法,以便我可以删除它们。

entity framework多对多关系表创建“向后”

我遇到了EF6和多对多关系的问题。 我有以下设置: public class Foo { public int Id { get; set; } public virtual ICollection Bars { get; set; } public virtual ICollection SubBars { get; set; } public Foo() { Bars = new HashSet(); SubBars = new HashSet(); } } public class Bar { public int Id { get; set; } public virtual […]

entity framework6运行时错误:“字典中没有给定键”

我正在使用EF6代码优先和每个层次结构的表编写一个ASP.Net应用程序 每当我尝试使用dbcontext执行任何有意义的操作(例如查询,更新等)时,我都会收到此错误: { “Message”: “An error has occurred.”, “ExceptionMessage”: “An error occurred while preparing the command definition. See the inner exception for details.”, “ExceptionType”: “System.Data.Entity.Core.EntityCommandCompilationException”, “StackTrace”: ” at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree, DbInterceptionContext interceptionContext, IDbDependencyResolver resolver, BridgeDataReaderFactory bridgeDataReaderFactory, ColumnMapFactory columnMapFactory)\r\n at System.Data.Entity.Core.EntityClient.Internal.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree, DbInterceptionContext interceptionContext)\r\n at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)\r\n at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, […]

如何仅在Entity Framework 6.1中加载子对象的某些字段?

我正在研究一个有两个类的模型, Product和Transaction 。 public class Product { [DataMember] public Guid ProductId {get; set;} [DataMember] public virtual ICollection Transactions { get; set; } } public class Transaction { [DataMember] public Guid TransactionId {get; set;} [DataMember] public DateTimeOffset Date { get; set; } [DataMember] public String Customer { get; set; } } 如何进行检索产品及其交易日期的查询? 我尝试过类似的东西 var product = […]

entity framework6从同一实体对象创建两个表

我想知道是否可以从一个已定义的实体对象类创建两个表实例。 例: public class EntityA() { public String name {get; set;} public String value {get; set;} } public class MyDbConext : DbContext { public DbSet instance1{ get; set; } public DbSet instance2{ get; set; } } 我要做的是创建两个具有不同表名的实体A实例。 这可能与代码第一entity framework? 我觉得只需要创建另一个扩展实体EntityA的类来创建同一个实体的另一个实例似乎很乏味。 期望的输出: 从EntityA类创建“Instance1_Table” 从EntityA类创建“Instance2_Table” DBConext中的代码将引发exception。 任何建议表示赞赏,谢谢,D

Linq到实体扩展方法内部查询(EF6)

有人可以向我解释为什么EF引擎在以下情况下失败了吗? 它可以使用以下表达式正常工作: var data = context.Programs .Select(d => new MyDataDto { ProgramId = d.ProgramId, ProgramName = d.ProgramName, ClientId = d.ClientId, Protocols = d.Protocols.Where(p => p.UserProtocols.Any(u => u.UserId == userId)) .Count(pr => pr.Programs.Any(pg => pg.ProgramId == d.ProgramId)) }) .ToList(); 但是,如果我将一些封装到扩展方法中: public static IQueryable ForUser(this IQueryable protocols, int userId) { return protocols.Where(p => p.UserProtocols.Any(u => u.UserId == […]

如何将DbSet 转换为List

鉴于以下简化的Entity Framework 6上下文,我尝试使用实体填充List,但是如何通过reflection进行转换(我相信)有问题。 public class FooContext : DbContext { public virtual IDbSet Foo { get; set; } //… } public class FooClass { public int Id{ get; set; } public string Name {get; set; } //… } public main() { using (var context = new FooContext()) { var sets = typeof(FooContext).GetProperties().Where(pi => pi.PropertyType.IsInterface && pi.PropertyType.GetGenericTypeDefinition().ToString().ToLower().Contains(“idbset”)); […]

entity framework6插入重复值

我有以下两个实体: public class Artist { [Key] public string ArtistId { get; set; } public string Name { get; set; } public virtual ICollection Genres { get; set; } } public class Genre { [Key] public int GenreId { get; set; } public string Name { get; set; } public virtual ICollection Artist { get; set; […]

entity framework6更新图表

保存不知道状态的对象图的正确方法是什么? 按状态我的意思是它们是新的还是现有的数据库条目正在更新。 例如,如果我有: public class Person { public int Id { get; set; } public int Name { get; set; } public virtual ICollection Automobiles { get; set; } } public class Automobile { public int Id { get; set; } public int Name { get; set; } public short Seats { get; set; } […]