Tag: entity framework core

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

在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核心:与同一实体的多对多关系

我试图与同一个实体映射多对多关系。 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 […]

EF Core中是否有唯一约束的数据注释(代码优先)?

我想知道在Entity Framework Core 2代码第一种方法中是否存在唯一约束的数据注释?

ASP.NET Core RC2种子数据库

我的问题是我试图用数据种子entity framework核心数据库,在我看来下面的代码显示工作。 我已经意识到这不应该在ApplicationDbContext构造函数中调用,应该从startup调用,但我不知道如何做到这一点。 编辑:根据Ketrex提供的解决方案,我的解决方案如下: Startup.cs: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { … app.ApplicationServices.GetRequiredService().Seed(); } 种子延伸: public static class DbContextExtensions { public static void Seed(this ApplicationDbContext context) { // Perform database delete and create context.Database.EnsureDeleted(); context.Database.EnsureCreated(); // Perform seed operations AddCountries(context); AddAreas(context); AddGrades(context); AddCrags(context); AddClimbs(context); // Save changes and release resources context.SaveChanges(); context.Dispose(); […]

在Entity Framework Core中使用SQL视图

例如,我有这样的模型: public class Blog { public int BlogId { get; set; } public string Url { get; set; } public BlogImage BlogImage { get; set; } } public class BlogImage { public int BlogImageId { get; set; } public byte[] Image { get; set; } public string Caption { get; set; } public int BlogId […]

entity framework7中是否存在DbSet .Local等效项?

我需要一个 ObservableCollection 在EF7中, DbSet.Local 似乎不存在; 有没有解决方法?

映射类别父ID自引用表结构到EF Core实体

数据库表: 图片 我尝试了这种方法将类别表映射到EF核心: protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => { entity .HasMany(e => e.Children) .WithOne(e => e.Parent) .HasForeignKey(e => e.ParentId); }); } 实体: [Table(“Category”] public class Category : EntityBase { [DataType(DataType.Text), MaxLength(50)] public string Name { get; set; } public int? ParentId { get; set; } public int? Order { get; set; } […]

无法让ASP.NET MVC 6 Controller返回JSON

我有一个MVC 6项目,我正在使用Fiddler来测试Web API。 如果我采取以下控制器操作,使用EntityFramework 7返回List。 然后html将呈现正常。 [HttpGet(“/”)] public IActionResult Index() { var model = orderRepository.GetAll(); return View(model); } 但是当我尝试返回Json响应时,我得到502错误。 [HttpGet(“/”)] public JsonResult Index() { var model = orderRepository.GetAll(); return Json(model); } 关于为什么对象没有正确序列化为json的任何想法?

我应该如何在MVC Core中管理DbContext Lifetime?

来自文档 应使用Scoped生存期将entity framework上下文添加到服务容器中。 如果您使用如上所示的辅助方法,则会自动完成此操作。 将使用entity framework的存储库应使用相同的生命周期。 我一直认为,我应该为我必须处理的每一个工作单元创建一个新的Context 。 这让我想一想,如果我有一个ServiceA和ServiceB ,它们在DbContext上应用不同的操作,他们应该得到一个不同的DbContext实例。 文档内容如下: Transient物体总是不同的; 为每个控制器和每个服务提供一个新实例。 Scoped对象在请求中是相同的,但在不同的请求中是不同的 回到ServiceA和ServiceB ,对我来说, Transient更适合。 我研究过,每个HttpRequest只能保存一次Context,但我真的不明白这是如何工作的。 特别是如果我们看一个服务: using (var transaction = dbContext.Database.BeginTransaction()) { //Create some entity var someEntity = new SomeEntity(); dbContext.SomeEntity.Add(someEntity); //Save in order to get the the id of the entity dbContext.SaveChanges(); //Create related entity var relatedEntity = new RelatedEntity { […]

如何从代码中应用迁移(EF Core)

如何从代码中应用迁移 对于EF6工作代码 Database.SetInitializer(null); var settings = new MigrationsConfiguration(); var migrator = new DbMigrator(settings); migrator.Update(); 如何在EF Core中制作类似的?