Tag: entity framework 4

entity framework:无法创建类型为’System.Collections.Generic.IList`1’的常量值

这使我今天无法解决问题。 我有这个简单的查询 var result = DataContext.Accommodations.Where(a => (criteria.MinPrice == null || a.AccommodationRates.Any(r => r.From >= criteria.MinPrice)) && (criteria.MaxPrice == null || a.AccommodationRates.Any(r => r.To criteria.Locations.Contains(j.Place.PlaceName))) ); 此查询的最后一行导致我出现问题 (criteria.Locations == null || criteria.Locations.Count == 0 || a.AccommodationPlaceJoins.Any(j => criteria.Locations.Contains(j.Place.PlaceName))) 它给出的错误是 无法创建类型为’System.Collections.Generic.IList`1’的常量值。 在此上下文中仅支持基本类型(例如Int32,String和Guid’)。 我甚至没有尝试创建列表。 我在这里尝试做的就是带回与一个地方相关的住宿(Place表中通过AccommodationPlaceJoin表链接到住宿表的地名)等于标准中的任何一个地名.Locations(属于IList类型)。 我已经尝试将此行更改为此,但它不起作用。 (criteria.Locations == null || criteria.Locations.Count == 0 || a.AccommodationPlaceJoins.Any(j => criteria.Locations.Any(l […]

从Entity Framework中的导航属性中删除所有记录

我在Program和Student表之间有1:N关系,EF转换为导航属性。 现在我要删除此导航学生中的所有记录。 我开始是这样的: foreach(Student student in program.Students) program.Students.Remove(student); 但我对此有点怀疑。 比我尝试过这样: while (program.Students.Count > 0) program.Students.Remove(program.Students.ToList()[0]); 但这似乎也很奇怪。 是否有一些更简单的方法来做到这一点,或者不是哪种方式最好?

如何在Entity Framework中设置两个相同类型的导航属性

使用代码第一个EF4(使用CTP5),我可以添加单个导航属性以及外键,它将遵循命名并且只将一次外键添加到表中。 如果我然后再添加相同类型的第二个属性,它会将其分解为表格中的4列而不是两列。 示例代码: 使用此模型,我获得了一个名为PressTypeID的PressType的AdapterFrameCapability表中的单个属性。 public class AdapterFrameCapability { [Key] public int AdapterFrameCapabilityID { get; set; } [Required] public int PressTypeID { get; set; } public virtual PressType PressType { get; set; } } 这是我想要建模的设置,但它会导致在表中创建4列,分别用于FromPressTypeID,FromPressTypeFromPressTypeID,ToPressTypeID和ToPressTypePressTypeID。 理想情况下,我只是喜欢FromPressTypeID和ToPressTypeID的列。 我在这做错了什么? public class AdapterFrameCapability { [Key] public int AdapterFrameCapabilityID { get; set; } [Required] public int FromPressTypeID { get; set; […]

如何将多个表达式传递给OrderBy for EF?

我使用的是EF 4.2,但我希望这也适用于EF 4和4.1。 我想将IQueryable和多个Expression<Func>传递给一个方法,并让该方法适当地将OrderBy和ThenBy应用于IQueryable 。 我找到了这个答案 ,并根据以下内容编写了以下方法: public IQueryable ApplyOrderBy(IQueryable query, IEnumerable<Expression<Func>> orderBy) { if (orderBy == null) { return query; } IOrderedQueryable output = null; foreach(var expression in orderBy) { if (output == null) { output = query.OrderBy(expression); } else { output = output.ThenBy(expression); } } return output ?? query; } 只要我订购的属性是string s,这样就可以正常工作,但是当我尝试按int属性排序时,我得到一个exception: 无法将类型“System.Int32”强制转换为“System.IComparable”类型。 […]

没有首先获得实体的EF4更新实体

如何更新实体而无需拨打电话进行选择。 如果我为实体提供密钥,那么在ObjectContext上调用SaveChanges()之后,它是否应该知道更新。 我目前这样做: var user = context.Users.Single(u => u.Id == 2); user.Username = “user.name”; user.Name = “ABC 123”; context.SaveChanges(); 这有效,但强制选择。 因为我知道Id ,为什么我不能这样做: var user = new User(); user.Id = 2; user.Username = “user.name”; user.Name = “ABC 123”; context.UpdateObject(user); context.SaveChanges(); 提前致谢。 编辑:此外,重要的是只更新受影响的属性。 这可能吗?

首先使用EF代码对Id字段进行asp.net mvc 3validation

我有以下型号: public class Product { [Key] [HiddenInput(DisplayValue = false)] public int Id { get; set; } [Required] [StringLength(10)] public string ProductCode { get; set; } [Required] [StringLength(40)] public string ProductName { get; set; } } 以及控制器中的以下一对Add方法: [HttpGet] public ActionResult Add() { return View(); } [HttpPost] [ValidateInput(false)] [ValidateAntiForgeryToken] public ActionResult Add(Product product) { productRepository.Add(product); return RedirectToAction(“Index”); […]

使用Entity Framework时,获取相同数据库中的总计数和页面行数

我目前正在使用以下方法来获取客户页面以及总计数。 唯一的问题是我正在进行2次数据库访问 – 一次用于获取总计数,另一次用于获取页面的实际行数。 我的问题是: 我可以将totalcount查询与实际行查询结合起来,以便Entity Framework在单个数据库中发送两个查询吗? public IList GetPageOfCustomers(string name, int skipCount, int pageSize, out int totalCount) { using(CustomerEntities e = new CustomerEntities()) { //FIRST QUERY var query = (from c in e.Customers where c.NAME.Contains(name) select new { c.CustomerID, c.NAME, c.CITY, c.STATE, c.COUNTRY }) .Distinct() .OrderBy(s = > s.NAME) .ThenBy(s = > s.CITY) .ThenBy(s […]

EF 4,如何添加部分类

我需要扩展我的EF部分类,因为我想添加一些function来使用Oracle的序列,但是我真的不知道如何使用这个部分类的东西,我制作了一个单独的.cs文件并将其命名为一个我自动生成的类如下: namespace GlassStoreDAL { public partial class CAR { private int _sequences; public int sequences { get { return _sequences; } set { _sequences = value; } } } } 现在我认为,在我的BLL上 – 它引用了GlassStoreDAL – 我可以找到我的“序列”属性,但显然出现了问题,我将不胜感激。 这是我生成的部分类,我应该还有序列属性吗? [EdmEntityTypeAttribute(NamespaceName=”Model”, Name=”CAR”)] [Serializable()] [DataContractAttribute(IsReference=true)] public partial class CAR : EntityObject { #region Factory Method /// /// Create a new […]

使用现有对象插入新对象

我是EF 4的新手,这是我到目前为止所做的: 根据我的数据库创建edmx文件 为我的对象(PO​​CO)创建代码生成。 现在我有了一个model1.tt,扩展后我会看到我的课程 基于IRepository为每个类创建一个存储库 现在,我正在处理两个对象,A和B.对象A具有类型B的属性。在我的winform中,我有一个组合填充了类型B的对象。当按下保存按钮时,A类的新实例是创建并设置所有属性。 对象B属性设置如下: objectA.myObjectB = (objectB)cmbBObjects.selectedItem; 然后我为objectA创建一个存储库并调用save方法。 在这个保存方法中,我有这个代码± public bool Save(ObjectA obj) { using(MyContext context = new MyContext()) { context.objectAs.AddObject(obj); context.SaveChanges(); } } 这段代码确实为数据库保存了一个新条目,但它也为对象B创建了一个新记录! 我不想要这个,因为对象B已经存在于数据库中! (我从combobox中选择了这个)。 这就是我填充combobox的方式: 在objectB存储库中: public IList GetAll() { using(MyContext context = new MyContext()) { IList objects = context.objectBs.ToList(); return objects; } } 在我的forms: ObjectBRepository rep = […]

使用Entity Framework 4和Linq查询比较DateTime属性中的日期的简单方法

我正在尝试运行以下一些代码,但是通过不将我期望的实体交给我来进行比较失败。 它比较06/09/2011 12:25:00 06/09/2011 0:00:00到06/09/2011 12:25:00 ,后者是我的数据库记录值。 这就是比较失败的原因,我没有得到我需要的记录。 我只是想比较日期是否匹配,我不关心时间。 DateTime today = DateTime.Now.Date; var newAuctionsResults = repo.FindAllAuctions() .Where(a => a.IsActive == true || a.StartTime.Value == today) .ToList(); 我怎样才能比较日期? 如果在.StartTime.Value部分使用.Date属性,我会得到一个exception: LINQ to Entities不支持指定的类型成员“Date”。 仅支持初始化程序,实体成员和实体导航属性。