entity-framework-core

EF Core 2.1 In memory DB not updating records

ぃ、小莉子 提交于 2019-12-06 04:11:26
I'm using the in memory database provider for integration tests however I don't seem to be able to update a record. I've run the same code against a real SQL database and everything gets updated fine. Here is my test fixture code. Test Fixture: public class TestFixture<TStartup> : IDisposable { private readonly TestServer _testServer; public HttpClient TestClient { get; } public IDatabaseService DbContext { get { return _testServer.Host.Services.GetService<DatabaseService>(); } } public TestFixture() : this(Path.Combine("src")) { } protected TestFixture(string relativeTargetProjectPatentDir) {

EF Core insert explicit value for identity column

放肆的年华 提交于 2019-12-06 03:57:11
I have problem with inserting data to database table which has foreign keys. As error I'm getting Cannot insert explicit value for identity column in table 'MachineTypes' when IDENTITY_INSERT is set to OFF.Cannot insert explicit value for identity column in table 'SpareTypes' when IDENTITY_INSERT is set to OFF. BaseEntity.cs public abstract class BaseEntity { [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] public Int64 Id { get; set; } public DateTime CreateDate { get; set; } } MachineType.cs public class MachineType : BaseEntity { [Required] [StringLength(50)] public string

EF Core 2.1 project Add-Migration NullReferenceException

末鹿安然 提交于 2019-12-06 03:42:01
I am getting a NullReferenceException on a project recently converted to dotnet core 2.1. My EF library project is dotnet core 2.1. Code first migrations. CsvHelper 6.0.2 Microsoft.EntityFrameworkCore 2.1.0 Microsoft.EntityFrameworkCore.Design 2.1.0 Microsoft.EntityFrameworkCore.SqlServer 2.1.0 Microsoft.EntityFrameworkCore.Tools 2.1.0 Output from the add-migration command PM> add-migration -Name MyMigration -Context EscData01Context -Project Data\EscData01 -StartupProject PacmsTri -verbose Using project 'Data\EscData01'. Using startup project 'PacmsTri'. Build started... Build succeeded. C:

Entity Framework Core 2.0 deleting record instead of updating it

拥有回忆 提交于 2019-12-06 01:14:18
问题 I think this could be a bug but thought I might be missing something that could explain the behavior. Any help would be appreciated. ### Technical details EF Core version: Microsoft.EntityFrameworkCore (2.0.1) Database Provider: Microsoft.EntityFrameworkCore.SqlServer (2.0.1) Database: SQL Server 2008 Operating system: Windows 10 IDE: Visual Studio 2017 Community Edition (15.4.4) I've got a situation in a C# console application where I call Update on my DbContext (location is a LocationParty)

How to use injected DbContext in parallel methods in asp.net core and ef7?

老子叫甜甜 提交于 2019-12-06 01:08:26
I have an Asp.net 5/Core 1 application with EF7. I register the DbContext in the DI container normally. services.AddEntityFramework().AddSqlServer() .AddDbContext<MyDbContext>(options => options.UseSqlServer(connection)); Here is a small sample showing what I'm trying to do. public class MyController : Controller { MyDbContext _myDbContext; public MyController(MyDbContext myDbContext) { _myDbContext = myDbContext; } public IActionResult Index() { //Just start these and don't wait for them to finish //We don't care if they succeed or not Task.Run(() => DoSomeLogging1()); Task.Run(() =>

EF Core Migrations with Multiple DB Schemas

假装没事ソ 提交于 2019-12-06 00:42:40
问题 EF Core 1.1 and SQL Server 2016 We are running a microservice application with some microservices having independent few tables. One of the solutions to have many tables for individual microservice, is to give these services a unique schema (e.g. not DBO) and put all of them in one database (good for cost + maintenance). That works fine in EF 6, however, looking at the generated dbo.__EFMigrationsHistory in core, it looks like core doesn't take the schema into account. I am aware that the

Lazy load in Web API Core 2.2

不羁岁月 提交于 2019-12-05 23:47:12
I am having issues with the lazy loading. I have the following dbcontext. public virtual DbSet<AccountGroupMst> AccountGroupMst {get; set;} I have enabled the lazy loading. services.AddDbContext<DBContext>(x => x.UseSqlServer(Configuration.GetConnectionString("Test")) .UseLazyLoadingProxies()); Model, I have virtual and it is a self referencing table. public class AccountGroupMst { [Key] [Required] public int AccountGroupId { get; set; } [MaxLength(255)] [StringLength(255)] [Required] public string AccountGroupName { get; set; } [ForeignKey("ParentAccountGroupId")] public int?

Entity Framework Core zero-or-one to zero-or-one relation

ぃ、小莉子 提交于 2019-12-05 23:16:43
Given these classes: public class A { public int Id {get; set;} public int? BId {get; set;} public B B {get; set;} } public class B { public int Id {get; set;} public int? AId {get; set;} public A A {get; set;} } Then with Fluent API modelBuilder.Entity<A>() .HasOne(a => a.B) .WithOne(b => b.A) .HasForeignKey<A>(a => a.BId); When creating objects and add them to database, things look like following in the corresponding tables: [A].BId is set [B].AId = null When I retrieve data using EF Core: A.B is set, A.BId is set B.A is set, but B.AId is null . What should I do to have B.AId set as well?

EF Core / Sqlite one-to-many relationship failing on Unique Index Constraint

断了今生、忘了曾经 提交于 2019-12-05 22:47:49
The context is each Car has a corresponding CarBrand . Now my classes are as shown below: public class Car { public int CarId { get; set; } public int CarBrandId { get; set; } public CarBrand CarBrand { get; set; } } public class CarBrand { public int CarBrandId { get; set; } public string Name { get; set; } } public class MyContext : DbContext { public DbSet<Car> Cars { get; set; } public DbSet<CarBrand> CarBrands { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlite(@"Data Source = MyDatabase.sqlite"); } } Here's a sample

Binding entity into WPF datagrid with EF 7 (core)

好久不见. 提交于 2019-12-05 21:21:11
I have a simple problem with data binding. I'm not able to get the datagrid DeviceDataGrid to visualise the entity information. The MainWindow.xaml file is as follows: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:domain="clr-namespace:FxEditorDatabaseStructure.Core.Domain" mc:Ignorable="d" x:Class="FxEditorDatabaseStructure.MainWindow" Title="MainWindow" Height="350" Width=