Tag: entity framework 4

EF Generic Repository从新插入的通用实体获取Id

我的所有实体都有财产ID。 数据库中的所有表都将自动生成的整数标识Id作为主键。 我有创建实体的通用方法。 有没有办法在实体插入数据库后获取实体ID? public override int Create(T entity) { string entitySet = GetEntitySetName(); _context.AddObject(entitySet, entity); _context.SaveChanges(); return ; //TODO Return Id here } 在简单(非通用)存储库中,我可能只返回Entity.Id,但如何在通用存储库中获得相同的行为? 我可能有包含int属性Id的所有实体的基本实体类,但有没有办法让它工作而不实现这种inheritance?

EntityFramework中的公用表表达式

我在Sql Server中有这个查询,我需要在EntityFramework中使用,那么如何编写一个与此结果相同的EntityFramwork代码 WITH cte AS ( SELECT * FROM StockGroups WHERE GroupParent =’Stationery’ UNION ALL SELECT g.* FROM StockGroups g JOIN cte ON g.GroupParent = cte.GroupName ) SELECT * FROM cte 我不知道如何在EF中转换它,所以我试着加入。 from a in db.StockGroups join b in db.StockGroups on new { GroupParent = a.GroupParent } equals new { GroupParent = b.GroupName } where […]

数据读取器与指定的entity framework不兼容

我有一个方法,将从sproc中返回裸min结果以填充选择菜单。 当我想要裸min结果时,我将bool getMin = true传递给sproc,当我想要完整记录时,我传递bool getMin = false。 这导致Entity FrameWork错误“ 数据读取器与指定的不兼容 ” 错误最相关的部分 {“Message”:“发生错误。”,“ExceptionMessage”:“数据读取器与指定的’CatalogModel.proc_GetFramingSystems_Result’不兼容。类型的成员’FrameType’在其中没有相应的列具有相同名称的数据读取器。“,”ExceptionType“:”System.Data.EntityCommandExecutionException“, 显然错误告诉我,当数据读取器试图设置属性’FrameType’时,查询结果中没有。 现在我理解了这个错误,我想知道的是,我正在为了将这个sql sproc分成两个sprocs而进行goning或者是否有解决方法? 我的function如下 public static IEnumerable GetFramingSystems(int brandID, string frameType, string glazeMethod, bool getMin) { using (CatalogEntities db = new CatalogEntities()) { return db.proc_GetFramingSystems(brandID, frameType, glazeMethod, getMin).ToList(); }; } 我的TSQL如下 ALTER proc [Catelog].[proc_GetFramingSystems] @BrandID INT, @FrameType VARCHAR(26), @GlazeMethod VARCHAR(7) =’Inside’, […]

如何对绑定到EF EntityCollection 的WinForms DataGridView进行排序

我正在尝试将WinForms DataGridView绑定到EntityFramework4对象的EntityCollection 。 麻烦的是,我无法弄清楚如何让它(自动)排序。 我所做的就是将BindingSource的DataSource属性设置为实体的集合。 MyBindingSource.DataSource = CurrentItem.InvoiceNotes; 我真的希望我可以添加一个简单的配置来使它工作; 我真的不想将我的EF Collection包装在一个新的BindingList容器中。

必须使用关系流畅API或数据注释显式配置此关联的主要结尾

“必须使用关系流畅的API或数据注释显式配置此关联的主要结尾。” 我在更新/迁移数据库时在entity framework4.4中收到此错误,但我不是要尝试指定1:1关系。 我想要这样的东西: public class EntityA { public int ID { get; set; } public int EntityBID { get; set; } [ForeignKey(“EntityBID”)] public virtual EntityB EntityB { get; set; } } public class EntityB { public int ID { get; set; } public Nullable PreferredEntityAID { get; set; } [ForeignKey(“PreferredEntityAID”)] public virtual EntityA PreferredEntityA […]

在C#中模拟CTE递归

假设有以下CTE返回我所拥有的某些树数据(邻接模型)的级别(取自Linq中的分层数据 – 选项和性能 ): WITH hierarchy_cte(id, parent_id, data, lvl) AS ( SELECT id, parent_id, data, 0 AS lvl FROM dbo.hierarchical_table WHERE (parent_id IS NULL) UNION ALL SELECT t1.id, t1.parent_id, t1.data, h.lvl + 1 AS lvl FROM dbo.hierarchical_table AS t1 INNER JOIN hierarchy_cte AS h ON t1.parent_id = h.id ) SELECT id, parent_id, data, lvl FROM […]

ASP.NET MVC /entity framework错误 – 无效的列名称’Environment_Id’

我是ASP.NET MVC和EF的新手,希望这不是一个愚蠢的问题 当我传递模型来查看我收到此错误 – exception详细信息:System.Data.SqlClient.SqlException:无效的列名称’Environment_Id’。 模型或数据库表具有该名称的属性。 可以指导我吗? **Here is the Version Model Class** public partial class Version { public Version() { this.ProfileVersions = new List(); this.ServerInfoes = new List(); } public int Id { get; set; } public string Number { get; set; } public string Tag { get; set; } public string Owner { […]

清洁处理EF中的循环引用的方法?

说我有这个表结构: Client ———– ClientId int not null (identity) CurrentDemographicId int null (FK to ClientDemographic) OtherClientFields varchar(100) null ClientDemographic —————— ClientDemographicId int not null (identity) ClientId int not null (FK to Client) OtherClientDemographicFields varchar(100) null 我们的想法是Client(在EF中)将具有ClientDemographics列表和CurrentDemographic属性。 问题是当我设置对象结构并尝试保存它时,我收到此错误: 无法确定相关操作的有效排序。 由于外键约束,模型要求或存储生成的值,可能存在依赖关系 这个错误是有道理的。 我在表格设置中有一个循环引用。 它不知道首先插入哪个实体(因为它同时需要来自两个表的Id)。 所以,我一起攻击了一个看起来像这样的解决方案: // Save off the unchanged ClientDemograpic ClientDemographic originalClientDemographic = client.CurrentClientDemographic; // Merge the […]

将数据从2个表映射到1个实体 – entity framework4

我被困在这里。 是否可以将entity framework4中的2个不同表格中的数据映射到1个实体。 我在一张桌子上有一堆员工,而在另一张桌子里我有一些som项目信息。 我想在一个实体中组合这两个表,并保持跟踪function等,这可能吗? 我不想使用函数导入,而只是通过实体模型来完成。 任何人都可以帮助 – 当我尝试这样做时,我总是得到以下错误: 错误3024:从第2354行开始映射片段时出现问题:必须指定EntitySet MyProjectTable的所有关键属性(MyProjectTable.PSInitials,MyProjectTable.ProjectID)的映射。 两个键都映射到它们各自的表。 新实体是使用MyProjectTable作为基础。 2个表之间的关系是1- * 希望你能帮忙。 /基督教

entity framework不会检测导航属性的更改

我在检测导航属性的更改时遇到问题: 我的测试模型如下所示: public class Person { public int Id { get; set; } public string Name { get; set; } public virtual Address Address { get; set; } } public class Address { public int Id { get; set; } public string Name { get; set; } } 我创建并保存了一个Person类型的对象,并指定了Name和Address属性。 我的问题是,如果我从数据库中取回Person对象并更改Address属性(例如Null),那么ef不会检测到更改! 我的代码是这样的: using (var ctx = […]