ASP .NET Boilerplate + MongoDb - asp.net

I am using ASP.Net boilerplate framework + SQL Server 2016 in my project. Recently I have faced a challenge with migration from SQL Server to MongoDB. I have found that it is possible with ASP .NET boilerplate and installed required NuGet packages, however, due to the lack of documentation the only thing I have managed to do is to define respective RepositoryBase class:
public abstract class MyRepositoryBase<TEntity, TPrimaryKey> : MongoDbRepositoryBase<TEntity, TPrimaryKey>
where TEntity : class, IEntity<TPrimaryKey>
{
protected MyRepositoryBase(IMongoDatabaseProvider databaseProvider)
: base(databaseProvider)
{
}
}
As far as I understand, first of all, I need to define connection string somewhere now. And then populate the database with required basic data(which previously had been done by EF Core migrations). Obviously, EF Core in the new approach is obsolete so does that mean for my DbContext class that it is obsolete as well?
Actually, there are plenty of questions in relation to ASP .NET boilerplate and MongoDB integration, therefore my current post is actually a request for provision of some kind of example of the existing integration. Thank you in advance.

You can register your module by depending on it on your web module.
[DependsOn(typeof(YourMongoDbModule))]
public class YourWebModule : AbpModule
{
}
I think you have to register the repository with:
IocManager.Register(typeof(IMongoRepository<>), typeof(MongoRepository<>), Abp.Dependency.DependencyLifeStyle.Singleton);
You can refer this sample.
Look at this comment also.
Here is a framework which maps EF Core to Mongo DB.

Related

How to add ApplicationInsights Logging Provider in Framework Console App Using Autofac

I am working on a .NET (full framework 4.7.1) console app that uses AutoFac for DI purposes.
We are in the process of migrating slowly to .NET Core, and have switched to using the ILogger abstractions provided by Microsoft.Extensions.Logging.Abstractions.
I have wired up ILogger<> and ILoggerFactory in AutoFac using the following
private static void RegisterLogging(ContainerBuilder builder)
{
builder.RegisterType<LoggerFactory>().As<ILoggerFactory>().SingleInstance();
builder.RegisterGeneric(typeof(Logger<>)).As(typeof(ILogger<>)).InstancePerDependency();
}
This depends on Microsoft.Extensions.Logging - and it seems to be working.
Now I want to wire up the Application Insights Logging provider, however all the documentation I can find only mentions how to add it to the .NET Core DI Container, and looking through the source code on various repos, I am a bit mystified on how to do it.
I figured that I might be able to do it like this:
builder.RegisterType<ApplicationInsightsLoggerProvider>().As<ILoggerProvider>();
But it depends on IOptions<TelemetryConfiguration> telemetryConfigurationOptions and IOptions<ApplicationInsightsLoggerOptions> applicationInsightsLoggerOptions neither of which I have.
Have anybody done this, or have suggestions on how to accomplish it?
I managed to get something going by doing it like this:
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging();
serviceCollection.AddApplicationInsightsTelemetryWorkerService();
builder.Populate(serviceCollection);
Not the very best solution, but I guess it does allow me to use the footwork of the ServiceCollection extensions methods, so I might have to live with that if nobdoy has a better answer

Modelling an existing database in .NET Core 2.x

I have an existing application. I am trying to port some pieces over from .NET 4.x over to .NET Core. I have created a context in my .NET Core app. I have create a db context via scaffold-dbcontext. I can run a basic query (hooray). Life is good. Now, I want to add in some async queries. I get the following error:
System.InvalidOperationException: 'The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.'
code:
var ctx = new GolfGameContext();
var userId = await (from u in ctx.AspNetUsers where u.Token == UserToken select u.Id).SingleAsync();
return userId;
This error seems strange. I have created some .NET Core 2.x apps from scratch and everything seems to work properly. I am able to do async queries with them just fine. When I look at the error link, I get taken to information about EF 6.x. I am guessing that there is something that the scaffold-dbcontext puts in the resulting models that cause this problem. I am also guessing that the code I have created in Core 2.x doesn't contain these same limitations. Am I on the right track? Do I need to change something in my context/models to get async to work properly? All thoughts are welcome.
TIA,
Wally

EF Code First DB gets dropped if used in different projects

I have an application which uses Entity Framework Code First models.
The structure of the project looks like this
Application.Models POCO objects
Application.EF EF Data Context and repositories
Application.Web.MVC The web application (the designer)
The context initializer looks like this:
public class DBContextInitializer : DropCreateDatabaseIfModelChanges<DBContext>
{
protected override void Seed(DBContext context)
{
}
}
Now, i have another API Application which gets the data from database as API calls. The project looks like this:
Application.Api.MVC The MVC4 API Project, containing DTO objects
Application.Models Same project
Application.EF Same project
Even if i don't change the POCO classes (inside Application.Models project), Entity Framework detects that the Metadata has changed, and tries to drop and re-create the database. If it does it, when i run the Designer Application (Application.Web.MVC), it drops it again, and so on.
I don't know why is it doing this. I am not changing the models.
Please ignore the question, it is all working. I was pointing to a different database.
Also, this was a good reason to learn about EF Migrations

Public Property has multiple definitions with identical signatures

I am writing an application to transfer data between an in-house web application and a Microsoft Dynamics CRM 2011 instance. I generated early bound entity classes using the CrmSvcUtil included with the Dynamics CRM SDK with the following parameters:
crmsvcutil /l:vb /url:http://nameofserver/MicrosoftDynamics/XRMServices/2011/Organization.svc /out:myorganizationcrmsdktypes.vb /username:username /password:password /domain:domain
I then renamed the existing standard sdk output file in my project and added the file generated by using the above command. Then I built the project and began receiving numerous errors like so:
Public Property account_activity_parties As System.Collections.Generic.IEnumerable(Of ActivityParty)' has multiple definitions with identical signatures.
Any ideas why?
Well, I figured it out...and I'll post the solution here just in case anyone else runs into it. I renamed my original early bound entity class myorganizationcrmsdktypes-old.vb and left it in the project. Apparently the project was building from both files, and this was causing the duplication.

Entity Framework 5 Code-First dropping/recreating database when app is restarted

I'm experimenting with the latest EF 5 CF (in VS2010, not VS2012). I'm generally following the MSDN EF 5.0 Quickstart: Creating a Model with Code First...
Rather than using a console app, my DbContext is in a Windows Service, which will eventually expose various data service methods by hosting a WCF Service (the client will be WPF MVVM)
In the OnStart of the Windows Service, I invoke SetInitializer, and then do a simple query to trigger initialization:
// Start the Windows service.
protected override void OnStart(string[] args)
{
Database.SetInitializer<MediaLibraryContext>(new MediaLibraryContextInitializer());
using (var context = new MediaLibraryContext())
{
var firstMedia = (from m in context.Medias select m).FirstOrDefault();
}
...
And EF CF creates the database from the model and seeds it, as expected.
But when I stop/restart the Service, EF appears to delete the database and recreate it (or perhaps it's just dropping the tables and recreating them?). All post-initialization changes I've made to the database are gone, and only the "seed" data is present.
I've worked with EF 4.1 and 4.3, but I've never seen this behavior. Any ideas where to look???
DadCat
EDIT: I found the problem just after posting this... the Quick start code has the database initialization strategy set to DropCreateDatabaseAlways.
That's what I get for copy/pasting code without carefully looking at it!
DC
I found the problem just after posting this... the Quick start code has the database initialization strategy set to DropCreateDatabaseAlways.

Resources