Tag: entity framework 4

entity framework4.2“该类型不归因于EdmEntityTypeAttribute,但包含在使用EdmSchemaAttribute归属的程序集中

我收到以下错误: System.InvalidOperationException未处理Message =类型’Judge’未归因于EdmEntityTypeAttribute,但包含在使用EdmSchemaAttribute归属的程序集中。 不使用EdmEntityTypeAttribute的POCO实体不能与使用EdmEntityTypeAttribute的非POCO实体包含在同一程序集中。 Source = EntityFramework StackTrace:System.Data.Entity.InternalContext.UpdateEntitySetMappingsForType(Type entityType)at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)…. public class GenericRepository where TEntity : class { internal z context; internal DbSet dbSet; public GenericRepository(z context) { this.context = context; this.dbSet = context.Set(); } public GenericRepository() { this.context = new z(); this.dbSet = context.Set(); } public virtual IEnumerable Get( Expression<Func> filter = null, […]

有没有办法找到已删除关系的所有实体?

我试图让我的业务逻辑不知道我的数据层的内部工作方式,反之亦然。 但entity framework正在努力做到这一点。 我可以插入到一个集合(在我的业务层中)而不引用ObjectContext: order.Containers.Add(new Container { ContainerId = containerId, Order = order }); 当在数据层中执行SaveChanges()时,这可以节省很多时间。 但是要从集合中删除项目,我需要对ObjectContext的引用。 (我在本指南中删除了EF实体的情况#1。)如果我这样做: delContainers.ForEach(container => order.Containers.Remove(container)); 然后,当我调用SaveChanges()我得到一个exception,告诉我我需要删除对象以及引用。 所以,我认为它的选择是: 将委托传递给将调用Entity Framework ObjectContext Delete方法的业务逻辑。 或者(我希望)找到一种方法来获取已删除其引用并实际删除它们的所有实体。 (在我的数据层中调用SaveChanges()之前。) 有谁知道这样做的方法? 更新: 我试过这个: // Add an event when Save Changes is called this.ObjectContext.SavingChanges += OnSavingChanges; … void OnSavingChanges(object sender, EventArgs e) { var objectStateEntries = ObjectContext.ObjectStateManager .GetObjectStateEntries(EntityState.Deleted); […]

帮助我理解“LINQ to Entities仅支持转换实体数据模型基元类型”

我有一个工作单元和一个使用EF 4和POCO的存储库。 由于EF在Skip()和Take()之前需要一个有序集,我添加了以下unit testing(没有模拟)只是为了拉出一条记录来查看它是否有效。 var myList = UOW.EntityRepo.Get( orderbyLambda: p => p.ID, page: 1, pageSize: 1); 这导致orderbyLambda = {p => Convert(p.ID)}的表达式和枚举期间的错误。 ID是一个tinyint (Int16 /短) 那么为什么ID无法订购呢? 更多关于错误的信息 Unable to cast the type ‘System.Int16’ to type ‘System.Object’. 我将orderbyLambda定义为Expression<Func> orderbyLambda 编辑: 如果我这样做,真正的杀手是: orderbyLambda: p => new { p.ID } 它有效…为什么?

如何使用EF在SQLite DB中检索和设置user_version

我要做的是加载并从我的SQLite数据库中设置user_version(PRAGMA user_version)。 我正在使用entity framework,但如果我不能通过它实现它,那么我需要能够在C#4.0中以其他方式实现它。 我试过了: this.DatabaseConnection.Connection.Open(); System.Data.Common.DbCommand cmd = this.DatabaseConnection.Connection.CreateCommand(); cmd.CommandText = “PRAGMA user_version”; cmd.CommandType = System.Data.CommandType.Text; System.Data.Common.DbDataReader reader = cmd.ExecuteReader(); 其中DatabaseConnection是我的EF上下文,Connection是上下文中的DbConnection对象,但是我得到一个例外: 查询语法无效。 接近标识符’user_version’,第1行,第8列。

不允许entity framework新事务,因为在会话中运行其他线程,multithreading保存

我试着在DB上保存multithreadingprocesso的日志,但是我收到以下错误:不允许新事务,因为会话中还有其他线程在运行。 在每个胎面我都有这个function: internal bool WriteTrace(IResult result, string message, byte type) { SPC_SENDING_TRACE trace = new SPC_SENDING_TRACE( message, Parent.currentLine.CD_LINE, type, Parent.currentUser.FULLNAME, Parent.guid); Context.SPC_SENDING_TRACE.AddObject(trace); if (Context.SaveChanges(result) == false) return false; return true; } 每个线程的Context都不同,但与DB的连接始终是相同的。 有没有办法解决这个问题? 谢谢Andrea

是什么让Entity Framework / Upshot相信我的对象图“包含周期”?

我正在使用Entity Framework 4.3(代码优先)测试Knockout 2.1.0和Upshot 1.0.0.2并遇到以下错误: {“类型’System.Collections.Generic.HashSet`1 [[KnockoutTest.Models.Person,KnockoutTest,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null]]的对象图包含循环,如果引用则无法序列化跟踪已停用。“} 我使用一个相当典型的模型来测试一些基本的父子实体。 public class Client { public Client() { Projects = new HashSet(); Persons = new HashSet(); } [Key] public int ClientId { get; set; } [Required] [Display(Name = “Client Name”, Description = “Client’s name”)] [StringLength(30)] public string Name { get; set; } […]

entity framework中DBContext,DBSet 的引用

我正在尝试使用最新Entity Framework 4.0的ADO.Net Codefirstfunction。 作为其中的一部分,我安装了Microsft的Entity Framework CTP 4 ,并使用Scott的教程首先创建模型。 在教程DBContext和DBSet中指定。 有些人可以告诉我们为了访问这个类而使用的引用是什么。 我使用了以下引用,但DBContext和DBSet没有任何DBSet System.Data.Entity的 System.Data.Entity.Design

EF4 Code仅映射inheritance

我有以下模型,我希望ShiftRequest和MissionRequest在数据库中有一个表。 public class RequestBase { public int Id { get; set; } public DateTime? RequestDate { get; set; } public int UserId { get; set; } public virtual ICollection Notifications { get; set; } } public class ShiftRequest : RequestBase { public virtual Column Column { get; set; } } public class MissionRequest : RequestBase […]

Singleton中的entity framework上下文

我正在构建一个在单例模式中使用EF的上下文的应用程序,如NHibernate使用Session: public class DbContextFactory { private static volatile DbContextFactory _dbContextFactory; private static readonly object SyncRoot = new Object(); public DbContext Context; public static DbContextFactory Instance { get { if (_dbContextFactory == null) { lock (SyncRoot) { if (_dbContextFactory == null) _dbContextFactory = new DbContextFactory(); } } return _dbContextFactory; } } public DbContext GetOrCreateContext() { if […]

禁用entity framework的默认值生成(代码优先)

我在数据库中有一个不能为null的列,我想将其设置为在数据库中具有默认值。 问题是entity framework似乎自己创建了一个默认值(例如,int => 0),并完全忽略了数据库中的默认值约束。 有没有办法禁用这个entity framework的默认值?