entity-framework-core

EF Core Analyzer RawSqlStringInjectionDiagnosticAnalyzer error

南笙酒味 提交于 2021-01-29 21:25:03
问题 I am trying to port everything from Entity Framework 6.3 to Entity Framework Core 2.1.2 and this is my first experience with EF Core. I have 2 projects one is my core or infrastructure project with all the entity models, DB Context and repositories and the other one is an asp.Net MVC which is my startup project and using all the services from the core project. Both projects are using .NetStandard Library 2.0.3 and targeted .Net Framework 4.6.2. And I have .Net Core SDK 2.2 on my machine. The

Joining two tables in EF Core, invalid column name

流过昼夜 提交于 2021-01-29 17:59:46
问题 I am trying to join two tables in EntityFramework Core using linq syntax in the following way: var results = dc.EntityA.Join(dc.EntityB, a => a.StringProperty, b => b.StringProperty, (a, b) => DoSomeStuff(a, b)).ToList(); Where StringProperty is a string column in the database. I am getting the error {"Invalid column name 'EntityId'."} I found this similar question Entity Framework: Invalid column name 'OrganizationStructure_ID' where it is suggested to set the foreignkeys during

How to remove multiple cascade paths in entity framework

让人想犯罪 __ 提交于 2021-01-29 17:22:57
问题 I am trying to define relationships between tables, but getting the following error: Introducing FOREIGN KEY constraint 'FK_QProducts_Quotes_QuoteId' on table 'QProducts' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.Could not create constraint or index. See previous errors. This is how my tables look like: What I am trying to achieve: A. Client creates an inquiry and adds products and quantities that he

DBContext object LINQ Sum(time) column to hours

三世轮回 提交于 2021-01-29 16:53:10
问题 This is an ASP.Net Core Web API Project, using EntityFrameWorkCore. I need to convert TotalLoggedInTime (format: 139:05:40) to Hours and Sum the hours and assign it to logonHours, it fails on that line. The first line for callsOffered works fine. C# Code: internal void Hydrate(DbSet<MyObj> myObj) { string callsOffered = myObj.Sum(a => a.CallsPresented).ToString(); string logonHours = myObj.Sum(a => TimeSpan.Parse(a.TotalLoggedInTime).TotalHours).ToString(); } SQL Table SELECT total_logged_in

How to Create Migration on Runtime in Entity Framework Core

送分小仙女□ 提交于 2021-01-29 16:07:08
问题 Is there any possible way to create migration on runtime in efcore. context.Database.Migrate(); Before this code, I need a code that, when I started my app, should create a migration about diff between postgresql database tables and ef models. Is there any way to do this? PM> enable-migrations PM> add-migration initial PM> update-database I dont want to use these ones. I want to make these code's jobs on runtime. I hope I could explain myself clearly. 回答1: Note, you need to re-compile the app

What is required for EF Core's IMethodCallTranslator to work with `EF.Functions` to provide a custom function?

天涯浪子 提交于 2021-01-29 13:28:59
问题 I'm trying to implement a custom IMethodCallTranslator in EF Core 3.1 with the Sqlite provider. I have created: An extension method off of this DbFunctions which is called at query time An implementation of IMethodCallTranslator which is Translate not called A derived RelationalMethodCallTranslatorProvider which I'm passing an instance of my IMethodCallTranslator . This constructor is hit. I also tried overriding RelationalMethodCallTranslatorProvider.Translate and this was not hit. An

How to configure a foreign key on an owned EF entity's property?

不打扰是莪最后的温柔 提交于 2021-01-29 12:54:31
问题 EF Core 3.0 I have the following (simplified) entities in my domain: class ApplicationUser { public int Id { get; set; } public string UserName { get; set; } // other properties } [Owned] class Stamp { public string Username { get; set; } public ApplicationUser User { get; set; } DateTime DateTime { get; set; } } class Activity { public Stamp Created { get; set; } public Stamp Modified { get; set; } // other properties } It's not particularly relevant, but it's worth mentioning that

Create database context from cookie and base path in Entity Framework Core

流过昼夜 提交于 2021-01-29 11:16:54
问题 Postgres database has multiple schemes like company1, company2, ... companyN Browser sends cookie containing scheme name . Data access operations should occur in this scheme. Web application user can select different scheme. In this case different cookie value is set. Npgsql EF Core Data provider is used. ASP NET MVC 5 Core application registers factory in StartUp.cs : public void ConfigureServices(IServiceCollection services) { services.AddHttpContextAccessor(); services.AddScoped

Setting up a relationship in Entity Framework Core

China☆狼群 提交于 2021-01-29 09:55:04
问题 I'm trying to set up a one to many relationship in Entity Framework Core using the Fluent API with no success. I have two objects called Message and Source and are defined as public class Message { public int ID { get; set; } public int FromUserID { get; set; } public int ToUserID { get; set; } public int SourceID { get; set; } public int Priority { get; set; } public string Subject { get; set; } public string MessageBody { get; set; } public DateTime DateTimeCreated { get; set; } public

How can I recursively retrieve a hierarchy of records using EF Core?

只愿长相守 提交于 2021-01-29 08:33:17
问题 I am using EF Core 2.1. I have a Group object that has the following properties: int GroupId int? ParentGroupId Group ParentGroup The object references the GroupId of its parent using the ParentGroupId property. The depth of the hierarchy is not known when querying. How can I retrieve the entire hierarchy? I've tried the following, which will get me three levels deep, but how can I get all levels of the hierarchy without knowing the depth? Do I need to rely on a stored proc? var group = await