When I save changes to DbContext, it doesn't update my database. No errors either.
Yes, the incoming form data is filled. Yes, the connection string is correct, I know this because I'm able to retrieve data from the database perfectly fine. If it's of relevance, it's a many-to-many relationship.
A roadmap is like an article as far as you're concerned which can be associated with many tags.
public static class RoadmapService
{
static ConkerDbEntities dbContext;
public static void createDbContext(ConkerDbEntities _dbContext)
{
dbContext = _dbContext;
}
public static void addToDatabase(Form form)
{
Roadmaps roadmap = new Roadmaps { RoadmapTitle = form.title,
RoadmapSummary = form.summary,
RoadmapBody = form.body };
var tags = new Tags[form.tags.Length];
for(int i = 0; i < tags.Length; i++)
{
tags[i] = new Tags();
tags[i].TagId = form.tags[i];
}
var roadmapTags = new RoadmapTags[form.tags.Length];
for(int i = 0; i < tags.Length; i++)
{
roadmapTags[i] = new RoadmapTags{Roadmap = roadmap, Tag = tags[i]};
}
dbContext.AddRange(roadmapTags);
dbContext.SaveChangesAsync();
}
}
}
Where dbcontext is created, this is the constructor of Startup.cs
public static SearchEngine engine;
public static ConkerDbEntities dbContext;
public Startup(IConfiguration configuration)
{
Configuration = configuration;
dbContext = new ConkerDbEntities();
RoadmapService.createDbContext(dbContext);
engine = new SearchEngine(dbContext);
}
I get no errors, and there's nothing added to the database. I'm probably doing something fundamentally wrong here. Thanks in advance.
You are using async programming, but your method is not async.
use
dbContext.SaveChangesAsync();
when you have async methods, usedbContext.SaveChanges();
when is not async.Also, you are using static classes, this should not be the case if you are using dependency injection. DI will handle the life cycle of your objects.
Your class is not defined as should be, you have some format errors, and should look something like this:
Then in your controller you can use your service as is
I also would recommend to study about linq and how you can avoid those foreach cycles using select, check the default net core coding standards.
Edit
It is more about the readability and maintainability of your code, in the answer I already put a link to a post where is better explained, you are creating technical debt in your code, you are initializing arrays and filling them, handling indices and more... , but it reduces to this:
What is more easy to read? this:
or this
If you just started learning programming, you could ignore this until you are more confortable with the
for
,foreach
,while
and other control structures. This is structure programming, and it is a topic by itself.That is the magic of dependency injection, just let the system create the object for you, you just have to ask for the type.
This is also a big topic, you should check my previous link to the microsoft documentation, but basically, all you have to do is to define classes as dependencies, so, when you ask for one, the system itself check the dependencies, and the dependencies of that objects, until resolves all the tree of dependencies.
With this, if you need one more dependency latter in your class, you can add directly but you do not need to modify all the classes that uses that class.
To use this in the controller, please check the official docs, you just have to add you dependencies to the constructor, and win!, basically two parts:
Add in your Startup.class
Then in your controller: