You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are trying to migrate NHibernate to EfCore 7 our huge banking project that contains 1500 entities and those mappings, consider allowing AsNoTracking or AsNoTrackingWithIdentityResolution via navigations, with this behaviour we could able to query only properties through configuration file.,
Consider following example is it possible, If we access Blogs through BlogOwner we dont want to track those instances. But if we access throug DbSet or any other entity that does not implies "AsNoTracking or AsNoTrackingWithIdentityResolution" must be trackable default now all navigation or collections are trackable.
Is it possible to handle this via IQueryExpressionInterceptor, in interceptor we need to find out any parent entity configurations that has AsNoTracking* or not? And could we apply AsNoTracking or AsNoTrackingWithIdentityResolution if its not already applied?
using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Metadata;using Microsoft.EntityFrameworkCore.Metadata.Builders;using System;using System.Collections.Generic;using System.Linq;namespaceSample{classProgram{staticvoidMain(string[]args){using(vardb=new BloggingContext()){
db.Database.EnsureCreated();
db.BlogOwners.ForEach(x=>x.Blogs.Add(new Blog {Url="http://blogs.msdn.com/adonet"}));varcount= db.SaveChanges();
Console.WriteLine("{0} records saved to database", count);}}}publicclassBloggingContext:DbContext{publicDbSet<BlogOwner> BlogOwners {get;set;}publicDbSet<Blog> Blogs {get;set;}publicDbSet<Post> Posts {get;set;}protectedoverridevoidOnConfiguring(DbContextOptionsBuilderoptionsBuilder){
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Sample;Trusted_Connection=True;");}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){
modelBuilder.Entity<BlogOwner>().HasOne(x=>x.Blog).WithMany().AsNoTracking();
modelBuilder.Entity<Blog>().HasMany(x=>x.Posts).WithOne(x=>x.Blog);}}publicstaticclassExtensions{privatestaticreadonlystringAsNoTracking="custom:asNoTracking";privatestaticreadonlystringAsNoTrackingWithIdentityResolution="custom:asNoTrackingWithIdentityResolution";publicstaticReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity>AsNoTracking<TPrincipalEntity,TDependentEntity>(thisReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity>builder)whereTPrincipalEntity:classwhereTDependentEntity:class{
builder.Metadata.PrincipalToDependent?.SetAnnotation(AsNoTracking ,true);returnbuilder;}publicstaticReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity>AsNoTrackingWithIdentityResolution<TPrincipalEntity,TDependentEntity>(thisReferenceCollectionBuilder<TPrincipalEntity,TDependentEntity>builder)whereTPrincipalEntity:classwhereTDependentEntity:class{
builder.Metadata.PrincipalToDependent?.SetAnnotation(AsNoTrackingWithIdentityResolution ,true);returnbuilder;}}publicclassBlogOwner{publicintOwnerId{get;set;}publicintBlogId{get;set;}publicstringOwnerEmail{get;set;}publicBlogBlog{get;set;}}publicclassBlog{publicintBlogId{get;set;}publicstringUrl{get;set;}publicList<Post> Posts {get;set;}}publicclassPost{publicintPostId{get;set;}publicstringTitle{get;set;}publicstringContent{get;set;}publicintBlogId{get;set;}publicBlogBlog{get;set;}}}
The text was updated successfully, but these errors were encountered:
@gokhanabatay I don't really understand what you are trying to do. Is the intention to get a connected graph of entities but prevent updates to some entities in the graph? If so, that's tracked by issue #29719, but it's unlikely to be implemented by not tracking those entities.
@ajcvickers our intention is to migrate ef core but keep our business logic as same as possible. With NHibernate we can define relation readonly/query only in some graph.
Let me explain another example:
Student -> Has Many Childrens -> Has One Country, Country is query only/readonly reference property in this graph.
Country -> Has Many City, Country is not query only/readonly in this graph.
@ajcvickers our intention is to migrate ef core but keep our business logic as same as possible. With NHibernate we can define relation readonly/query only in some graph.
Let me explain another example:
Student -> Has Many Childrens -> Has One Country, Country is query only/readonly reference property in this graph.
Country -> Has Many City, Country is not query only/readonly in this graph.
We are trying to migrate NHibernate to EfCore 7 our huge banking project that contains 1500 entities and those mappings, consider allowing AsNoTracking or AsNoTrackingWithIdentityResolution via navigations, with this behaviour we could able to query only properties through configuration file.,
Consider following example is it possible, If we access Blogs through BlogOwner we dont want to track those instances. But if we access throug DbSet or any other entity that does not implies "AsNoTracking or AsNoTrackingWithIdentityResolution" must be trackable default now all navigation or collections are trackable.
Is it possible to handle this via IQueryExpressionInterceptor, in interceptor we need to find out any parent entity configurations that has AsNoTracking* or not? And could we apply AsNoTracking or AsNoTrackingWithIdentityResolution if its not already applied?
The text was updated successfully, but these errors were encountered: