entity-framework

Bulk Insert Optimization in .NET / EF Core

冷暖自知 提交于 2021-01-29 03:27:58
问题 I have some .NET Core code that does some bulk loading of the DB with random sample data. I'm getting 20 inserts/second on localhost, and looking to improve my performance. I'm doing the basic stuff like calling _dbContext.SaveChanges() only once, etc. A number of posts like this indicate gains can be had by manipulating properties on the DbContext's Configuration, such as Configuration.AutoDetectChangesEnabled and Configuration.ValidateOnSaveEnabled . My .NET Core MVC app's DbContext is a

Best way to control unicity of an attribute using EF Code First

吃可爱长大的小学妹 提交于 2021-01-29 03:24:54
问题 I've got a class User which has an attribute Name that must be unique. So far I've investigated 3 ways of checking this: Annotations [StringLength(100)] [Index(IsUnique = true)] public string Name { get; set; } Problem is, by trying to insert a user with a repeated name it throws this ex: as you can see, I would have to navigate into the inner exceptions (which I don´t know if it is possible, but I assume it is) and the last inner exception´s message is not user friendly at all. Fluent Api

EF Core one-way self referencing entity binding

匆匆过客 提交于 2021-01-29 02:28:22
问题 I have set up a self referential entity using EF Core which looks like this: Entity public class DetailType { public int DetailTypeId { get; set; } public string Name { get; set; } public int? ParentTypeId { get; set; } public DetailType ParentType { get; set; } public IEnumerable<DetailType> ChildTypes { get; set; } } Binding modelBuilder.Entity<DetailType>() .ToTable("detailtype") .HasOne(x => x.ParentType) .WithMany(x => x.ChildTypes) .HasForeignKey(x => x.ParentTypeId); I am retrieving

Extending Entity Framework Model at application runtime

懵懂的女人 提交于 2021-01-29 00:40:55
问题 For a business application, I am providing a base entity model. Thereafter the end user should be able to extend the model for his specific needs. For the base model I want to use database-first approach. But I don't know how to accommodate for allowing user to extend it. One part is to provide a UI for entity model editing and the other is to reflect the changes in the model and database thereafter. Please offer suggestions. EDIT : - Once the entity model is edited and saved, all EF

Extending Entity Framework Model at application runtime

限于喜欢 提交于 2021-01-29 00:36:47
问题 For a business application, I am providing a base entity model. Thereafter the end user should be able to extend the model for his specific needs. For the base model I want to use database-first approach. But I don't know how to accommodate for allowing user to extend it. One part is to provide a UI for entity model editing and the other is to reflect the changes in the model and database thereafter. Please offer suggestions. EDIT : - Once the entity model is edited and saved, all EF

The SqlParameterCollection only accepts non-null Parameter type objects

对着背影说爱祢 提交于 2021-01-28 21:36:10
问题 I am trying to use stored procedures in Entity Framework Core. When executing a stored procedure, I am passing two input parameters and one output parameter. I keep getting this error: The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects. This is my code: public string InsertCardsData(DateTime RecordingStartDate, DateTime RecordingEndDate) { try { // DateTime DRecEndDate = Convert.ToDateTime(RecordingEndDate); DateTime DRecEndDate =

AspNet Core Autofac disposing my DbContext even if its registered as SingleInstance

痞子三分冷 提交于 2021-01-28 21:18:55
问题 In our application, we have an api which will store some data to a database. We are using Entity Framework Core 3.1.1 . After this entity has been stored, a message is posted to Azure Servicebus and a consumer will read this message a store the message to another table in the same DbContext . As far as I understand, a LifetimeScope is defined per request to the api. The LifetimeScope will start when the api request hits the ApiController and finishes when the endpoint is done processing the

Entity Framework query a comma separated field

混江龙づ霸主 提交于 2021-01-28 21:02:48
问题 I have a table called Article: -Id -Title -Content -Tags (This is a comma seperated list of tags e.g. 'Sports,Italy,Ferrari') Using entity framework i want to be able to find all articles that have certain tags. List<Article> GetArticles(List<String> tags) { //do an entity framework query here to find all articles which have the tags specified } The returned entries should contain all the tags specified. e.g. If the input to the function was 'car','blue', then all entries with these at least

Override serial sequence in PostgreSql with Entity Framework (C#)

走远了吗. 提交于 2021-01-28 20:18:48
问题 How do I override the serial/sequence in an ID field when adding records with Entity Framework on a PostgreSQL database? The serial/sequence auto incrementing ID works fine, when adding records, but I need to write a migration script where the old records I migrate must maintain their old ID's. It works fine if I just shoot an INSERT query directly to the server, but when doing with with Entity Framework, they just start from 1, 2, 3 etc. 回答1: You should to reset sequence in the database

How to apply Entity Framework code first approach to a project

陌路散爱 提交于 2021-01-28 19:54:47
问题 I use Visual Studio 2013 and need to use Entity Framework code first approach in a project. I used NuGet Manager to install EF v6.1 into my project. But I can't create an empty code first model or even a code first model from database although there are two other choices (DB first and Model first). How I can add Entity Framework code first feature to my project? 回答1: Here is a beginners tutorial for using entity framework code first: An Absolute Beginner's Tutorial for understanding Entity