I agree with @Ivan, I wouldn't recommend you this way, but you answered that you need to, so here we go.
You're using EFCore right ? Luckily, EFCore is open-source, so we can dig into the source code and build custom EFCore versions.
A few months ago I had also a specific need with EF Context scaffolding, we also have over 200 tables and needed to put mappings for each table in a separate class, because EF Core, defaults to put all the mapping stuff in the DbContext
file and this generated a 10k+ lines of code long DbContext
class for us ??.
EntityTypes
generation is handled here. Interesting line for you is #109 :
_sb.AppendLine($"public partial class {entityType.Name}");
Here you could just add your Interface this way :
_sb.AppendLine($"public partial class {entityType.Name} : IEntity");
Then we have to implement your interface, on line #113 we have the following code :
using (_sb.Indent())
{
GenerateConstructor(entityType);
GenerateProperties(entityType);
GenerateNavigationProperties(entityType);
}
Just before GenerateProperties(entityType);
you can add the following lines to implement interface specification :
_sb.AppendLine("[NotMapped]");
_sb.AppendLine("public int Id { get => PropertyId; set => PropertyId = value; }");
_sb.AppendLine("");
If your really need/want partial classes you could simply write some code to generate another file in the WriteCode
method which is called once per table and has all the info needed for this (type name, etc.)
Here is the code for building the project with your custom implementation. After scaffolding your project you can just return to a official EFCore build.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…