entity-framework-core

Can I use an Interface as a property with Entity Framework?

限于喜欢 提交于 2021-02-05 04:55:35
问题 I have the following class which is a model for a database table: public class User : IUser { internal int Id { get; set; } public string Name { get; set; } public string Email { get; set; } public ITest TestData { get; set; } } When I run Update-Database command, it throws an error: The property User.TestData is of an interface type ( ITest ). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type How and where can I

retrieve json with entityframework core

萝らか妹 提交于 2021-02-04 21:32:07
问题 I have store procedures returning json, thanks to for json path. How do you consume them with entity-framework-core? The following doesn't work: var foo = _db.Set<JObject>() .FromSql("dbo.Mine @customerid = {0}", _user.guid) .FirstOrDefault(); Because JObject type is not part of the model: InvalidOperationException: Cannot create a DbSet for 'JObject' because this type is not included in the model for the context. But how are we supposed to do that with entity-framework-core? 回答1: After

retrieve json with entityframework core

十年热恋 提交于 2021-02-04 21:31:48
问题 I have store procedures returning json, thanks to for json path. How do you consume them with entity-framework-core? The following doesn't work: var foo = _db.Set<JObject>() .FromSql("dbo.Mine @customerid = {0}", _user.guid) .FirstOrDefault(); Because JObject type is not part of the model: InvalidOperationException: Cannot create a DbSet for 'JObject' because this type is not included in the model for the context. But how are we supposed to do that with entity-framework-core? 回答1: After

Many-to-many query in Entity Framework 7

假装没事ソ 提交于 2021-02-04 18:09:09
问题 I am following this example which I got from http://ef.readthedocs.org/en/latest/modeling/relationships.html class MyContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag

Many-to-many query in Entity Framework 7

假装没事ソ 提交于 2021-02-04 18:07:35
问题 I am following this example which I got from http://ef.readthedocs.org/en/latest/modeling/relationships.html class MyContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag

Many-to-many query in Entity Framework 7

99封情书 提交于 2021-02-04 18:07:17
问题 I am following this example which I got from http://ef.readthedocs.org/en/latest/modeling/relationships.html class MyContext : DbContext { public DbSet<Post> Posts { get; set; } public DbSet<Tag> Tags { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<PostTag>() .HasKey(t => new { t.PostId, t.TagId }); modelBuilder.Entity<PostTag>() .HasOne(pt => pt.Post) .WithMany(p => p.PostTags) .HasForeignKey(pt => pt.PostId); modelBuilder.Entity<PostTag

Entity Framework Core - Migration - No Parameterless Constructor Defined for this Object

不问归期 提交于 2021-02-04 17:19:45
问题 I am working with the latest .Net Core and EF Core in Visual Studio 2017. I have created a model and it was working great. I have since made some modifications and am getting the following error when I try to add a new migration: Build succeeded. 0 Warning(s) 0 Error(s) Time Elapsed 00:00:09.08 System.MissingMethodException: No parameterless constructor defined for this object. at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&

Replacing a entity collection in Entity Framework Core causes DbContext to fetch the new values when not saved to db. How to reload the collection?

99封情书 提交于 2021-02-04 08:36:08
问题 What I can't understand fully is exemplified in a unit test below. Basically I want to know why DbContext overrides collection values currently stored in the database even if Reload() and a new load for the collection has been called. If I try to call dbContext.Entry(test.TestChildren).Reload(); I get the exception: System.InvalidOperationException: 'The entity type 'List' was not found. Ensure that the entity type has been added to the model.' I have also loaded TestChildren explicitly via

Entity Framework Core - Output DB agnostic with version 5+

為{幸葍}努か 提交于 2021-02-04 07:31:10
问题 I have an app that uses EF Core and was originally written using V3.1.1. We have decided to take the plunge and update to v5.0.1 now its out of preview. The app supports both MSSQL and SQLite and originally this worked pretty well. Migrations were created as mostly agnostic by the tooling and the documentation here provided 3 ways to use multiple databases: Create multiple context's and switch between them using the --context option to dotnet ef migrations . Create multiple migration

How to resolve error with enitiy freamwork include

时光怂恿深爱的人放手 提交于 2021-01-29 21:52:35
问题 I write app using .Net 5 and Entity Freamwork Core. I have method that is looking for some data. It looks like this: public async Task<Result<List<Company>>> Search(string keyword, DateTime From, DateTime To, JobTitle jobTitle) { return Result.Ok(await _dataContext.Companies .Include(x => x.Employes .WhereIf(From != default && To != default, x => x.DateOfBirth >= From && x.DateOfBirth <= To && x.JobTitle == jobTitle)) .WhereIf(!string.IsNullOrEmpty(keyword), x => x.Name.Contains(keyword))