So, adapting some of the code from the tutorial here:
I have got a twilio app up and running and it correctly posts and inserts calls into my sql server, although I am trying to add additional fields to be inserted and I ran into some issues. In my controller, this is the method i am using:
[HttpPost]
public TwiMLResult Create(
[Bind(Include = "QuestionId,RecordingUrl,Digits,CallSid,From")]
Answer answer)
{
_answersRepository.Create(answer);
var nextQuestion = new
QuestionFinder(_questionsRepository).FindNext(answer.QuestionId);
return TwiML(nextQuestion != null ? new Response(nextQuestion).Build() :
ExitResponse);
}
My question is two parts. First, how can I add more fields to be added to my sql table? I tried adding StartTime, EndTime, Duration after ,From and adding it to the Model, synced my database so I can see the columns on the table but nothing actually get's inserted. I put the data types as string so maybe that was the issue? I could not tell from the twilio documentation what datatype those fields wer.
The second part of the question is can I put custom fields into that Bind(Include) statement? For instance, can I create a variable called Name and then have the TwiMLResult Create send a string Name along with the twilio data with it as well? Of course, I would add it to the model class and thus to the table.
I guess my problem is is that I don't understand what is happening with the TwiMLResult Create method very well. What is happening exactly? This method here is what is actually RECEIVING the data coming from twilio, correct? and the _answersRepository.Create is what writes it to the database? So I should be able to add more fields and have them written to the db just fine I would think. I just am not sure why the StartTime, EndTime, Duration information isn't coming through to this point. Similarly, I am not sure how to add a custom variable, for instance, to pass the person's name to this point and have it written to the db.
I hope this all makes sense and isn't too convoluted to understand. Thank you in advance everyone! I really appreciate the help!
Syd
EDIT: Basically, I determined that those fields do not come through and thus were passing null values. To answer the second part of my question, you can definitely pass any query values and record them (which is what I ended up doing).
Related
I have an entity/model defined int C#. There's an in Id on it. Nothing special. This is so standard I am almost certain I'm going to get flagged duplicate for this but I honestly cannot find another question about it.
I have a controller that expects an object with the properties of the entity.
public IActionResult Submit(MyModel model)
I'm basically just passing this in be saved by EF Core.
But... I get an error about not liking the Id being null before it even makes it inside the controller action. I know I can instantiate these and save them typically and they don't have an Id up front, under what circumstance would it complain about it being null?
id: ["Error converting value {null} to type 'System.Int32'. Path 'id', line 1, position 258."]
Right so about 9/10 of the way through this question (and way too much time head-scratching) I figured out exactly what it was. Leaving here in case anyone runs into the problem in the future - mostly likely me in about a month. Hello, future me.
There's no problem instantiating the entity for saving without supplying an Id. The issue was passing literally id: null in the post data, and the server trying to convert the null to an int.
I'm using Azure Mobile Services and am running into an issue with the "UpdateAsync" method. For many of the properties I am storing in my item the UpdateAsync method works just fine. For others the update is completely ignored. Has anyone ran into an issue like this before? The call I am making is nothing fancy and the code is below.
It seems that the issue of the property not being updated may be limited to properties that contain a number in the name. For example CP20M is one of my properties that is not updating through this method. Another property, "Weight" updates without issue. Both are doubles. Does that make any sense? The only way I have of updating these fields that seems to work is to delete the entry and insert a new entry. That will get the appropriate values into all properties.
Any ideas are appreciated.
public async Task UpdateUserProfileItemAsync(UserProfile userProfile)
{
await _userProfileTable.UpdateAsync(userProfile);
await SyncAsync();
}
Just rename the field you are having problem with. It worked for me.
This is the example what I had done on my program.
Currently I had set my database for accepting the datetime Allow NULL and also default value as NULL.
The datetime i set it to nullable like this:
System.DateTime? EntryTime
Then here is my code in MODEL, I do it in the model so that every function who need it can use it.
UserLogin newItems = db.MyItems.Create();
newItems.Id = id;
//newItems.EntryTime = null;
db.UserLogins.Add(newItems);
db.SaveChanges();
Even I didnt commented it, it still goes into the same error result.
when I come to update the data into database it give me "System.Data.Entity.Validation.DbEntityValidationResult" error.
I totally out of idea. Can someone help me?
EDIT 1 I made a mistake on variable.
The only given error is System.Data.Entity.Validation.DbEntityValidationResult and no detailed information given, that's why I am totally out of idea.
Most ORMs won't care about the default value on the table; they care about the object - and the object has a value (a null in this case), whether or not you specify it explicitly. So typically, it will dutifully pass down the null as part of the insert, and the default value will not be used. There may be ways of overriding this in some ORMs, but personally I wouldn't count on it.
Have you tried inserting the data with insert statement?
INSERT INTO `table` (id) VALUES ('id_values');
But I guess you'll have to create a custom module for this. With my MVC application, I use the service-repository approach. Entity Framework is so frustrating.
This question already has answers here:
How to update only one field using Entity Framework?
(17 answers)
Closed 8 years ago.
Hey guys I am curious in how I can update just one column at a time in a database. When I run something such as postman or fiddle to query a Put to my database. If I only include one field, it sets all of the other fields = to null. Is there anyway I would be able to leave the other fields blank when I query the PUT and it will only change the one field I am asking the PUT to update? Sorry if my explanation is not good I am new to using API's.
Here is my PUT method (basic scaffold):
public IHttpActionResult PutUser(int id, User user)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != user.Id)
{
return BadRequest();
}
db.Entry(user).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
When I want to update, say just the first name, and there is already a first and last name in the database. After the PUT method the last name would be set to null. So I debugged and noticed that the user it is taking in in the parameter already has last name set to null. Any advice would be greatly appreciated!
I'm guessing that you are sending a blank user with just information in the column that you want to update. Well what you should do is read the user details first, then change the column/field as required, then send this new user object back to the API Put method
Edit
Basically you can't really specify a single column/field to update using this API pattern. Look at your constructor, there's no parameter to tell the code which column you want to update. (You could test for nulls, but I would strongly advise against it). Your code is telling EntityFramework to update the database with the User object that you provide.
Therefore you need to first retrieve the user that you want to update (using a GET), and only change the field that you want to change, then send the updated User object back to the API Put method. If you send a User object with NULL values, then these NULL values will be saved to the database.
If you really want to be able to specify columns, you can write separate PUT method overloads that will only update the column you want. However programming this way requires more effort and more maintenance, and I would advise against it.
I'm writing stored procedures in C# at the moment and I've run into a problem with the size of a parameter.
So I've created a project in VS 2008 and created several stored procedures which all look a bit like this:
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void SaveProgress(... SqlString logpart, ...)
{
...stuff...
}
}
Now because I've not specified anything else, when I deploy this to a database, the CREATE DATABASE statement (apparently) gets created with a nvarchar(4000) as the definition for the input parameter.
However, I regularly have to flush log parts larger than 4000 chars, so I'd like that to be nvarchar(MAX).
Now I think I can do some jiggery-pokery and use Management Studio to re-define the CREATE DATABASE statment, but I'd actually like to define the fact that I want it to be MAX in the project/solution, so the deployment gets done correctly and I don't have to start adding large wads of comments and/or documentation for anyone who needs to maintain this code after me.
Is there any way to specify this in the code or maybe in the AssemblyInfo or something like that?
Revisiting this years later, I tried to use SqlChars in a function that read data from the database and returned a formatted string with data in it. Using SqlChars actually made the function bomb, stating that it could not find linked server System - an error message that seems to have nothing to do with the problem, as I was never referencing a linked server in the first place.
Changing the return type and parameters back to SqlString, adding [return:SqlFacet(MaxSize = -1)] attribute to the function, and adding [SqlFacet(MaxSize = -1)] to each parameter made my function work properly.
Try using SqlChars. SqlChars automatically maps to NVARCHAR(MAX)