Tag: entity framework

默认entity framework超时

EF查询的默认超时是多少? 我试图通过检查context.CommandTimeout找出它,但它返回null 。 我还查看了连接字符串中的web配置,例如Connect Timeout= ,但默认情况下似乎没有。

entity framework中的表值函数?

是否可以使用entity framework调用表值函数(TVF)? 我在我的数据库中定义了三个TVF,它们没有显示在Entity Framework的模型中,也没有显示在“从数据库更新模型”向导中。 在Linq-to-SQL中很容易做到这一点,你只需将TVF拖到设计图面上,但在L2E中看起来似乎不太可能。 到目前为止,我还没有找到任何甚至一起提到TVF和entity framework的内容。

DropCreateDatabaseAlways种子未调用

我在自定义数据库初始化程序中调用Seed方法时遇到问题。 我正在使用EF 5.0并具有以下代码: public static class MyDatabase { public static void Initialize() { Database.SetInitializer(new MyInitializer()); } } public class MyInitializer : DropCreateDatabaseAlways { protected override void Seed(MyContext context) { base.Seed(context); context.Roles.Add(new Role { ID = 1, Name = “User”, }); context.Roles.Add(new Role { ID = 2, Name = “Admin”, }); context.SaveChanges(); } } 这两个类存在于MVC应用程序的单独类库中。 在Global.asax […]

找不到源类型’System.Data.Entity.DbSet’的查询模式的实现

我第一次使用entity framework,但似乎没有按预期工作。 我有这个代码: using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; public static class QueryClass { public static void Query() { using (var context = new MyDbEntities()) { DbSet set = context.Tables; var query = from val in set select value; } } } 在查询行(确切地说“set”变量用红色加下划线)我得到错误: 无法找到源类型’System.Data.Entity.DbSet’的查询模式的实现。’选择’未找到。 缺少’System.Linq’的引用或using指令 MyDbEntities由Entity Framework在Database-First方法中自动生成, context.Tables是一个DbSet ,因此它应该能够使用Linq,它是通过using指令添加的。 为了避免误解,我在本课程中找到以下内容: public virtual DbSet […]

使用visual studio安装程序在安装过程中更改app.configuration文件失败:无法加载文件或程序集’EntityFramework’

项目信息:我们正在创建一个安装程序项目,允许用户选择服务器名称和数据库名称。 安装主程序后,将创建相应的数据库。 安装程序项目: 有一个带有两个文本框的附加UI屏幕,包含servername和databasename。 将在安装时执行自定义操作,并将servername和databasename定义为自定义操作数据。 主要应用: 安装程序类连接安装后事件。 该类将在安装期间自动执行。 自定义操作数据将作为参数传递给此类。 安装后事件将触发一个方法: 创建数据库 获取app.config文件并调整connectionstring。 一切正常, 期望保存配置文件:我们收到此错误: System.Configuration.ConfigurationErrorsException:创建> entityFramework的配置节处理程序时出错:无法加载文件或>程序集’EntityFramework,Version = 6.0.0.0,Culture = neutral,> PublicKeyToken = b77a5c561934e089′ 很奇怪,在运行和调试主应用程序时,我们不会遇到entityframwork的任何错误。 此外,我们能够将代码作为unittest执行,因此只有在使用visual studio安装程序进行安装期间更改配置文件时才会出现此assembly问题 您可以在下面找到更改配置文件的代码。 void DeployInstaller_AfterInstall(object sender, InstallEventArgs e) { try { Configuration config = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location); string connectionsection = config.ConnectionStrings.ConnectionStrings [“XBSDbDataContext”].ConnectionString; ConnectionStringSettings connectionstring = null; if (connectionsection != null) { config.ConnectionStrings.ConnectionStrings.Remove(“XBSDbDataContext”); } […]

Web API 2 + MVC 5共享标识表

我有一个Web API和MVC5共享相同的身份表。 属于Web API的Context: public partial class CrestfineContext: IdentityDbContext { public CrestfineContext(): base(“WelbeckDatabase”, throwIfV1Schema: false) { } protected override void OnModelCreating(DbModelBuilder builder) { builder.Conventions.Remove(); builder.Entity().ToTable(“UserClaim”).HasKey(r => r.Id); builder.Entity().ToTable(“UserLogin”).HasKey(l => l.UserId); builder.Entity().ToTable(“Role”).HasKey(r => r.Id); builder.Entity().ToTable(“User”).HasKey(r => new{ r.IDNumber, r.UserName}); builder.Entity().ToTable(“User”).HasKey(r => r.UserName); builder.Entity().ToTable(“UserRole”).HasKey(r => new { r.RoleId, r.UserId });/**/ } 属于Web应用程序(MVC 5)的Context是: public partial class CrestfineContext […]

添加,删除和更新相关实体

简化,我有一个entity framework将两个表映射到对象: Items和Properties 。 每个项目都有一定的属性(一对多)。 从我的程序外部,我收到带有属性的“死”项,这些属性是新项或现有项的更新及其属性。 这个数据可能来自WCF调用,Web表单POST,反序列化:我要点插入并更新数据库中的项目和属性,以及我收到的未链接数据。 我发现了各种相关的问题和答案 (其中甚至都没有编译 )。 问题是我必须编写大量代码来同步现有项的属性和传入的更新项: private static void UpdateProperties(Item existingItem, Item updatedItem, TestdatabaseEntities context) { // Find deleted properties foreach (var existingProp in existingItem.Properties.ToList()) // ToList() to work on a local copy, otherwise you’ll be removing items from an enumeration { var inUpdate = updatedItem.Properties.Where(p => p.Name == existingProp.Name).FirstOrDefault(); […]

GroupBy with Count

我有列表testList返回,它有5个项目与适当的值。 var testList = _context.LectureReportStudent .Include(x => x.LectureReport) .Include(x => x.LectureReport.Lecture) .Include(x => x.Student) .Where(x => stuIds.Contains(x.LectureReport.LectureId)) .Select(x => new StudentModel { Name = x.Student.FirstName + “” + x.Student.LastName, Position = x.Student.JobTitle, JobId = x.LectureReport.LectureId, StudentId = x.StudentlId, LectureId = x.LectureReportId, Date = x.Date }; 我在StudentId上进一步GroupBy这个列表。 var result = testList.GroupBy(x => x.StudentId) 每个项目都有以下型号 Date, DaysOnLecture, […]

在LINQ to Entities查询中不能使用ToString()

所以我有以下代码: string searchQuery = collection[“query”]; var srmas = ( from SRMAs in db.SRMAs join SRMAStatus in db.SRMAStatus on SRMAs.Id equals SRMAStatus.Id join PurchaseOrders in db.PurchaseOrders on SRMAs.PONumber equals PurchaseOrders.PONumber join Suppliers in db.Suppliers on PurchaseOrders.SupplierID equals Suppliers.SupplierID join SRMADetails in db.SRMADetails on SRMAs.Id equals SRMADetails.SRMAId where ids.Contains(SRMAs.Status) && ( searchQuery.Contains(PurchaseOrders.suppliersOrderNumber) || searchQuery.Contains(SRMAs.PONumber.ToString()) ) select new […]

来自LINQ的结果,包括用于TimeSpan,GROUP和JOIN的SUM

我遵循EF模型: 我想在以下视图中获取数据: 在数据实体中,Time是Timespan,以毫秒为单位。 结果, PlaceInStep由StepId放置在成员之间。 如何进行这个难题? 编辑1 : 我的一些代码: var query = from data in ctx.Data join member in ctx.Members on data.MemberId equals member.MemberId join team in ctx.Teams on member.TeamId equals team.TeamId group member by member.TeamId into g select new {TeamId = g.Key, TeamName = g.Select(t=>t.Teams.TeamName), TotalTime = ???};