Click here to Skip to main content
15,888,733 members
Home / Discussions / ASP.NET
   

ASP.NET

 
Rant[REPOST] post list of products instead of single product in post method odata Pin
Richard Deeming29-Aug-18 2:58
mveRichard Deeming29-Aug-18 2:58 
QuestionSub Level Hyperlink not showing in asp.net Pin
Member 1115418828-Aug-18 19:13
Member 1115418828-Aug-18 19:13 
AnswerRe: Sub Level Hyperlink not showing in asp.net Pin
dan!sh 28-Aug-18 20:55
professional dan!sh 28-Aug-18 20:55 
GeneralRe: Sub Level Hyperlink not showing in asp.net Pin
Member 1115418828-Aug-18 21:04
Member 1115418828-Aug-18 21:04 
GeneralRe: Sub Level Hyperlink not showing in asp.net Pin
dan!sh 28-Aug-18 21:09
professional dan!sh 28-Aug-18 21:09 
QuestionHow to publish more than one webapps from visual studio in single click Pin
Dominic Louis27-Aug-18 20:12
Dominic Louis27-Aug-18 20:12 
Question.Net Core 2.1, MongoDB, add states collection to country states collection Pin
jkirkerx24-Aug-18 12:31
professionaljkirkerx24-Aug-18 12:31 
Question.Net Core 2.1, seeding a MongoDB, foreach on Countries, Pin
jkirkerx24-Aug-18 9:47
professionaljkirkerx24-Aug-18 9:47 
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())
public static async Task SeedAsync(IMongoDatabase _database)
{
    var websiteStates = _database.GetCollection<WEBSITE_STATES>("Website_States");

    // Check and see if we have any website users
    double 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 here
                foreach (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?
public class UserRepository : IUserRepository
{
    private readonly MongoDBContext _context = null;
    public UserRepository(IOptions<Settings> settings)
    {
        _context = new MongoDBContext(settings);
    }

////////////////////////////////////////////////////////////

public async Task<IEnumerable<WEBSITE_USERS>> GetAllUsers()
{
    try
    {
        return await _context.WebsiteUsers
                .FindSync(_ => true).ToListAsync();
    }
    catch (Exception ex)
    {
        // log or manage the exception
        throw 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.
public static void 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

AnswerRe: .Net Core 2.1, seeding a MongoDB, foreach on Countries, [sort of solved] Pin
jkirkerx24-Aug-18 10:25
professionaljkirkerx24-Aug-18 10:25 
General[solved] Pin
jkirkerx27-Aug-18 7:59
professionaljkirkerx27-Aug-18 7:59 
QuestionAccess HttpContext in ASP.NET Core Pin
JimFeng22-Aug-18 6:35
JimFeng22-Aug-18 6:35 
AnswerRe: Access HttpContext in ASP.NET Core Pin
Vincent Maverick Durano22-Aug-18 8:40
professionalVincent Maverick Durano22-Aug-18 8:40 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng23-Aug-18 5:07
JimFeng23-Aug-18 5:07 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Vincent Maverick Durano23-Aug-18 7:42
professionalVincent Maverick Durano23-Aug-18 7:42 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng23-Aug-18 8:39
JimFeng23-Aug-18 8:39 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Vincent Maverick Durano23-Aug-18 10:28
professionalVincent Maverick Durano23-Aug-18 10:28 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming23-Aug-18 9:14
mveRichard Deeming23-Aug-18 9:14 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng24-Aug-18 9:21
JimFeng24-Aug-18 9:21 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 2:02
mveRichard Deeming28-Aug-18 2:02 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng28-Aug-18 7:09
JimFeng28-Aug-18 7:09 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 7:16
mveRichard Deeming28-Aug-18 7:16 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng28-Aug-18 7:46
JimFeng28-Aug-18 7:46 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 7:56
mveRichard Deeming28-Aug-18 7:56 
GeneralRe: Access HttpContext in ASP.NET Core Pin
JimFeng28-Aug-18 7:57
JimFeng28-Aug-18 7:57 
GeneralRe: Access HttpContext in ASP.NET Core Pin
Richard Deeming28-Aug-18 8:12
mveRichard Deeming28-Aug-18 8:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.