entity-framework-core

EF foreign key reference using Id vs object

久未见 提交于 2020-06-13 05:47:51
问题 What is the difference between foreign key reference using Id vs object. For example: FK relation using Id class Product { public int Id { get; set; } public string Name { get; set; } public string PhoneNumber { get; set; } public int CategoryId { get; set; } } vs FK relation using object class Product { public int Id { get; set; } public string Name { get; set; } public string PhoneNumber { get; set; } public Category Category { get; set; } } I have noticed from database that by using Id

Get List of Modified Objects within Entity Framework 7

こ雲淡風輕ζ 提交于 2020-06-11 20:10:21
问题 I am stumped - upgrading to Entity Framework 7 and I typically override the SaveChanges inside the DbContext to be able to get a list of all the Modified Objects before it changes. Ultimately I have a script that fires that tracks the previous version in a database. In Entity Framework 6 I would get the model changes like so: var oc = ((IObjectContextAdapter)this).ObjectContext; var modifiedItems = oc.ObjectStateManager.GetObjectStateEntries(EntityState.Modified | EntityState.Deleted); List

How to tell Entity Framework that my ID column is auto-incremented (AspNet Core 2.0 + PostgreSQL)?

徘徊边缘 提交于 2020-06-10 07:39:05
问题 Code is simple. Tag.cs entity: public partial class Tag { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } HomeController.cs class: public async Task<IActionResult> Index() { tagRepository.Insert(new Tag { Name = "name", Description = "description" }); await UnitOfWork.SaveChangesAsync(); // calls dbContext.SaveChangesAsync() return View(); } TagRepository.cs class: // Context it

Mocking EF core dbcontext and dbset

好久不见. 提交于 2020-06-09 12:02:48
问题 I am using ASP.NET Core 2.2, EF Core and MOQ. When I run the test I am getting this error: Message: System.NotSupportedException : Invalid setup on a non-virtual (overridable in VB) member: x => x.Movies What I am doing wrong? public class MovieRepositoryTest { private readonly MovieRepository _sut; public MovieRepositoryTest() { var moviesMock = CreateDbSetMock(GetFakeListOfMovies()); var mockDbContext = new Mock<MovieDbContext>(); mockDbContext.Setup(x => x.Movies).Returns(moviesMock.Object

One To Many returns empty array (solved)

爷,独闯天下 提交于 2020-06-07 05:49:45
问题 I'm trying to connect two objects with one-to-many relationship, Workplace can hold multiple People, while one Person can only have one Workplace. When I execute this code snippet and check to see the results, p1 has properly assigned w1 as the p1.Workplace, but the list of people of the w1.employees is always empty. Do I have to manually add p1 to w1.Employees even after I assigned w1 to p1.Workplace? SeedData.cs (code snippet) var w1 = new Workplace { EntryCode = "1111" //[...] other data I

Method 'get_Info' in type 'Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension

拟墨画扇 提交于 2020-06-01 06:48:36
问题 I upgraded my web api from .netcore2.1 to 3.1 and updated all nuget packages to their latest version. Now my api throws the following error {"StatusCode":500,"ErrorMessage":"Internal server error: Method 'get_Info' in type 'Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal.SqlServerOptionsExtension' from assembly 'Microsoft.EntityFrameworkCore.SqlServer, Version=2.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' does not have an implementation Stack: at Microsoft

Eager Loading in Entity Framework Core 2.2

丶灬走出姿态 提交于 2020-06-01 05:52:45
问题 Have Entity Framework Core 2.2 removed Eager Loading? The include method won't appear in Visual Studio intellisense, and when I write it manually it gives an error: "it does not contain a definition for 'Include' and no accessible extensions..." Example School school = _context.Students.Include... My demo is made with a simple ASP Core 2.2. Web Application. Not much have been changed. The project use this packages: <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.App" />

C# - How to get the directory path of a .NET Standard Library project inside a WPF solution

徘徊边缘 提交于 2020-06-01 05:30:32
问题 I have a VS2019 solution that has a WPF project referencing a .NET Standard 2.0 project included in the same solution. I am using Entity Framework Core on the .NET Standard Library project where the DbContext class is created as follows. Because the MyDbContext.cs file is inside the Standard Library project, I was expecting AppDomain.CurrentDomain.BaseDirectory directory in the code below to be inside \bin\Debug\ of the Standard Library project where MySQLite.db should have been created . But

Entity Framework Core throws System.Data.SqlTypes.SqlNullValueException when loading entities from DbContext

天涯浪子 提交于 2020-06-01 05:10:22
问题 I have this MyEntity table in the database: The data inside MyEntity is the following: The EF Core entity is the following: public class MyEntity { public int Id { get; set; } public int MyInt { get; set; } } I am getting an exception: System.Data.SqlTypes.SqlNullValueException: 'Data is Null. This method or property cannot be called on Null values.' when trying to load the MyEntity from the DbContext : var myEntities = dbContext.Set<MyEntity>.ToList(); What am I doing wrong? 回答1: You are

EF Core unable to determine relationship represented by foreign keys in abstract class

可紊 提交于 2020-05-30 08:47:10
问题 I'm trying to implement an abstract class for CRUD objects which contains two references to the User class; one for the User which created the object and the other for the User which last modified it. Entity framework is unable to determine the relationship represented by the CreatedBy and ModifiedBy navigation properties on the class inheriting them (Department). A potential additional complication is that the User class also has a property of class Department, which is unrelated to the