Tag: entity framework

是否可以在EntityFramework中对SelectMany使用Select(l => new {})

我正在尝试一些我不太确定的事情,但我想在这里询问是否有可能。 能够做到吗? public IQueryable GetInfo(int count, byte languageId) { return db.Info.SelectMany(i => i.LanguageInfo) .Where(l => l.Language.id == languageId) .Select(l => new Info { AddDate = l.Info.AddDate, Description = l.Description, EntityKey = l.Info.EntityKey, id = l.Info.id, Title = l.Title, ViewCount = l.Info.ViewCount } ) .OrderByDescending(i => i.id) .Take(count); } 执行此方法时出现错误 无法在LINQ to Entities查询中构造实体或复杂类型“GuideModel.Info”。 这是否意味着“不可能”? 谢谢

如何在entity framework中查找具有指定日期范围列表的日期?

我有一个IEnumerable我创建的类包含日期范围。 那个class看起来像这样: public class Range where T: struct { public T Start { get; set; } public T End { get; set; } } 我想查找我的集合中的所有记录,其中日期列落在指定日期范围的任何一个范围内。 这是我的尝试: deals = deals.Where( deal => criteria.DateRanges.Any( dt => deal.CloseDate >= dt.Start && deal.CloseDate < dt.End.Value.AddDays(1))); 这会抛出我假设的错误,因为EF不知道如何将criteria.DateRanges.Any()转换为SQL。 那你怎么写这个来查找与任何日期范围相匹配的日期?

显示原始值entity framework7

我有一个审计表,跟踪添加,删除和修改。 我在entity framework内跟踪这个而不是使用数据库触发器有多种原因,但实际上是因为我们使用了一个进程账户,我想跟踪用户物理上对该记录进行了哪些更改。 我有这个与EF 5合作,我不记得我可能已经在EF6工作了。 无论哪种方式,我都在努力尝试捕捉原始值。 我注意到,当我在观察时 – 我可以在非公众成员中看到原始价值 – 所以在我的脑海里,我知道它必须存在于某个地方。 最终这适用于EF早期版本: EntityEntry dbEntry; //this is actually passed in a different area just showing as an example. foreach (string propertyName in dbEntry.OriginalValues.PropertyNames) { // For updates, we only want to capture the columns that actually changed if (!object.Equals(dbEntry.OriginalValues.GetValue(propertyName), dbEntry.CurrentValues.GetValue(propertyName))) { result.Add(new TableChange() { AuditLogID = […]

每个请求的上下文:如何更新实体

我有一个存储库类,如下所示。 有一种获取实体对象的方法 – GetPaymentByID。 我正在检索Payment对象并对其PaymentType属性进行更改。 但这并没有反映在数据库中。 我知道原因 – SaveContextChanges方法使用新的上下文。 我需要使用Context Per Request方法。 因此,我在每种方法中创建新的上下文。 在这种情况下,如何修改代码以成功更新数据库? 注意:客户端程序不应使用ObjectContext,因为可以使用不使用Entity Framework的另一个存储库更改存储库 。 注意 :“ DataContext很轻,创建起来并不昂贵 ” namespace MyRepository { public class MyPaymentRepository { private string connectionStringVal; public MyPaymentRepository() { SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); sqlBuilder.DataSource = “.”; sqlBuilder.InitialCatalog = “LibraryReservationSystem”; sqlBuilder.IntegratedSecurity = true; // Initialize the EntityConnectionStringBuilder. EntityConnectionStringBuilder entityBuilder = […]

entity framework代码中的许多关系首先使用“虚拟”关键字相互访问

此摘录代码与显式Junction表成功创建了许多关系,其中包含其他数据。 问题: 我希望能够从学生访问课程,反之亦然, (因此注释的虚拟属性。但如果我取消注释它,它会导致错误(见下文)) 如果我没有显式创建联结表(没有其他数据),则虚拟关键字可以正常工作,因为EF会按惯例创建联结表。 题: 如何在不经过注册的情况下让学生访问课程? 或者那是不可能的? 如果不可能,那么最好的方法是什么呢? ( EF和C#的初学者 ) public class Student { [Key] public int StudentId { get; set; } public string StudentName { get; set; } //public virtual Course Courses { get; set; } } public class Course { [Key] public int CourseId { get; set; } public string CourseName […]

将EF CodeFirst Base类转换为Inherited类(使用table-per-type)

我正在使用EF Code First,并且有两个类定义如下: public class User { public int Id { get; set; } public string Username { get; set; } public string Email { get; set; } } [Table(“Visitors”)] public class Visitor : User { public Visitor() { Favourites = new List(); } public virtual IList Favourites { get; set; } } 这使用Table-Per-Typeinheritance并定义DB模式,如下所示: Users […]

在扩展Roles表之后,实体类型IdentityRole不是当前上下文的模型的一部分

我扩展了由Entity Framework创建的AspNetRoles,如下所示: public class AspNetRoles:IdentityRole { public AspNetRoles() : base() { } public String Label { get; set; } public String ApplicationId { get; set; } public AspNetApplications Application { get; set; } public static readonly String SystemAdministrator = “SystemAdministrator”; } 我明白,因为我扩展了identityrole表,我不得不对我的用户管理员进行更改。 这就是我所做的: public class ApplicationUserManager : UserManager { public ApplicationUserManager(IUserStore store) : base(store) { […]

如何在一个MVC应用程序中完全结合EntityFramework,Repository,UnitOfWork和Automapper?

首先,我决定创建一个名为IDataAccessLayer接口,并开始将所有内容放入其中: GetUsers() , GetUser(int id) , GetOrderByNumber(int number) , DeleteOrder(int Id)等方法。 起初工作刚刚完美。 但后来我意识到DataLayer:IDataLayer的具体实现DataLayer:IDataLayer正在发展壮大。 我决定把它切成几个部分类文件。 我仍然觉得我做错了什么。 然后我决定为每个逻辑部分创建接口,如IUsers , IOrders , IItems等。没有用,因为我通过注入控制器构造函数的一个依赖属性访问存储库。 所以每次我需要在我的控制器中使用不同类型的dataContext时,我不能只添加另一个属性。 然后经过几个小时阅读有关entity framework的文章后,我终于意识到我必须使用Repository和Unit of work模式。 而且我仍然需要以某种方式将POCO与我的ViewModel对象分开,尽管他们几乎总是分享相似之处。 Automapper有很多帮助。 但是现在,我不确定如何一起使用所有东西。 entity framework,模式,自动映射和dependency injection框架,如Ninject。 我不清楚如何将所有内容混合到一个很棒的架构中。 你能告诉我一些很好的例子。

LINQ子查询“NOT IN”问题

我不明白为什么这个查询失败了。 var qTags = from tagsU in _context.ADN_ProductTagsView where !(from o in _context.ADN_ProductTagsView where o.ProductID == productId select o.ProductTagID).Contains(tagsU.ProductTagID) select tagsU; 或者这一个: var tagAux = from o in _context.ADN_ProductTagsView where o.ProductID == productId select o.ProductTagID; var qTags = from tagus in _context.ADN_ProductTagsView where !tagAux.Contains(tagus.ProductTagID) select tagus ; 两个都给我这个错误: LINQ to Entities does not recognize the […]

如何从表记录中选择除某些列以外的表记录

我有一个从sql数据库创建的实体数据库。 我需要在datagridview上显示记录,我正在使用此代码。 DBEntities db = new DBEntities(); dataGridView1.DataSource = db.Agent.Select(x => new { Name = x.Name, Second_Name = x.Second_Name}).ToList(); 例如,真正的代理表包含大约10列,我需要显示所有,exept’id’。 如果我为每8列做同样的事情,那么就变成一条长而无意义的行。 怎么做更多的遗忘和好。