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

EntityTypeBuilder.ToFunction vs ModelBuilder.HasDbFunction inconsistencies in inheritance mapping? #33408

Closed
Schlurcher opened this issue Mar 26, 2024 · 2 comments

Comments

@Schlurcher
Copy link

Schlurcher commented Mar 26, 2024

If I define a a TVF on the context it is usable and handles inheritance and materialization correctly.

modelBuilder.HasDbFunction(typeof(AdmsContext).GetMethod(nameof(UserFileSystem))!)
	.HasName("fn_filesystem")
	.HasSchema("dbo");
var data = await Context.UserFileSystem()
	.Take(100)
	.ToListAsync();

grafik

If I try to map the function in an EntityTypeConfiguration it immediatly throws and complains that derived types cannot be mapped to functions. Am I missing something in my EntityTypeConfiguration? Mapping to derived types clearly works in the first scenario, why would it be different when directly mapping an EntityType / DbSet.

System.InvalidOperationException
The entity type 'FileSystemArchiveVirtual' is mapped to the 'DbFunction' named 'fn_filesystem', but is derived from 'FileSystemObject'. Derived entity types cannot be mapped to a function.
public class FileSystemObjectConfiguration : IEntityTypeConfiguration<FileSystemObject>
{
	public void Configure(EntityTypeBuilder<FileSystemObject> builder)
	{
		builder.ToFunction("fn_filesystem");

		builder.HasKey(x => x.Path);

		builder.Property(x => x.ObjectId);
		
		builder.HasDiscriminator(x => x.ObjectTypeId)
			.HasValue<FileSystemCompany>(FileSystemObjectType.Company)
			.HasValue<FileSystemArchiveVirtual>(FileSystemObjectType.ArchiveVirtual)
			.HasValue<FileSystemDossier>(FileSystemObjectType.Dossier)
			.HasValue<FileSystemFile>(FileSystemObjectType.File);
		
		builder.HasOne<FileSystemObject>(x => x.Parent)
			.WithMany(x => x.Children)
			.HasForeignKey(x => x.ParentPath);

		builder.Property(x => x.ParentPath).ValueGeneratedOnUpdateSometimes();
	}
}
@roji
Copy link
Member

roji commented Mar 26, 2024

modelBuilder.HasDbFunction(typeof(AdmsContext).GetMethod(nameof(UserFileSystem))!)
	.HasName("fn_filesystem")
	.HasSchema("dbo");

var data = await Context.UserFileSystem()
	.Take(100)
	.ToListAsync();

Here you simply seem to be invoking the UserFileSystem() function, as a plain .NET function; whatever its .NET implementation is will get executed; you should be able to remove HasDbFunction: that's about allowing the function in question to be used inside LINQ queries. In other words, your TVF function here really being "mapped" at all (or at least, you're not using it as such).

@AndriySvyryd
Copy link
Member

Duplicate of #19970

@AndriySvyryd AndriySvyryd marked this as a duplicate of #19970 Apr 26, 2024
@AndriySvyryd AndriySvyryd closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2024
@AndriySvyryd AndriySvyryd removed their assignment Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants