Tag: entity framework core

entity framework核心SelectMany然后包括

我似乎无法弄清楚如何在使用SelectMany时让EF Core包含/加载相关对象。 context.MyObject .Where(w => w.Id == Id) .SelectMany(m => m.SubObject) .Include(i => i.AnotherType) 本来会想到类似上面的东西会起作用,但是折叠的SubObject集合使AnotherObject为null并且不包括在内。 一直在寻找几个小时。 任何帮助,将不胜感激。 谢谢

如何在SaveContext上更新已修改和已删除的实体?

目标是跟踪谁更改并删除了一个实体。 所以我有一个实现接口的实体: interface IAuditable { string ModifiedBy {get;set;} } class User: IAuditable { public int UserId {get;set;} public string UserName {get;set;} public string ModifiedBy {get;set;} 1541692055 public byte[] RowVersion { get; set; } } 现在,实体删除操作的代码可能如下所示: User user = context.Users.First(); user.ModifiedBy = CurrentUser.Name; context.Users.Remove(employer); context.SaveContext(); 实际上: ModifiedBy 更新将永远不会执行 (当我的db历史触发器期望“处理”它时)。 只有删除语句才会在DB 上执行 。 我想知道如果实体被修改,如何强制EF Core“更新”已删除的实体/条目(实现特定的接口)。 注意: RowVersion增加了额外的复杂性。 […]

在EF Core中添加一对一关系时迁移数据?

在我的一个表中添加新的一对一关系后,我无法弄清楚如何为我的数据库中的现有行添加默认数据。 升级前我的数据库基本上看起来像这样: — Team — Name: TEXT — History — Id: INT …其中历史是从其他不相关的表指向它的外键。 在我的升级中,我基本上希望团队有一个历史记录,所以我的新数据库看起来像: — Team — Name: TEXT HistoryId: INT — History — Id: INT 我现在的问题是,我的数据库中有现有的团队,他们需要指向唯一的历史记录行,因此我需要为每个现有的团队创建一个新的历史记录行。 我尝试在迁移中手动添加Up方法中的条目,但由于我的模型与现有模式不匹配,因此失败。 protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( name: “HistoryId”, table: “Team”, nullable: false, defaultValue: 0); using (var db = new XMDBContext()) { foreach (var team in db.Team) […]

如何从Entity Framework Core中的DbContext获取列名和相应的数据库类型

假设我有这个表: 如何从Entity Framework Core中的DbContext获取列名和数据库数据类型? 提示 名为clg#的列由EF Core Scaffold工具转换为clg1,因此我需要真正的列名而不是当前的EF名称 我需要数据库类型,而不是clrType,当然必须是跨平台。 也许我会改变数据库,所以方法也必须工作。 期望的结果: 谁能提供解决方案?

当相关对象在.Include中为null时,Entityframework Core 2.0错误

我正在使用EF core 2.0和.Net core2.0来开发我的webapi应用程序。 在下面的查询中,当案例没有工作流程(即可空的工作流程)时,我会遇到exception。 我得到的错误是’System.NullReferenceException:对象引用未设置为对象的实例。’ 当c.workflow为null时,我怀疑.ThenInclude()上的exception。 案例实体: public class Case { [Key] public int CaseId { get; set; } public int? WorkflowId { get; set; } public Workflow Workflow { get; set; } } 查询: var item = await _context.Cases .Include(c => c.CaseDetail) .Include(c => c.Workflow).ThenInclude(wf => wf.CurrentWorkflowLevel) .Include(c => c.Alerts) .FirstOrDefaultAsync(c => c.CaseId […]

EntityFramework Core 1.1.0缺少Include()?

我正在使用EntityFramework Core 1.1.0。 我可以查询表并加载实体,但Microsoft的指令表明我是否要加载关系数据,我应该使用.Include()函数: https://docs.microsoft.com/en-us/ef/core/querying/related-data 您可以使用Include方法指定要包含在查询结果中的相关数据。 在以下示例中,结果中返回的博客将使用相关post填充其“ Posts属性。 using (var context = new BloggingContext()) { var blogs = context.Blogs .Include(blog => blog.Posts) .ToList(); } 我没有.Include()选项。 任何想法为什么缺少或如何加载外键关系数据? this.context.Mail .Include(“Files”) // This is missing 我已经使用显式加载关系数据。 这适用于小型结果集,但随着我的数据集的增长,这将让我感到悲伤。 var mails = this.context.Mail.ToList(); mails.ForEach(mail => { this.context.Entry(mail) .Collection(m => m.Files) .Load(); });

升级到v1.1后,ASP.NET Core将无法在IIS中运行

我已经升级了我的project.json以使用asp.net核心的新v1.1,现在当我尝试启动IIS Express进行调试时,我收到以下错误: 程序'[8784] dotnet.exe’已退出,代码为-2147450749(0x80008083)。 程序'[7352] iisexpress.exe’已退出,代码为0(0x0)。 我升级的project.json: “dependencies”: { “Microsoft.NETCore.App”: { “version”: “1.1.0”, “type”: “platform” }, “Microsoft.ApplicationInsights.AspNetCore”: “1.0.0”, “Microsoft.AspNetCore.Mvc”: “1.1.0”, “Microsoft.AspNetCore.Routing”: “1.1.0”, “Microsoft.AspNetCore.Server.IISIntegration”: “1.1.0”, “Microsoft.AspNetCore.Server.Kestrel”: “1.1.0”, “Microsoft.Extensions.Configuration.EnvironmentVariables”: “1.1.0”, “Microsoft.Extensions.Configuration.FileExtensions”: “1.1.0”, “Microsoft.Extensions.Configuration.Json”: “1.1.0”, “Microsoft.Extensions.Logging”: “1.1.0”, “Microsoft.Extensions.Logging.Console”: “1.1.0”, “Microsoft.Extensions.Logging.Debug”: “1.1.0”, “Microsoft.Extensions.Options.ConfigurationExtensions”: “1.1.0”, “Microsoft.IdentityModel.Tokens”: “5.1.0”, “Microsoft.AspNetCore.Authentication”: “1.1.0”, “Microsoft.AspNetCore.Authentication.JwtBearer”: “1.1.0”, “Microsoft.AspNetCore.Authentication.Cookies”: “1.1.0”, “Microsoft.AspNetCore.Identity.EntityFrameworkCore”: “1.1.0”, “Microsoft.EntityFrameworkCore”: “1.1.0”, “Microsoft.EntityFrameworkCore.Tools”: “1.0.0-preview2-final”, “SapientGuardian.EntityFrameworkCore.MySql”: […]

添加迁移值不能为空。 参数名称:语言

我正在尝试创建一个新的迁移,但我得到一个System.ArgumentNullException说: System.ArgumentNullException: Value cannot be null. Parameter name: language at Microsoft.EntityFrameworkCore.Utilities.Check.NotNull[T](T value, String parameterName) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations..ctor (IOperationReporter reporter, Assembly assembly, Assembly startupAssembly, String projectDir, String rootNamespace, String language) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.c__DisplayClass4_0.b__4() at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.c__DisplayClass3_0`1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) Value cannot be null. Parameter name: language

如何将相同的列添加到EF Core中的所有实体?

想象一下,我想在我的所有实体中添加一个IsDeleted列或一些审核列。 我可以创建一个基类,我的所有实体都将从该基类inheritance,这将解决我的问题,但是我无法指定创建列的顺序,因此我将在实体的字段之前得到所有审计字段,我不想要。 我希望他们在桌子的尽头。 在标准版本的entity framework中,我们可以使用指定列顺序的注释来完成此操作。 但是,目前EF核心并不存在这样的事情。 我可以使用OnModelCreating()方法上的流畅api来做,问题是我只知道如何为我的每个实体单独执行它,这意味着我必须为我拥有的每个实体编写相同的代码。 对于我的所有实体,我有什么办法可以做到这一点吗? 某种for循环迭代遍历dbcontext上DbSets中注册的所有实体?

EF 7 beta 6:EF 7中具有一对多关系的实体

我有一个非常简单的模型: class Bag { public int Id { get; set; } public ICollection RandomThings { get; set; } } class RandomThing { public int Id { get; set; } public String Description{ get; set; } } 我的上下文的OnModelCreating (在EF 6中 )类似于: protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity() .ToTable(“Bag”) .HasMany(x => x.RandomThings).WithRequired(x => x.Bag).WillCascadeOnDelete(); modelBuilder.Entity().ToTable(“RandomThing”); […]