I want to retrieve data from my sql server. in the table, contained 1 column with date format.
I have succeed retrieve the value for the other column it that table. However, the time_stamp column value is missing.
Is there anyone have this issue before? I dont know why the value for this column is not displayed
here is the detail:
model
public Nullable<System.DateTime> time_stamp { get; set; }
view
<td>
#Html.DisplayFor(modelItem => item.time_stamp)
</td>
controller
model.feedback = entities.FEEDBACKS.ToList();
I think the problem related to the data format, might be in the model or view.
Because the other column is display correctly.
I have solved this issue. It is because I use the connection created from ADO connection to edmx. And I add the time_stamp manually. So the connection is not updated.
Solution --> rebuild the edmx connection.
Related
This was working with EF5 but there seems to be an issue with EF6.
We are trying to save a new row to a table using EF6. The primary key column is marked as an Identity value and we have an insert trigger on the table.
When we call db.saveChanges() it always tries to use 0 as the primary key column value.
If we insert a record through Toad it uses the trigger correctly. So the trigger is working.
If we change the trigger to check for 0 instead of null then it also works.
Has anybody else had this issue with EF6?
Thanks,
Joe
With 12c it works fine
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Number { get; set; }
I know this issue has arisen many times on Stack Overflow. I've looked at them, but believe this to be unique:
I'm not using .Single() or .SingleOrDefault() in any LINQ expressions
I checked my models for duplicates, like "Id" and "ID", there are none
This happens onload of any Kendo UI MVC grids, on any table, on any page in my website that has such a grid, and we have several different pages, each with their own grids
Just some history, we just changed some foreign keys to Guids from IDs (ints) in order to point them at different tables in the database than they are currently in the code. Models were updated, accordingly. We also added a view for the Kendo grid to be pointed at, and this was changed in the view. But even grids that have nothing to do with the original or new SQL view/C# models seem to have been affected. Now, during this phase:
public OurDatabase(bool enableLazyLoading=true)
: base("name=OurDBContext")
{
Database.SetInitializer<OurDatabase>(null);
((IObjectContextAdapter)this).ObjectContext.ContextOptions.ProxyCreationEnabled = enableLazyLoading;
((IObjectContextAdapter)this).ObjectContext.ContextOptions.LazyLoadingEnabled = enableLazyLoading;
}
It keeps giving the error "Sequence contains more than one matching element". It will highlight on that middle line with that error.
Can something changing in the database cause this? Code that once worked, and hasn't been changed since that time, now does not, and this is very confusing.
I found out the answers to my problem. Yes, plural.
First, if I commented out the public virtual DbSet<blah> blah { get; set; } in the database entity, and everything that had anything to do with its model, the old code would work again. So that told me the issues were in the model. I also thought I could name "blah" whatever I wanted. I found it should be named after the DB or view name, instead, so I updated that, accordingly.
Second, I found out I had some major issues with my models, which I had used some decorations on with little knowledge behind them. Ex.
[Key]
[Column(name:"GUID", order:2)]
public Guid Guid
It was #2 because ID was #1, and if I put ID in just like above, I got a green squiggly under ID saying 'MyProject.Entities' hides inherited member 'MyProject.Entities.PersistedEntity<int>.Id'. To make the current member override that implementation, add the override keyword. Otherwise add the new keyword.
That was because of the PersistedEntity<int> inherited class that had Id and Name in it, already, that a team member had added. I commented that out and declared the ID and Name columns (which also had the green squiggly until commenting out that class) the normal way.
Then I got an error on the composite keys, saying it could not order them (gee, I wonder why). So these are how they look, now:
[Key]
[Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int ID { get; set;}
[Key]
[Column(Order = 1)]
public Guid GUID { get; set;}
[Key]
[Column(Order = 2)]
[StringLength(60)]
public string Name { get; set;}
Which set the ordering and composite key correctly, starting at 0 instead of 1 that I had previously. I had other columns, too, so I continued with setting the [Key] and [Column(Order = x)] attributes on those, too.
Third, I also added a decoration above the class, calling it a table, even though this was for a view (weird):
[Table("My.View")]
Keeping things commented out that relates to adding new models in the controllers seems to be the way to go to troubleshoot this lousy, non-descript error that for me said nothing about the real, underlying issues - at least in this instance. I also didn't think ALL of my grids would stop working just because I added a model - even ones that were not pointed at the new model or the one it was replacing.
After I got the models with the right annotations, I was able to uncomment out the DbSets in my main database model, and because my team member used "Id", when I re-generated the model, I needed to update the ID the grid was using in the Kendo view:
.DataSource(dataSource => dataSource
.Ajax()
.Model
{
.model.Id(p => p.ID)
})
Also, note, I was only able to get it fully working after upgrading to Entity Framework 6.1. Before then, I was getting "Invalid object name 'dbo.My.View'". Apparently in previous versions of EF, you can't just add a model for a table/view and expect it to find it.
I need to add dynamic controls in MVC that I have done through view but I am facing problem in inserting records. My query is there are pre-defined questions based on that answertext boxes will be generating. So each question should display corresponding to its answer.
If there are 20 questions then at run time it will generate 20 answer text boxes which means multiple records are going at once. As far as I know I will do it through DataTable please correct me if I am wrong. But how would I send data through view I am able to perform get request but unable to send multiple records and repository file as well.
Following is my schema
tblAnswer
AnswerID identity column primary key,
AnswerText varchar(500)
QuestionId foreign key
tblQuestion
QuestionId primary key identity column
QuestionText
for instance:-
#foreach(var ques in Models.questions)//questions is a list of questions
{
//enter code here
#ques.QuestionText ///will display question text
#Html.TextboxFor(model=>model.AnswerText)
<br/>
}
If i take list above then it will force me to take model[i], AnswerText then I need to pass list though I am passing list still not able to perform.
Model
public int AnswerId,
public string selectedAnswer,
public int AnswerText,
public int QuestionId,
public list<Question> questions
Selected answer is in radio button there would be 10 radio button on the screen which is also associated with other table. And questiontext does not exist in Answer table
I would handle this by creating a custom ViewModel that contained a string for the question and a string for the answer entered, then return a collection of these to bind to your view.
I have an MVC application using Code First data migrations and now, long after making a table, I'd like to change the PK column to not be an auto-generated Identity column. But I get the impression I just can't do this without having CodeFirst delete the table somehow (which will be hard given all the dependencies) and recreate it? Really wishing I didn't have to do that.
What I've tried:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int AssessmentID { get; set; }
and this in OnModelCreating():
modelBuilder.Entity<Assessment>()
.Property(e => e.AssessmentID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
When doing add-migration, it doesn't add anything. I tried manually adding this line but it does not update the db:
AlterColumn("dbo.Assessment", "AssessmentID", c => c.Int(nullable: false, identity:false));
I've concluded that this is indeed not possible. Like the post below says, there is no SQL Alter command to change the Identity status on a column, so CodeFirst can't create one. I plan to remove all migration files and rescaffold (things have gotten messy in other ways).
Remove Identity from a column in a table
I am trying to create a partial view to display some data. My controller takes in a string itemID and performs a query based on that. When the partial view is called, it displays the same record over and over again.
Note: I changed the name of objects for security purposes.
[ChildActionOnly]
public ActionResult someAction(string itemID = "")
{
//Empty itemID
if(string.IsNullOrEmpty(itemID))
{
return RedirectToAction("Index", "Search");
}
var model = _db.someTable
.Where(r => r.itemID == itemID)
.OrderBy(r => r.col1)
.ThenBy(r => r.col2)
.Take(20);
return PartialView("_myView", model);
}
I have tried removing the OrderBy and ThenBy methods, but the result remain the same, (Order would not matter since they are duplicates...). When I remove the .Where method, it works as expected and displays 20 different records (though, not filtered by any means).
My view was created by Visual Studio using the List template. The view been proven working by removing the .Where method from the LINQ statement. Here are the important bits of the view:
#model IEnumerable<MyApp.Models.OperationData>
.
.
.
#foreach (var item in Model) {
<tr>
<td>
#Html.DisplayFor(modelItem => item.some column)
</td>
.
.
.
Any ideas as to what is wrong with this query?
EDIT: Tried the other LINQ syntax and ended up with the same results:
var model = from r in _db.someTable where r.itemID == itemID select r;
Winner winner chicken dinner!
Turns out the issue was with the mapping of model to table. The table I was working on has a composite key, which I didn't know about... After reading mipe34's bit about primary keys, I decided to do some investigation into the table structure. After discovering the composite keys, I added the mapping for the 2nd key and all works well.
This issue was extremely confusing since the SQL generated by LINQ worked perfectly fine when run in SQL Management Studio.
Thanks all!
Hit the breakpoint just after the model variable and see what SQL query is generated by LINQ - VS should display it for you. You can also try to execute the query (add .ToList() at the end of the query) to see the actual result, what is in the collection to distinguish if there is a problem in query or view.