Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable non-tracking for specific Entity #29719

Closed
Deivisson opened this issue Nov 30, 2022 · 4 comments
Closed

Enable non-tracking for specific Entity #29719

Deivisson opened this issue Nov 30, 2022 · 4 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@Deivisson
Copy link

Hi everyone!

I have a situation where I have an Entity that can do only ReadOnly queries against the database table... To avoid passing in all queries .AsNoTracking(), I'd like to know if is possible to configure that entity to never be tracked?

EF Core version: 6
Database provider: (e.g. Npgsql.EntityFrameworkCore.PostgreSQL)
Target framework: (e.g. .NET 6.0)
Operating system: Linux
IDE: (VsCode)

@ErikEJ
Copy link
Contributor

ErikEJ commented Nov 30, 2022

Make it Keyless?

@Deivisson
Copy link
Author

@ErikEJ thank you for answering me!!

But, If make it keyless, I lose the option to retrieve the record by its ID, using the Find methods.

My entity

...
builder.ToTable("customers", "shared", t => t.ExcludeFromMigrations()).HasNoKey();
...

Generic method to check if the entity is Active

public virtual bool ActiveEntity<TCustomEntity, TKey>(TKey key)
      where TCustomEntity : class, IEntity
      where TKey : notnull
    {
      var entity = GetDbContext().Set<TCustomEntity>().Find(key);
      var property = entity?.GetType().GetProperty("Status");
      var value = property?.GetValue(entity);
      if (value == null) return false;
      return (short)value == StatusGeneric.Active.Id;
    }

   // checking if the entities are Activity
  bool fooIsActive = .ActiveEntity<FooReadOnlyEntity, long?>(1));
  bool barIsActive = .ActiveEntity<BarReadOnlyEntity, long?>(999));

Below is the error when set entity to keyless

"exception": {
        "ClassName": "System.NullReferenceException",
        "Message": "Object reference not set to an instance of an object.",
        "Data": null,
        "InnerException": null,
        "HelpURL": null,
        "StackTraceString": "   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.FindTracked(Object[] keyValues, IReadOnlyList`1& keyProperties)\n   at Microsoft.EntityFrameworkCore.Internal.EntityFinder`1.Find(Object[] keyValues)\n   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Find(Object[] keyValues)\n   at Linear.Infrastructure.Common.BaseRepository`1.ActiveEntity[TCustomEntity,TKey](TKey key) in /code/src/submodules/infrastructure/Common/BaseRepository.cs:line 118\n   at

@roji
Copy link
Member

roji commented Nov 30, 2022

But, If make it keyless, I lose the option to retrieve the record by its ID, using the Find methods.

There's no particular reason to use Find if you're not going to involve change tracking (which doesn't make sense if the entities are read-only). You can simply query by the ID using Single(x => x.Id == "something") (but without having EF treat that property as a key).

@ajcvickers
Copy link
Member

Notes from triage: Read-only entities are tracked by #7586. We don't plan to make it possible to mark entities as no-tracking.

@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Dec 5, 2022
@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Dec 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

4 participants