entity-framework-core

Zero-or-one relationship in EF Core

て烟熏妆下的殇ゞ 提交于 2021-02-11 14:21:49
问题 I have a somewhat special scenario of a Zero-or-one relationship in EF Core which cannot be easily modeled by the default (as described, e.g.,in EF Core One to One or Zero Relationship). I've got multiple entities with a 0-1 relationship to a "Match" entity. Multiple instances can reference the same "Match" entity (which is why I can't put the foreign key into the "Match" table, which seems to be the recommended way of modeling a 0-1 relationship). How to define the relationship from one of

Zero-or-one relationship in EF Core

混江龙づ霸主 提交于 2021-02-11 14:20:09
问题 I have a somewhat special scenario of a Zero-or-one relationship in EF Core which cannot be easily modeled by the default (as described, e.g.,in EF Core One to One or Zero Relationship). I've got multiple entities with a 0-1 relationship to a "Match" entity. Multiple instances can reference the same "Match" entity (which is why I can't put the foreign key into the "Match" table, which seems to be the recommended way of modeling a 0-1 relationship). How to define the relationship from one of

MySQL with EntityFramework Core - Composite Foreign Key

China☆狼群 提交于 2021-02-11 14:19:30
问题 I have two tables Project and ProjectMedia . they are defined as follow: CREATE TABLE `project` ( `ID` int(11) NOT NULL, `LANG` char(2) NOT NULL, `NAME` varchar(64) CHARACTER SET utf8 DEFAULT NULL, `DESCS` varchar(2048) CHARACTER SET utf8 DEFAULT NULL, `SHORT_DESC` varchar(512) CHARACTER SET utf8 DEFAULT NULL, PRIMARY KEY (`ID`,`LANG`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; ID and LANG are a composite primary key in the Project table. ProjectMedia table definition: CREATE TABLE `project

How to use JsonObject of Pomelo.EntityFramework

风流意气都作罢 提交于 2021-02-11 14:15:11
问题 I want to store list of string into mysql table as json. I saw there is support for this in pomelo entityframework. I followed this https://libraries.io/github/tuanbs/Pomelo.EntityFrameworkCore.MySql Here is my entity public class Project { public int Id {get;set;} public string Title {get;set;} public JsonObject<List<string>> Tags {get;set;} } But when _context.Database.EnsureDeleted(); is called it gives below error Navigation property 'Tags' on entity type 'Project' is not virtual.

Entity Framework Core - problem with get relationship many to many

若如初见. 提交于 2021-02-11 13:05:51
问题 I have an issue with my Get method. In my BookRepository I have this method: public Book GetBook(int id) { return _context.Books .Include(a => a.BookAuthors) .Single(b => b.Id == id); } These are my 3 classes: public class Book { public int Id { get; set; } public string Title { get; set; } public virtual ICollection<BookAuthor> BookAuthors { get; set; } public string PublishingHouse { get; set; } public int NumberOfPages { get; set; } public int ISBN { get; set; } } public class Author {

Migrate Entity Framework 4.0 to Core

久未见 提交于 2021-02-11 13:00:27
问题 I have an old project developed with the Entity Framwork 4.0 which uses some complex types. Since I need to migrate it to .NET Core, I created a new project, installed all required libraries and used the command PM> Scaffold-DbContext "Server=ServerInstance; Database=DBName; Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models on the existing database in order to create the new models. The problem is that it does not generate the complex type classes. I can

EF Core with Cosmos DB provider, UserManager AddLoginAsync gives ConcurrencyFailure

岁酱吖の 提交于 2021-02-11 12:49:46
问题 Creating a new user, fetching the user again with usermanager for testing, and then using the method AddLoginAsync with the recently fetched user gives the error ConcurrencyFailure, Optimistic concurrency failure, object has been modified. When fetching the user the "ConcurrencyStamp" has the correct etag, but after the "AddLoginAsync" I can see the user object has an invalid etag, the ConcurrencyStamp is a GUID. I have followed the documentation and added this to the IdentityUser model

How to implement pagedList on DbQuery Entity in Dbcontext without fetching full data in .net core?

独自空忆成欢 提交于 2021-02-11 12:49:06
问题 I have to execute a sql string in .net core so first I added a Dbquery type entity in dbcontext. internal DbQuery<TempData> MyDataList{ get; set; } Then I fetched the data with my sql string as var data = context.MyDataList.FromSql(dataqueryString).ToList(); Iam getting the data correct but need to implement PagedList on this without fetching the whole data from database. I have tried like var data = context.MyDataList.FromSql(dataqueryString).ToPagedList(1,10) But this will fetch whole data

Sandbox Sqlite with EF Core

戏子无情 提交于 2021-02-11 12:29:20
问题 I'm making a small web app which currently is using sqlite + EF Core 2.1 for storing data. Are there any solutions that I can sandbox my sqlite database file ? I mean to load my sqlite database with its data and triggers from my physical db file into memory. Everytime when I turn on/off VS debugger, database will be refreshed with my existing data in physical file . Thank you, 回答1: Here's some code that literally does what you asked: var sandboxConnection = new SqliteConnection("Data Source=

Violation of PRIMARY KEY constraint 'PK_XY'. Cannot insert duplicate key in object 'dbo.XY'

自作多情 提交于 2021-02-11 08:23:44
问题 Using EF Core, I have a Zone that can have multiple Sol (soils), same Sol can be attached to multiple Zone : public class Sol { // ... public ICollection<Zone> Zones { get; set; } = new List<Zone>(); } public class Zone { // ... public ICollection<Sol> Sols { get; set; } = new List<Sol>(); } public override void Configure(EntityTypeBuilder<Zone> builder) { // ... builder .HasMany(p => p.Sols) .WithMany(p => p.Zones); } When adding my Sols to a Zone however I get the following exception: