We’ve all been victims of asking questions that are very straight forward. And eventually we get help or figure it out on own. In my opinion, whenever I ask those type of questions, I tend to feel embarrassed and keep to myself for the rest of class time. Through the readings from How to ask questions the smart way, we’ll always see “stupid questions” that are asked because we are all human beings that will usually seek for shortcuts. I am not saying that everyone asks stupid question, but there will always be a time where you won’t even think that your question is not that bright.
If everyone stopped and thought about how they would ask questions intelligently, I’d think that we’d probably get answers quickly and informatively. To show you an example of a question that has been asked the right way, we turn to StackOverflow, a Q & A website for all aspiring programmers.
For this example, I’ll show you how this is the right way to ask a question: Getting “The entity type model is not part of the model for the current context.”
This person is trying to figure out why he/she is getting the “The entity type model is not part of model error.”:
Q: Getting "The entity type model is not part of the model for the current context."
I am having this issue updating my database 1 column at a time in asp.net using web api. I am trying to query a PUT to just update one value in the row instead of updating that one and setting the rest to null. I made a separate model outside of the controller to take in the update so I could do one at a time. When I hit this line db.Entry(user).State = EntityState.Modified; in the controller that is where it is erroring out. Any advice how I can fix this?
This is my separate ViewModel I am taking in in the put method:
namespace WebAPI.Models.ViewModels
{
public class UserViewModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
This is my controller calling in the method with the ViewModel in my parameter:
public HttpResponseMessage PutUser(int id, UserViewModel user)
{
HttpResponseMessage response;
if (db.User.IsInRole("Admin"))
{
try
{
db.Entry(user).State = EntityState.Modified;
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(id))
{
response = new HttpResponseMessage(HttpStatusCode.NotFound);
return response;
}
else
{
throw;
}
}
response = new HttpResponseMessage(HttpStatusCode.NoContent);
return response;
}
This is my DBContext file:
public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<User> Users { get; set; }
}
}
By just looking at the question/title, I can tell that he/she knows what they are talking about. This person was straight forward on what the problem was and explained what his/her program is trying to do. Notice how this person knows where the error is coming from. This shows that he/she have been searching through out his/her code thoroughly. This person also included all the required code to be shown in his/her post. Which makes answering the question far more easier.
A: The error comes from how you initialize the data context db.
The user object has been created in a separate db, so, when you are trying to update user, the current db doesn't know about this user object.
You could solve it by getting a user:
try
{
// or check on FirstName and LastName if you don't have a user id
var updatedUser = db.Users.SingleOrDefault(x => x.id == id);
updatedUser.FirstName = user.FirstName;
updatedUser.LastName = user.LastName;
db.Entry(updatedUser).State = EntityState.Modified;
db.SaveChanges();
}
Alternatively, you could make sure that the data context you are using to create the user object is the same as the one that is trying to update the user.
Does this make sense to you?
The amount of information given from the asker will always reflect on the amount of information given from an answerer. Scrolling through the different answers provided, I noticed that they are given with sincereness and honesty. I also noticed the effort the asker gave to respond to most of the responses. Which can help engage with the problem and finding the solution
There are questions that can be answered by just doing research. But instead people need to create a post on StackOverflow about simple procedures. This type of question will always be a turn off for other users because they know that he/she is just being flat out lazy and waiting for someone to rescue them.
Here is an example of that: How do I export my private PGP key from the new Thunderbird?
How do I export my private PGP Key from the new Thunderbird Version? I created it when 72.2.1 was released and I want to backup it, but I have now idea how to export it. I am using MacOS.
Thank you!
Although this person is being nice, they need to know that there’s only one step away from answering your own question. Which is the INTERNET, do you part and just research it on your own. I’ll guarantee you that there is a step-by-step tutorial video on youtube that shows you exactly what to do.
Now I know sometimes we’ll ask random question, but wouldn’t it be better if you thought about what we’re gonna ask is worth the time and effort? Through my analysis of the article created by Eric Raymond, asking “smart questions” will effectively give answers in a well informed and understandable way. My lasting message: Everyone should do their part by giving the effort to “Think before you act.” This is a test