efcore | EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, upda | Object-Relational Mapping library
kandi X-RAY | efcore Summary
kandi X-RAY | efcore Summary
EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of efcore
efcore Key Features
efcore Examples and Code Snippets
Community Discussions
Trending Discussions on efcore
QUESTION
I have class library called Persistence.EFCore contains this method
...ANSWER
Answered 2022-Mar-21 at 21:38The first parameter of ConfigureService((context, services)=>{ ... }) is HostBuilderContext which gives you access to configuration, using its Configuration property.
So assuming you have a service extension method which accepts configuration, then you can easily pass configuration to it using context.Configuration
.
Example
QUESTION
I'm using efcore and I scaffolded from an existing database, but I forgot to make an initial migration when I did that so it picked up my new model. I decided that I wanted to keep them separate so I removed the migration, deleted the snapshot, and now every time I create another migration, it still has the new model in it.
On top of this, using dotnet ef migrations list
will list pending migrations that no longer exist. If I do dotnet ef database update
they will get 'migrated` and show up in __MigrationHistory, but again, they don't actually exist in the directory.
I've tried creating the files again, going as far as adding the class names to the files, doing a dotnet ef migrations remove
, but I still have the same issue. It will delete the files, tell me everything is rolled back, but then the files will still be listed as pending, the database snapshot will still contain the new table (that doesn't exist) if I do any future migrations.
Not really sure how to proceed. Everything I've read says that if I delete the snapshot and migrations, it will create a fresh snapshot, but it doesn't seem to do that. Is this stuff cached somewhere locally that I can clear out?
...ANSWER
Answered 2022-Mar-07 at 14:58Of course after posting this, I figured it out.
My issue was that I kept running dotnet ef
commands with the --no-build
flag because they would fail. I know why they were failing, it wasn't a big deal, but apparently this prevented the snapshot from updating as well.
So I fixed it by taking the offending files out of my folder for the moment, running dotnet ef migrations list
, and with a successful build, it fixed all of the issues I was having.
QUESTION
To visualize - naively - if I would like to complete such an operation, I would probably use the code below.
Assume that dbContext
and dbContext.Entries
are of type DbContext
(PostgreSQL) and DbSet
respectively.
ANSWER
Answered 2022-Feb-28 at 01:14If entity framework tries to modify or delete a record that is missing or had been recently updated, it will throw a DbUpdateConcurrencyException
.
DbUpdateConcurrencyException
Exception thrown by DbContext when it was expected that SaveChanges for an entity would result in a database update but in fact no rows in the database were affected. This usually indicates that the database has been concurrently updated such that a concurrency token that was expected to match did not actually match
Hence you could wrap your code in a try catch and catch that exception.
QUESTION
So I'm running into a rather odd issue here. I've got a PostgreSQL 14 server and I'm using the Npgsql Entity Framework Core provider to access it. If I have an Entity Framework Core entity that contains a date as a NodaTime Instant
type (which becomes timestamp with time zone
in the database) and try to project that via .InZone(timeZone).LocalDateTime
, I run into 3 situations.
Actual example query would be something like this:
...ANSWER
Answered 2022-Feb-25 at 21:53The NodaTime plugin doesn't support passing parameterized time zones from .NET; you can only use a constant expression such as DateTimeZoneProviders.Tzdb["Europe/Berlin"]
(see these docs for supported translations). You can use this to convert a PG timestamp with time zone
into a timestamp without time zone
in PG, and then retrieve the result as a timestamp without time zone
, which can be read as a NodaTime LocalDateTime.
However, if what you're looking for is to get a ZonedDateTime/LocalDateTime with the time zone of the client machine (where .NET is executing), then doing .InUtc().LocalDateTime
is the way to do that - any reason you're trying to avoid it?
A bit more context (as well as a full code sample) could shed some more light on what you're trying to achieve.
QUESTION
I'm trying to create a docker-compose script to fire a stack with a PostgreSql database and an ASP.NET Core 6 Web API. To test the scenario, I've created a new ASP.NET Core 6 Web API using the default template. I have then added NuGet package Npgsql
(6.0.3), and a sample controller which just queries the DB engine version, e.g.:
ANSWER
Answered 2022-Feb-22 at 16:23When a container connects to another container on the bridge network, you use the container port. Not the mapped port on the host. So your connection string should be
QUESTION
I came across this error when modifying a DB first project (using fluent migrator) and scaffolding the EF context to generate models. I have reproduced it by making a code-first simplification. This means that I can't accept answers that suggest modifying the annotations or fluent configuration, because this will be deleted and recreated on the next migration and scaffold.
The simplified idea is that a device has:
- many attributes
- many histories representing changes to the device over time
- each history entry has an optional location
IOW you can move a device around to locations (or no location) and keep track of that over time.
The code-first model I came up with to simulate this is as follows:
...ANSWER
Answered 2021-Nov-10 at 06:20Update: The bug is fixed in EF Core 6.0, so the next applies to EF Core 5.0 only.
Looks like you have hit EF Core 5.0 query translation bug, so I would suggest to seek/report it to EF Core GitHub issue tracker.
From what I can tell, it's caused by "pushing down" the root query as subquery because of the Take
operator (which is basically what First
method is using in the second case). This somehow messes up the generated subquery aliases and leads to invalid SQL.
It can be seen by comparing the generated SQL for the first query
QUESTION
I have the following configuration for which I want to provide some seed values.
...ANSWER
Answered 2022-Jan-01 at 16:42This is very similar to How to fix EF Core migration error on join table configuration, but for owned entity type.
Be careful with fluent API overloads, as they return different things. For instance, OwnsOne
without builder action delegate returns a builder for the owned entity type. But the overload with action returns the original (owner) entity builder so you can continue configuring it (the configuration of the owned entity is considered to be inside the action delegate).
In your case, here
QUESTION
The below LINQ query works perfectly fine in EF6 world where the whole query seems to be evaluated at the server (checked with SQL Profiler) but fails in EFCore6.
...ANSWER
Answered 2022-Feb-11 at 08:20Consider to rewrite your query until this bug is fixed
QUESTION
I get on creating Migration some Warnings like this one:
The foreign key property 'AppUserClaim.UserId1' was created in shadow state because a conflicting property with the simple name 'UserId' exists in the entity type, but is either not mapped, is already used for another relationship, or is incompatible with the associated primary key type. See https://aka.ms/efcore-relationships for information on mapping relationships in EF Core.
It applies to all entities with AppUser navigation property. Other navigation properties has no warning.
...ANSWER
Answered 2022-Jan-24 at 15:49I ran into a similar issue. In my OnModelCreating
method, I had flipped the order in which I was applying migrations.
QUESTION
I juste upgraded my web api to .net6 using EFCore and NPGSQL (Postgres).
I had to Set a Switch for my code to work:
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
Now EFCore Creates my DateTime fields to 'timestamp with time zone' which breaks everything.
How can I get the old 'timestamp without time zone' for DateTime Types?
Should I remove the LegacyTimestamp switch?
Thanks for your help!
...ANSWER
Answered 2022-Jan-05 at 09:02I Fixed it by forcing column type for all DateTime and DateTime? to "timestamp without time zone" by adding the following code in the OnModelCreating of my DBContext.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install efcore
Most people use EF Core by installing pre-build NuGet packages, as shown above. Alternately, the code can be built and packages can be created directly on your development machine.
The latest stable version is available on NuGet. Use the --version option to specify a preview version to install.
Most people use Microsoft.Data.Sqlite by installing pre-build NuGet packages, as shown above. Alternately, the code can be built and packages can be created directly on your development machine.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page