Tag: entity framework

entity frameworkSaveChanges()不更新数据库

var paymentAttempt = _auctionContext.PaymentAttempts.Where(o => o.Id == paymentAttemptId).SingleOrDefault(); if (paymentAttempt != null) { paymentAttempt.PaymentAttemptStatusId = (int)PaymentAttemptStatus.Defunct; paymentAttempt.PaymentAttemptStatus = _auctionContext.PaymentAttemptStatuses.Where(pas => pas.Id == paymentAttempt.PaymentAttemptStatusId).First(); var relevantWinningBidsTotalPrices = _auctionContext.GetWinningBidsTotalPricesForPaymentAttempt(paymentAttemptId).ToArray(); foreach (var winningBid in relevantWinningBidsTotalPrices) { winningBid.Locked = false; _auctionContext.UpdateObject(winningBid); } _auctionContext.SaveChanges(); } 在上面的代码之后 _auctionContext.SaveChanges(); 被称为winningBid按预期更新,但paymentAttempt不是。 为什么是这样? 真的很令人沮丧。 也没有错误。 如果像EF一样没有跟踪对象或类似的问题,我会发现无法发生,但是没有发生这样的错误。

从.NET Core 1.0类库引用mscorlib 4.0.0.0

我有一个.NET Core 1.0类库,它以.NET 4.6.1为目标,并引用了.NET标准库1.6.0和Identity Framework 2.2.1 project.json { “version”: “1.0.0-*”, “dependencies”: { “Microsoft.AspNet.Identity.EntityFramework”: “2.2.1”, “System.Runtime”: “4.1.0”, “NETStandard.Library”: “1.6.0” }, “frameworks”: { “netstandard1.6”: { “imports”: [ “net461” ] } } } 在我的项目中,我只是创建了身份模型,它扩展了基本的Identity Framework模型(用户,角色等)。 当我尝试编译时,会发生这种情况…… 任何想法如何解决这个问题?

entity framework6编译LINQ查询

我正在尝试通过缓存查询来提高Web应用程序的性能。 public static Func<myEntity, List, IQueryable> CompiledDuplicatedResponses = CompiledQuery.Compile<myEntity, List, IQueryable>( (db, hashes) => from r in db.FormResponse from h in db.IndexHASHes from d in hashes where r.id == h.FormResponseID && h.IndexHASHString == d.hash select r); 我收到的错误是在编译时: 类型’myEntity’不能在generics类型或方法’System.Data.Entity.Core.Objects.CompiledQuery.Compile(System.Linq.Expressions.Expression>)’中用作类型参数’TArg0’。 没有从’myEntity’到’System.Data.Entity.Core.Objects.ObjectContext’的隐式引用转换。 我正在使用EF6

entity framework可以使用单个SaveChanges()添加许多相关实体吗?

我正在向数据库编写许多(20多个)父子数据集,并且EF要求我在每个集合之间保存更改,否则它会抱怨无法找出主键。 是否可以将数据刷新到SQL Server,以便EF可以从身份中获取主键,并在写入所有更改结束时发送SaveChanges? foreach (var itemCount in itemCounts) { var addItemTracking = new ItemTracking { availabilityStatusID = availabilityStatusId, itemBatchId = itemCount.ItemBatchId, locationID = locationId, serialNumber = serialNumber, trackingQuantityOnHand = itemCount.CycleQuantity }; _context.ItemTrackings.Add(addItemTracking); _context.SaveChanges(); var addInventoryTransaction = new InventoryTransaction { activityHistoryID = newInventoryTransaction.activityHistoryID, itemTrackingID = addItemTracking.ItemTrackingID, personID = newInventoryTransaction.personID, usageTransactionTypeId = newInventoryTransaction.usageTransactionTypeId, transactionDate = newInventoryTransaction.transactionDate, usageQuantity […]

如何更新Entity Framework中的相关实体

我有一个MVC项目,并为数据库使用Entity Framework Code First和POCO对象。 例如: public class ClassA { public int? Id {get; set;} public string Name { get; set;} public virtual ClassB B {get; set;} } public class ClassB { public int? Id {get;set;} public string Description {get;set;} } 我有一个ActionResult,可以创建或编辑模型。 问题是当我调用此ActionResult来更新模型,并且model.B已更改时,关系不会保存在数据库中。 当调用ActionResult来创建一个新对象时,它按预期工作。 我该如何解决这个问题? public ActionResult Save(ClassA model) { model.B = GetBFromDb(model.B.Id.Value); if(ModelState.IsValid) { if […]

如何在entity frameworkDbContext中使用dependency injection?

我目前正致力于为网站添加新function。 我有一个使用EF6创建的DbContext类。 该网站使用主布局,其中根据请求的页面呈现子布局。 我想使用dependency injection来访问Sublayouts中的DbContext。 通常,我会使用Controller来处理调用,但是,我想在这种情况下跳过它。 此外,我希望保持实施的灵活性,以便添加新的DbContexts,我将能够轻松使用它们。 我在考虑创建一个“IDbContext”接口。 我将使用新界面(让我们说“IRatings”)实现这个界面。 我是以正确的方式去做的吗? 有什么想法吗?

在EF Code First中过滤导航属性

我在EF中使用Code First。 假设我有两个实体: public class Farm { …. public virtual ICollection Fruits {get; set;} } public class Fruit { … } 我的DbContext是这样的: public class MyDbContext : DbSet { …. private DbSet FarmSet{get; set;} public IQueryable Farms { get { return (from farm in FarmSet where farm.owner == myowner select farm); } } } 我这样做,以便每个用户只能看到他的农场,我不必调用每个查询的位置到数据库。 现在,我想过滤掉一个农场的所有水果,我尝试了这个(在Farm类中): […]

更改所有字符串属性最大长度

在EF 6中,我可以这样做: modelBuilder .Properties() .Where(p => p.PropertyType == typeof(string) && p.GetCustomAttributes(typeof(MaxLengthAttribute), false).Length == 0) .Configure(p => p.HasMaxLength(2000)); 由于EF7 ModelBuilder没有Properties()函数,我如何在EF7中做同样的事情?

entity framework – 重用复杂类型

我在Code First Entity框架中有一个实体,目前看起来像这样: public class Entity { // snip … public string OriginalDepartment { get; set; } public string OriginalQueue { get; set; } public string CurrentDepartment { get; set; } public string CurrentQueue { get; set; } } 我想为这些类型创建复杂类型,如下所示: public class Location { public string Department { get; set; } public string Queue { […]

entity framework核心:与同一实体的多对多关系

我试图与同一个实体映射多对多关系。 User实体具有用于Contacts的IList数据字段,用于存储用户的联系人/朋友信息: public class User : DomainModel { public virtual IList Contacts { get; protected set; } //irrelevant code omitted } 当我尝试使用流畅的API来映射这么多对多的关系时,它给我带来了一些麻烦。 显然,当我在user.Contacts属性上使用HasMany()时,它没有WithMany()方法来调用next。 Visual Studio中的intellisense仅显示WithOne() ,但不显示WithMany() 。 modelBuilder.Entity().HasMany(u => u.Contacts).WithMany() // gives compile time error: CS1061 ‘CollectionNavigationBuilder’ does not contain a definition for ‘WithMany’ and no extension method ‘WithMany’ accepting a first argument of type […]