entity-framework-core

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

為{幸葍}努か 提交于 2019-12-07 14:40:51
问题 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

dotnet exec needs a managed .dll or .exe extension while adding Entity Framework Core (1.1.0) Migrations

大城市里の小女人 提交于 2019-12-07 14:37:24
问题 Error Message: PM> Add-Migration InitialDatabase dotnet exec needs a managed .dll or .exe extension. The application specified was 'C:\Users\xxxxxx\documents\visual studio 2017\Projects\TheWorld\src\TheWorld\bin\Debug\netcoreapp1.0\TheWorld.runtimeconfig.json' Process finished with non-zero exit code PM> Visual Studio Version: 2017 RC Project Dependencies : Error Screenshot : 回答1: I had the same problem. The only thing I had to do is changing the Target Framework in the Project properties.

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

爷,独闯天下 提交于 2019-12-07 14:10:29
问题 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

How to implement Select For Update in EF Core

北慕城南 提交于 2019-12-07 13:18:39
问题 As far as I've understood it, there is no option in EF (and EF Core) to explicitly lock resources which I'm querying, but I'll need this functionality quite often and don't really feel like falling back to writing select statements every time I'll need it. Since I only need it for postgres and according to the spec FOR UPDATE is the last item in the query, the easiest I thought about implementing it was to get the select statement as described here: In Linq to Entities can you convert an

Binding entity into WPF datagrid with EF 7 (core)

我怕爱的太早我们不能终老 提交于 2019-12-07 12:08:33
问题 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"

Entity Framework Core, DELETE CASCADE, and [Required]

这一生的挚爱 提交于 2019-12-07 11:58:24
问题 I am running into an issue DELETE CASCADE in Entity Framework Core that I can't seem to find a good solution to. Here's a super simplified version of my model: User {UserID, Name} Recipe {RecipeID, UserID} Ingredient {IngredientID, UserID} RecipeIngredient {RecipeID, IngredientID} *RecipeIngredient is the many-to-many table. Recipe and Ingredient both have UserID marked as [Required], and RecipeIngredient has RecipeID and IngredientID marked as [Required]. The issue is that SQL will not

EF 7: How to load related entities in a One-to-many relationship

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-07 11:12:53
问题 I have the following code. Why are my navigation properties (Requirement in Course, and Courses in Requirement) are null? public class Course : AbsEntity { [Key] public string Title { get; set; } public string Term { get; set; } public int Year { get; set; } public string CourseId { get; set; } public double GradePercent { get; set; } public string GradeLetter { get; set; } public string Status { get; set; } public int ReqId { get; set; } public Requirement Requirement { get; set; } } public

EF Core 2.0 TransactionScope Error

流过昼夜 提交于 2019-12-07 10:31:32
问题 I am trying to use TransactionScope in my SELECT query in EntityFramework Core 2.0. However I am getting this error : "Enlisting in Ambient transactions is not supported." The idea is to implement "NO LOCK" option (I know it's not a good idea to have that option in place but it's vendor's requirement) when I do select query. So I added an extension method (Entity Framework with NOLOCK) public static async Task<List<T>> ToListReadUncommittedAsync<T>(this IQueryable<T> query) { using (var scope

Workaround needed for EF Core performing GroupBy operations in memory instead of in SQL

有些话、适合烂在心里 提交于 2019-12-07 09:08:57
问题 I'm working in Entity Framework Core 1.1.0 (and upgrading is not an option at this point, due to breaking changes in later versions). My query is of the following form: var q = db.MyTable .GroupBy(t => new { t.Field1 }) .Select(g => new { g.Key.Field1, MaxField2 = g.Max(x => x.Field2) }) .ToList(); In test code this works well and returns the expected data. But when deployed to a real environment, with real data, it times out. Why? Well, I put a sniffer on the SQL server, and here's the

Entity Framework Core InMemory database tests break when run in parallel

拈花ヽ惹草 提交于 2019-12-07 08:17:04
问题 When running all the test, I'm eventually getting errors saying: "An item with the same key has already been added. Key: 125" This doesn't happen when each test is run separately. The funny thing is that each test works with a different DbName, to avoid any conflict: [TestMethod] public void Test1() { using (var context = CreateTestingContext()) { ... } } [TestMethod] public void Test2() { using (var context = CreateTestingContext()) { ... } } protected static SGDTPContext