-
Notifications
You must be signed in to change notification settings - Fork 3.3k
what is the best way to add our own namespace while generating the model via Scaffolding #30332
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
Comments
@archanasoni Typically, the appropriate namespaces are added based on the types in the generated code. What is the case where this is not happening? |
@ajcvickers, UseDb2 and IBMDBServerType.LUW require namespace IBM.EntityFrameworkCore protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseDb2("Connection string",
p => p.SetServerInfo(IBMDBServerType.LUW));
} another example is: protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<NET7TAB1>(entity =>
{
entity.HasKey(e => e.ID)
.ForDb2IsClustered(false);
});
} |
@archanasoni the UseDB2 method should be on an extension class in the Microsoft.EntityFrameworkCore, much like how UseSQLServer is. This way the method "lights up" for users automatically, without them having to explicit use an IBM-specific namespace. |
Thanks @roji from the beginning we have this method and others in IBM specific namespace. For some reasons we do not wish to change.. Could you help how we can add IBM Specific namespace during the model generation ? |
Since scaffolding switched to using templates in EF 7.0, there's no longer an easy way for a provider to add arbitrary namespaces; users can easily modify the templates in any way they want (that was the point of this feature), but providers cannot. Note that CSharpDbContextGenerator was internal in 6.0 and below; you should really try to avoid accessing/overriding internal EF APIs so that this kind of breakage doesn't happen. If you finding yourself needing to do something with an internal API, you should signal this to us so we can provide guidance on an alternative implementation, or possibly change things within EF in order to accommodate your needs. Concretely, we may want to add a a template parameter allowing extra namespaces to be passed; CSharpModelGenerator is still internal, so that still wouldn't be sufficient as a 1st-class way for a provider to inject additional namespaces. /cc @bricelam @ajcvickers In the meantime, if you don't want to change the namespace of your extension methods, I think you'll have to instruct your users to edit their templates to add the namespace, as per the docs. |
Thanks @roji but this is a breaking change for us. We implemented internal class by going through some blog from MS but unfortunately, we are unable to find that blog now. |
@archanasoni we'll discuss this internally and post back here. FWIW note that there's an analyzer that generates warnings whenever you use an internal EF API (e.g. anything in Internal namespaces), precisely to avoid people accidentally using it. I hope you're seeing those warnings and taking them into account. |
If you return a MethodCallCodeFragment with a |
Thanks @bricelam , we had string version that we've changed to MethodInfo now but still we don't see namespace is being generated.. //here MethoInfo is exactly same what we have for SQL Server
private static readonly MethodInfo UseDb2MethodInfo
= typeof(Db2DbContextOptionsExtensions).GetRuntimeMethod(
nameof(Db2DbContextOptionsExtensions.UseDb2),
new[] { typeof(DbContextOptionsBuilder), typeof(string), typeof(Action<Db2DbContextOptionsBuilder>) })!;
public override MethodCallCodeFragment GenerateUseProvider(
string connectionString,
MethodCallCodeFragment? providerOptions)
=> new (UseDb2MethodInfo,
providerOptions == null
? new object[] { connectionString }
: new object[] { connectionString, new NestedClosureCodeFragment("x", providerOptions) }); |
@bricelam , @ajcvickers May we know status of this issue ? Will it be fixed in .NET8 ? |
@archanasoni Unfortunately, this issue has been postponed and will not ship in EF Core 8. |
@ajcvickers it's really important for us to have fix for this.. is there a way which we can implement and fix it by our own in .NET 8 now ? |
@archanasoni The best thing to do is use the recommended pattern for scaffolding like the other database providers do. Anything else, even this fix, is just a band aid over the real issue. |
Fixed for 8.0.0-rc2. Using the MethodInfo overloads should now generate the usings. |
it's fixed in version 8.0.0 Thanks |
Hi,
We've requierment to add using statement :
using IBM.EntityFrameworkCore
Above statement has to be added while generating the Context class via scaffolding. Till .NET 6 we implemented CSharpDbContextGenerator and added in our service, but from .NET7 we see it is part of .tt file which we are not much familior..
how can we achieve the same result now ?
The text was updated successfully, but these errors were encountered: