Quantcast
Channel: CodeProject Latest postings for ASP.NET
Viewing all articles
Browse latest Browse all 3938

.Net Core 2.1, seeding a MongoDB, foreach on Countries,

$
0
0
It's been pretty rough trying to figure out how to write the MongoDB context and some sort of MongoInitializer that runs from Programs.cs while passing the settings to load the MongoDB parameters. I'm seeding in .Net Core and not Angular V6 because it just seems like the right place to do it.
So I've seeded my default users, countries, and now states / provinces. I decided to copy the format of my previous versions here, but was forced to really change it up MongoDB style. I'm trying to loop through the countries I just wrote, to create states.

So this is what I wrote based off research from the internet. Don't know if it's right or wrong and it's very foreign to me. I'm in uncharted waters here on this. I understand this:
var websiteCountries = _database.GetCollection<WEBSITE_COUNTRIES>("Website_Countries");
but not the cursor part:
using (IAsyncCursor<WEBSITE_COUNTRIES> cursor = await websiteCountries.FindAsync(new BsonDocument()))
So the above code must set the cursor position; perhaps it set it at the end, and that's why the batch is null.

I'm not even sure if I'm on the right track here. I really don't care if it's async or not.
I'll read up on this part here await websiteCountries.FindAsync(new BsonDocument())
publicstaticasync Task SeedAsync(IMongoDatabase _database)
{var websiteStates = _database.GetCollection<WEBSITE_STATES>("Website_States");// Check and see if we have any website usersdouble totalDocuments = websiteStates.CountDocuments(FilterDefinition<WEBSITE_STATES>.Empty);if (totalDocuments == 0)
    {var websiteCountries = _database.GetCollection<WEBSITE_COUNTRIES>("Website_Countries");try
        {using (IAsyncCursor<WEBSITE_COUNTRIES> cursor = await websiteCountries.FindAsync(new BsonDocument()))
            {
                IEnumerable<WEBSITE_COUNTRIES> batch = cursor.Current; // batch is null hereforeach (WEBSITE_COUNTRIES country in batch)
                {
                    Console.WriteLine(country.LongName);
                }
            }
        }catch(Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }                
    }            
}

I wrote this, well sort of copied it, havn't tested it yet. This is in my UserRespository and according to documentation is a CRUD method of getting all the users. What's sort of confusing is the context. I can generate a context here in the repository, but had to use a manual connection in my Seed. Perhaps this is the answer to my problem. FindSync vs FindAsync(_ => true).ToListAsync(). Why true?
publicclass UserRepository : IUserRepository
{privatereadonly MongoDBContext _context = null;public UserRepository(IOptions<Settings> settings)
    {
        _context = new MongoDBContext(settings);
    }////////////////////////////////////////////////////////////publicasync Task<IEnumerable<WEBSITE_USERS>> GetAllUsers()
{try
    {returnawait _context.WebsiteUsers
                .FindSync(_ =>true).ToListAsync();
    }catch (Exception ex)
    {// log or manage the exceptionthrow ex;
    }
}

My MongoDBInitializer where I wasn't able to generate a context. This isn't that important but it did send me down a different road and how I ended up here on this path.
publicstaticvoid SeedData(Settings settings)
{
    IMongoDatabase _database = null;var client = new MongoClient(settings.MongoDB.Connection);if (client != null)
        _database = client.GetDatabase(settings.MongoDB.Database);// Seed the Databases
    WebsiteUsers.Seed(_database);var countryTask = WebsiteCountries.Seed(_database);var statesTask = WebsiteStates.SeedAsync(_database);

}
If it ain't broke don't fix it
Discover my world at jkirkerx.com

Viewing all articles
Browse latest Browse all 3938

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>