Tag: entity framework

如何使用Entity Framework 7在代码中首先处理这两个模型?

问题 我正在编写将模拟库的概念ASP.NET 5 MVC6的certificate。 我正在使用CTP6和beta3。 如何使用代码优先方法在Entity Framework 7中定义它们的关系? 货架和书籍需要连接表。 在我看来,这个应用程序将一次考虑一个架子和自己的书籍。 换句话说,一个书架与书籍有一对多的关系。 楷模 货架型号: public class Shelf { public int ShelfId { get; set; } public string ShelfName { get; set; } public ShelfType MyProperty { get; set; } public virtual List Books { get; set; } public DateTime Created { get; set; } public string […]

如何创建嵌套的GridView来编辑EF Code First关系?

我有一个经典的父子关系,我想通过使用asp:GridView控件来CRUD。 对于CRUD,父级很容易,但挑战是在asp:GridView嵌套asp:GridView ,它能够处理子关系。 为了使问题更容易,我构建了一个例子。 请考虑以下EF代码: public class Context : DbContext { public DbSet Animals { get; set; } public DbSet Tags { get; set; } } public class Animal { public int AnimalID { get; set; } public string Name { get; set; } public virtual IEnumerable Tags { get; set; } } public class […]

为最小int值设置约束

我正在使用Repository模式。 我有一个名为Product的实体,我想设置价格的最小值以避免零价格。 是否可以在EntitytypeConfiguration类中创建它? 我的产品配置类 public class ProductConfiguration : EntityTypeConfiguration { public PlanProductConfiguration(string schema = “dbo”) { ToTable(schema + “.Product”); HasKey(x => new { x.IdProduct }); Property(x => x.Price).HasColumnName(“flt_price”) .IsRequired() .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); } }

如何简化与entity framework的有多种关系的访问?

这是我想要做的: var user = db.User.First(conditions); user.Book.First(); 目前这是我必须这样做的。 var user = db.User.Include(“Book”).First(conditionsForUser); user.Book.First(); 我想简化这个的原因是因为我不想在每次想要访问关系时指定包含的内容。 看起来很麻烦。 例如:鉴于我之前已经检索过用户,我希望能够执行以下操作: user.Book.First() user.Blog.First() user.SomeOtherHasManyRelationship.Where(conditions) 这是我到目前为止: public object RelationshipFor(string relationship) { using (var db = User.DbContext()) { var relationshipType = TypeRepresentedBy(relationship); // unused for now, not sure if I need the type of the relationship var myTable = ((ICollection)db.Send(RelationshipName)); // RelationshipName is […]

entity framework对鉴别器做了案例

有没有办法将内部查询转换为子类? 如果我只查询单个子类,我会这样做 auctions.OfType().Where(auctionBid => auctionBid.AuctionBidProp) 现在我想对子类的类型做一个案例 auctions.Where(auction => (auction is AuctionBid) && ((AuctionBid) auction).Prop == 1 || (auction is AuctionBuy) && ((AuctionBuy) auction).Prop == 1) 有没有办法做到这一点? 当然上面的行给出了一个错误:LINQ to Entities仅支持转换EDM原语或枚举类型。

在实体上显式加载多个引用/集合

考虑以下实体模型: public class Parent { public virtual FirstChild FirstChild { get; set; } public virtual SecondChild SecondChild { get; set; } } 在我的代码中,我加载了Parent实体: Parent parent = ; 为了明确加载其导航属性,我使用 db.Entry(parent).Reference(p => p.FirstChild).Load(); db.Entry(parent).Reference(p => p.SecondChild).Load(); 但这导致两个DB查询。 问题:是否有更优雅的方式,允许在单个查询中显式加载多个导航属性? 如果我没有parent加载,我会急切加载: Parent parent = db.Parents .Include(p => p.FirstChild) .Include(p => p.SecondChild) .FirstOrDefault(); 但是,正如我所提到的,我已经加载了没有相关实体(我无法修改加载代码)。

附加和更新boolean或int并不总是有效

使用Entity Framework 5,给定一个存在于数据库中的对象messenger , published了比特属性,在以前版本的EF中,我会像这样更新它: using (var c = new EFContext()) { Data.Messenger cm = new Messenger { MessageId = messageId }; c.Messengers.Attach(cm); cm.Published = newPublishedValue; c.SaveChanges(); } 但是,如果newPublishedValue为true ,则使用EF5,但如果newPublishedValue为false ,则不对数据库进行任何更改。 同样,此模式不会将整数值设置为0。 这里发生了什么? 更换 c.Messengers.Attach(cm); 同 c.Entry(cm).State = System.Data.EntityState.Modified; 修复了这个问题,但是如果EF5不会更新我的new Data.Messenger属性的默认结构值,那么这不会使Attach方法有点无用吗? 我在这里想念的是什么?

检索数据时出现EF错误:SQL中的表名错误

我有一个带2个表的简单edmx 。 这些表由单个Navigation Property 。 (1对多)。 当我运行我的代码时,我得到一个例外:“无效的对象名称dbo.Enquiries” 数据库中没有dbo.Enquiries (它实际上称为dbo.Enquiry ),因此错误本身是不言自明的。 但它在哪里找到了Name,我该如何解决? 编辑以按要求显示代码。 var foo = (from d in context.Dealerships join e in context.Enquiry on d.Id equals e.DealershipId where (d.ParentBusinessId == id) select d).AsEnumerable(); 这是生成的sql。 foo {SELECT [Extent1].[Id] AS [Id], [Extent1].[BusinessName] AS [BusinessName] FROM [dbo].[Dealerships] AS [Extent1] INNER JOIN [dbo].[Enquiries] AS [Extent2] ON [Extent1].[Id] = [Extent2].[DealershipId] […]

当我从现有数据库创建模型时,Entity Framework 4.0生成只读模型

我正在开发一个自定义的ado.net提供程序并使用该提供程序我在Visual Studio 2010中集成了entity framework支持。我正在创建所有可能的映射并从数据库中读取表对象的所有相关元数据。 对于我的测试,表包含主键和其他字段。 当我使用这个表从数据库创建一个模型时,我得到一个包含所有列映射和所有内容的模型,但我也得到以下错误消息: 生成模型时出现警告或错误。 有关详细信息,请参阅错误列表。 在运行应用程序之前必须解决这些问题。 从数据库加载元数据花了00:00:11.4799371。 生成模型花了00:00:04.2751189。 将连接字符串添加到App.Config文件。 编写.edmx文件时间为00:00:00.0005060。 如果我用XML编辑器打开.edmx文件,我会看到以下错误: 它还为表添加了一个带有SELECT …语句的DefiningQuery。 我正在围成一圈寻找解决方案并拼命寻求这个问题的一些帮助。

EntityFramework从SQL Server View返回损坏/交换的数据

我从SQL Server中的视图中进行简单查询: SELECT [PricePerM] FROM RealtyStatParent ORDER BY PricePerM 当我在SQL Management Studio中执行查询时,我得到了正确的结果。 这意味着我从1.00开始获得2532行,结束时为173543.6893。 当我使用entity framework从C#进行查询时,我得到了相同的结果: var justDecimals = context.RealtyStatParents .OrderBy(item => item.PricePerM) .Select(item => item.PricePerM) .ToArray(); 到现在为止没什么特别 但我真正不理解的是跟随查询。 我首先选择整行,然后选择价格(十进制)。 var entireRows = context.RealtyStatParents .OrderBy(item => item.PricePerM) .ToArray(); var decimalFromRows = entireRows .Select(item => item.PricePerM) .ToArray(); PricePerM的许多值重复(值1或48)而不是实际值,并且结果集未正确排序。 EF设计器中行的定义很简单: public partial class RealtyStatParent { public Nullable PricePerM […]