AbpDbContext and IdentityDbContext - c#

I have an ASP.NET Core (v. 2.2) application that uses IdentityDbContext<User, Role, string>.
I'm adding Abp Framework to the application, I want to use AbpDbContext.
How to combine the functionality of two contexts?

You need to create a module class like below
public class BlogModule : AbpModule
{
}
and add your other module dependency in the attribute. Something like this
[DependsOn(typeof(BlogModule))]
public class WebAppModule : AbpModule
{
}
And read https://docs.abp.io/en/abp/latest/Module-Development-Basics

Related

How to add DbContext In Startup.cs without using Entity Framework?

I am currently working on a project where I am developing a class library that later on will be uploaded as a nugget package, such that if a user creates a.NET Core application, she/he can download the nugget package and use it accordingly.
Essentially within the class library, Entity Framework, Nethereum and other packages are installed as dependencies. One of my goals is not to require users to add Entity Framework to their application (since the nugget package (, i.e. the class library I am building)) already has it installed. For that reason, there is a DbContext that accepts the database connection string in the class library and builds the options.
public class BEFDbContext: DbContext
{
public BEFDbContext(string connectionString) :
base(SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), connectionString).Options) { }
public DbSet<ApplicationEvent> Events { get; set; }
}
Next, the user has to create another class in the application code that extends the BEFDbContext class found in the class library.
public class NewDatabaseContext: BEFDbContext
{
public NewDatabaseContext(string connectionString):base(connectionString){}
}
So far so good, however, at this point, I would like to 'initialise' the NewDatabaseContext class in the Startup.cs class. Generally, one would use Entity Framework and would add the code as such:
services.AddDbContextPool<NewDatabaseContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("defaultconnection"));
});
However, as I mentioned before One of the goals is to not require users/developers to add Entity Framework to the application (once again since we have it in the class library).
So, my question is How I can add the NewDatabaseContext class as DbCcontext in the Startup.cs without using Entity Framework?
Since you wanted the alternative response you can use Extension methods
in your library add the following code
public static class ServiceCollectionExtensions
{
public IServiceCollection AddApplicationDbContext<T>(this IServiceCollection services, IConfiguration configuration) where T : BEFDbContext
{
services.AddDbContextPool<T>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("defaultconnection"));
});
return services;
}
}
then in the startup of application you can use
public void ConfigureServices(IServiceCollection services)
{
...
services.AddApplicationDbContext<NewDatabaseContext>(Configuration);
...
}
You can have variations of this as per your need. Like accepting the connection string instead of the whole Configuration, etc.
This answer uses generics and extension methods. If you want more details then please checkout:
Generic methods: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-methods
Extension Methods: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/extension-methods

.Net Core with .Net Framework Configuration

How to set the Configuration Manager AppSetting into legacy .Net Framework dependency.
The WebApi in .Net Core but for the data access have to use the legacy .Net Framework. Then Base library structure is
public class OracleRepository : BaseRepository
{
public OracleRepository() : base(ConnectionString)
{
}
}
public abstract class BaseRepository
{
private string _connectionString;
public BaseRepository(string connectionString)
{
ConnectionString = ConfigurationManager.AppSettings[connectionString];
}
}
You can certainly keep the repository layer in the .net framework instead of using Core.
Instead of using the ConfigurationManager.AppSettings, I would suggest to inject the required settings in form of either KeyValuePair collection or Dictionary.
Then you can initialize this KeyValuePair collection or Dictionary in the startup method.

Entity Framework Core - Customise Scaffolding

In Entity Framework 6 we can add the T4 templates the scaffolding uses by running
Install-Package EntityFramework.CodeTemplates.CSharp
But in Entity Framework Core the scaffolding system does not appear to use T4 templates, nor does it seem like the scaffolding can be customised. It seems to be all in c# classes eg.
https://github.com/aspnet/EntityFramework/blob/a508f37cf5a0246e9b92d05429153c3d817ad5ec/src/Microsoft.EntityFrameworkCore.Tools.Core/Scaffolding/Internal/EntityTypeWriter.cs
Is there any way to customise the output from the scaffold?
There is a special, yet-to-be-documented hook to override design-time services:
class Startup
{
public static void ConfigureDesignTimeServices(IServiceCollection services)
=> services.AddSingleton<EntityTypeWriter, MyEntityTypeWriter>();
}
Then implement your custom generator.
class MyEntityTypeWriter : EntityTypeWriter
{
public EntityTypeWriter(CSharpUtilities cSharpUtilities)
: base(cSharpUtilities)
{
}
// TODO: Override with custom implementation
}
Update: See Yehuda Goldenberg's answer for another way to do this in EF Core 1.0.2+.
In .Net Core 1.1, the way to override design-time services is to implement the IDesignTimeServices interface in the startup assembly. For example:
public class MyDesignTimeServices : IDesignTimeServices
{
public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
{
serviceCollection.AddSingleton<EntityTypeWriter, MyEntityTypeWriter>();
serviceCollection.AddSingleton<DbContextWriter, MybContextWriter>();
}
}
See https://github.com/aspnet/EntityFramework/issues/5617

ASP.NET 5 RC1 Entity Framework 7 - custom identity issues

I'm trying to create my custom identity implementation to use int values for my primary keys insted of default strings in ASP.NET 5 RC1 with EF7.
I have my custom User and Role classes as well as UserStore, UserManager, RoleStore, RoleManager. For now I have issues with UserStore being Unable to resolve service for type 'DAL.BgCarsDbContext' while attempting to activate 'DAL.Identity.UserStore'.
My code for User and UserStore classes:
public class User : IdentityUser<int>
{
}
public class UserStore : UserStore<User, Role, BgCarsDbContext, int>
{
public UserStore(BgCarsDbContext context, IdentityErrorDescriber describer = null)
: base(context, describer)
{
}
}
I know that's happening because of the default UserStore behaviour.
However, I would like to use the default implementation that UserStore provides because it implements a lot of interfaces
IUserLoginStore<TUser>, IUserStore<TUser>, IDisposable, IUserRoleStore<TUser>, IUserClaimStore<TUser>, IUserPasswordStore<TUser>, IUserSecurityStampStore<TUser>, IUserEmailStore<TUser>, IUserLockoutStore<TUser>, IUserPhoneNumberStore<TUser>, IQueryableUserStore<TUser>, IUserTwoFactorStore<TUser>
and I'd like to avoid having to write the implementation of every method from the interfaces. So is there any way to avoid that?

extending aspnet identity to include additional properties for WEB FORMS

i am currently implementing aspnet identity(code first) for WEB FORMS(VS 2013) i would like to know how to add additional properties like date of birth to the entity.
i can currently do this via MVC but have issues on aspnet WEBFORMS.
You should create a class which inherits from IdentityUser and add your custom properties to it. An example would look like the following
public class ApplicationUser : IdentityUser
{
public DateTime DateOfBirth { get; set; }
}
The next step would be to change your database context to use this new user
public ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
...
}
If that doesn't solve your problem you should post what your issues actually are.

Categories