Tag: entity framework

长时间运行的Entity Framework事务

当用户打开某个实体的编辑表单时,我想锁定该实体并让她进行任何更改。 在编辑过程中,她需要确保没有其他人对其进行任何编辑操作。 如何在entity framework(C#)4 +,数据库MS SQL Server 2008中锁定实体? 非常感谢你提前!

处理entity framework中的存储过程

如何处理在Entity Framwork中返回不同输出的存储过程。 例如,在sp上,有条件和条件。 if condition返回整数,否则condition返回datatable。 如何使用entity framework处理这个与请建议。

找不到Entity Framework 4注释

我正在尝试使用注释将POCO类映射到我的数据库表。 我需要使用Table注释来指定我的表的名称,但我无法解析Table注释。 注意:我导入了System.Data.Entity命名空间但它不起作用。 我必须导入哪个命名空间才能使用EF注释? 注1我的意思是: [Table(“my_table”)] public class MyClass { // … } 注2: My Entity Framework dll是v4.0.30319

entity framework映射奇数 – 成员名称不能与其封闭类型相同

我已经从DB创建了一个实体模型,并且收到错误“成员名称不能与其封闭类型相同”。 这意味着有一个Property与Class同名。 所以错误在这里 /// /// There are no comments for Employee in the schema. /// [global::System.ComponentModel.BrowsableAttribute(false)] [global::System.Runtime.Serialization.DataMemberAttribute()] public global::System.Data.Objects.DataClasses.EntityReference EmployeeReference { get { return ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.GetRelatedReference(“HumanResourceModel.FK_EmployeeReferenceMapping_Employee”, “Employee”); } set { if ((value != null)) { ((global::System.Data.Objects.DataClasses.IEntityWithRelationships)(this)).RelationshipManager.InitializeRelatedReference(“HumanResourceModel.FK_EmployeeReferenceMapping_Employee”, “Employee”, value); } } } 这是其中的一部分 [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName=”HumanResourceModel”, Name=”EmployeeReference”)] [global::System.Runtime.Serialization.DataContractAttribute(IsReference=true)] [global::System.Serializable()] public partial class EmployeeReference : global::System.Data.Objects.DataClasses.EntityObject { 我可以通过重命名属性的名称来“修复”此错误(但这将导致将来出现大量问题),但是如何从创建的代码中修复此问题? 我将外键重命名为映射,但这不起作用。 有任何想法吗? […]

要处理的linq函数:两个字段相等或同时为null或为空

嗨,我有一个linq查询,我将一个对象与我数据库中存储的所有其他实体进行比较。 名字和姓氏是必填字段,所以我不必检查是否为空。 但在街道的情况下,我必须做这些检查。 如果两个字段都为空或空字符串或者它们是相同的,我想匹配。 以下linq查询工作正常。 但我徘徊是没有办法让它更具可读性。 例如,使用像Bool FieldsAreEqualOrBothNullOrEmpty(r.street,request.street)这样的自定义函数 无法想象如何做到这一点,如果可能的话。 var same = from r in db.Requests where r.firstName.ToLower() == request.firstName.ToLower() && r.lastName.ToLower() == request.lastName.ToLower() //Seems long to just compare two fields && ( string.IsNullOrEmpty(r.street) && string.IsNullOrEmpty(request.street) ) || r.street.ToLower() == request.street.ToLower() select r; return same;

如何加载entity framework预编译的视图

我发现通过使用EdmGen.exe工具预编译我的视图,我可以提高应用程序的EdmGen.exe 。 这一切都很好,但是我或我的大学都无法确定项目实际使用生成的.cs文件的方式。 似乎没有任何关于在任何地方生成的类的引用,所以如何包含它? 任何人都可以对此有所了解,因为它真的很令人沮丧,不知道它是如何工作的! 编辑 我们已经确认EntityViewGenerationAttribute用于标记预编译视图的类类型,但这必须意味着它通过reflection加载。 如果是这样的话,有没有办法明确地将它放在代码中,预编译的视图类应该用于某个视图?

entity framework代码第一个多列外键

首先使用代码我设计了3个类: class User { public Int32 ID {get;set;} public virtual ICollection {get;set;} public Int32 MainCityID {get;set;} public UserCityDetail MainCityDetail {get;set;} } class City{ public Int32 ID {get;set;} … } class UserCityDetail{ [Key, Column(Order = 0)] public Int32 UserID {get;set;} [Key, Column(Order = 1)] public Int32 CityID{get;set;} … } 所以基本上我有一个用户在几个城市有不同的细节。 UserCityDetail的用户ID是PK和FK。 我也希望直接参考主要的城市细节,所以我在用户上放置了一个城市ID FK。 如何将用户ID和MainCityID配置为像MainCityDetail的FK一样?

MySql + entity framework =每个派生表都必须有自己的别名

我必须从我的C#程序访问另一个开发人员的一些MySql视图。 因此,经过一些搜索,我决定entity framework,并使用此问题中提到的驱动程序: 使用MySQL与entity framework ( MySQL .NET Connector )。 现在,我正在尝试获取一个视图的第一个元素: myEntities.events.First(); 在那里,我得到一个例外: System.Data.EntityCommandExecutionException was unhandled Message=An error occurred while executing the command definition. See the inner exception for details. Source=System.Data.Entity StackTrace: at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at System.Data.Objects.ELinq.ObjectQueryProvider.b__0[TResult](IEnumerable`1 sequence) at System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable`1 […]

dependency injection:ASP vNext。 这是怎么回事?

因此,在我基于CQRS的错误跟踪Web-API中,我在进行unit testing之前重构了我的代码(诚然,应该首先考虑); 我有这个类和构造函数: public class BugCommandHandler : IBugCommandHandler { private BugContext db; public BugCommandHandler(BugContext bugContext) { db = bugContext; } //Interface implementation } 在我的控制器中,我有这个: public class BugsController : Controller { private IBugCommandHandler commandHandler; private BugContext db; public BugsController(BugContext bugContext, IBugCommandHandler bugCommandHandler) { db = bugContext; commandHandler = bugCommandHandler; } } 最后,在我的Startup类中,我已经注入了依赖项 services.AddSingleton(); 我的unit testing和手动集成测试都工作正常,因为我在没有DI的情况下手动调用它。 BugCommandHandler实现现在如何工作,就像它在构造函数中使用数据库上下文一样被调用( […]

在EF上使用默认filter的实体

我的.edmx文件上有一个名为Client的实体。 我必须再次运行几个linq querys但是在所有这些问题上,我需要一个filter(比如说active = 1)。 我不希望在我的所有查询中都有一个where c.active == 1 ,更有意义的是我的实体应用了默认filter。 这是可以做到的吗? 或者也许我可以将查询作为实体的基础而不是表格? (我是数据库第一种方法) 我知道另一个解决方案是在数据库上创建一个视图并将实体链接到视图,但我也不希望这样。