I'm having an odd little problem with MVC2. I am perorming some CRUD tasks, and I can Create records in the database just fine. But when I go to update a record with the following code nothing seems to happen to the database:
EntityModelConnection entityModelConnection = new EntityModelConnection ();
try {
OrderLogic orderLogic= new OrderLogic ();
EntityObject_Orders orderToUpdate = OrderLogic.GetOrderByID(1);
orderToUpdate.Name = "Laptop";
EntityModelConnection.SaveChanges(); }
My code isn't using names like these, they are just renamed for clarity :)
When I run over this code, no errors are thrown and the correct single record is returned from OrderLogic.GetOrdersByID, but nothing seems to update in the database.
It's a bit hard to find guides on this since most people are using UpdateModel in the controller and I'm doing my logic far from that location, so I can't (seem) to use that feature.
Any thoughts on what I might be missing? Is there something strange about MVC when it comes to updating records I might be missing?
I'm going for a coffee to refresh my brain :)
MVC or no MVC should have no effect on your data layer. I suspect that your order isn't being updated because it either isn't marked as being changed in the persistance layer or you're not getting it from the same persistance layer object that you're saving changes in. How is it that the connection you're creating and saving from is provided to the OrderLogic object to retrieve the EntityObject_Orders object?
It looks like your Order instance came from a different entity connection.
Related
I currently have New Relic tracking performance of a specific method as follows:
[Transaction(Web = true)]
public IHttpActionResult List(string companySiteName, [FromUri]DateTime? asOf, [FromUri]string status = "approved")
{...}
Since this method is running slowly, I want to get a more detailed view of what is happening internally. I know that adding the [Trace] tag to methods within List will do this, but my problem is that all my code within List are definitions of variables using Linq and database connections.
Any idea on how to add New Relic Trace to non methods?
(Documentation Pages I used found here and here)
The database work should already be captured, if the database is one we support, so you should be able to see if the database work is causing the slowdown and why on your charts.
If the database work is not causing the slowdown, then, logically, LINQ would be the culprit. In this case, I would recommend pulling the queries out into their own methods and instrumenting those so which one is to blame.
This is a contrived example however I have simplified it for ease of explanation.
Please see my update at the bottom before investing too much of your
time!
Background
I have some (a lot of) code that ordinarily queries my DB as follows:
SELECT name FROM sites where IsLive=1;
My challenge is to, under certain conditions, return the full list of sites, essentially
SELECT name from sites;
I do not wish to modify the actual C# code issuing the SQL (although I can do if I have to in order to achieve my goal which is purely for demonstration purposes).
Therefore in order to leave as much untouched as possible my thoughts are to insert a database-proxy-view called site that returns the data dependent on a control variable
Method
Rename existing site table to site_table
Create a new view named site that the C# code now unknowingly targets and which returns the (possibly filtered) details from site_table according to the control variable value (Note a limitation on variables in views meant I had to create a function in order to demonstrate this - see http://dev.mysql.com/doc/refman/5.7/en/create-view.html wrt error 1351)
Changes made
ALTER TABLE site RENAME TO site_table;
CREATE FUNCTION controlVariableFn() RETURNS VARCHAR(16) RETURN #controlVariable;
CREATE OR REPLACE VIEW site AS SELECT * from site_table WHERE (IsLive = 1 OR controlVariableFn() = 'SHOWALL');
The above statements are ugly but achieve the result I want, however my problem is to dynamically pass through controlVariable without changing my main SQL queries being sent.
My Question
Is there a way to (ideally as I am creating my connection object) define the controlVariable outside the actual SQL to be executed but which the View can still access similar to the above as though it had been supplied as a regular user variable parameter to the query?
so the code would look something like
var connectionString = "Server=localhost;User ID=un;Password=pw;Database=dbname;....";
DbConnection db = new MySql.Data.MySqlClient.MySqlConnection
(connectionString, "controlVariable=SHOWALL");
var results = db.Query<Site>("SELECT * FROM site;");
(I understand that this would not be a smart permanent solution)
Update
My preferred solution as outlined above will not work for me as once I get into my data access layer as the results set will
essentially be filtered again back to the original set. There are some circumstances where it
could work; it would depend on the SQL issued (e.g. when collapsing a
results set down instead of trying to expand a results set as I was
trying to do here).
In that regard I am no longer looking for an answer here but will leave it for posterity as a preferred option and as per the guidelines - thanks anyway.
If you do not want to edit the c# code then the variable will have to be stored in the database although i am not sure how you will not edit the code.
If you are willing to edit the code then you can access a secondary configuration table which will have the settings that you would like the user to pass to the view. take this and allow the user to select which they want and then pass it to the view through the application.
I'm new to NHibernate, but have managed to get it all running fine for my latest project. But now I've reached the inevitable performance problem where I need to get beyond the abstraction to fix it.
I've created a nunit test to isolate the method that takes a long time. But first a quick overview of my domain model is probably a good idea:
I have a 'PmqccForm' which is an object that has a 'Project' object, which contains Name, Number etc and it also has a 'Questions' object, which is a class that itself contains properties for various different 'Question' objects. There is a JobVelocityQuestion object which itself has an answer and some other properties, and a whole bunch of similar Question objects.
This is what I'm talking about with my PmqccForm having a Questions object
This is the questions part of the model:
The key point is that I want to be able to type
form.Questions.JobVelocityQuestion
as there is always exactly 1 JobVelocityQuestion for each PmqccForm, its the same for all the other questions. These are C# properties on the Questions object which is just a holding place for them.
Now, the method that is causing me issues is this:
public IEnumerable<PmqccForm> GetPmqccFormsByUser(StaffMember staffMember)
{
ISession session = NHibernateSessionManager.Instance.GetSession();
ICriteria criteria = session.CreateCriteria(typeof(PmqccForm));
criteria.CreateAlias("Project", "Project");
criteria.Add(Expression.Eq("Project.ProjectLeader", staffMember));
criteria.Add(Expression.Eq("Project.IsArchived", false));
return criteria.List<PmqccForm>();
}
A look in my console from the Nunit test which just runs this method shows that there is nearly 2000 sql queries being processsed!
http://rodhowarth.com/otherstorage/queries.txt is the console log.
The thing is, at this stage I just want the form object, the actual questions can be accessed on a need to know basis. I thought that NHibernate was meant to be able to do this?
Here is my mapping file:
http://rodhowarth.com/otherstorage/hibernatemapping.txt
Can anyone hint me as to what I'm missing? or a way to optimize what I'm doing in relation to NHibernate?
What if I made the questions a collection, and then made the properties loop through this collection and return the correct one. Would this be better optimization from nhibernates point of view?
Just try to add fetch="subselect" to the mapping file for Questions component and see if this solves the issue with multiple selects to that table - you should now see one 2nd select instead of hundreds separate queries, e.g.
<component name="Questions" insert="true" update="true" class="PmqccDomain.DomainObjects.Questions" fetch="subselect">
See for more info - Improving performance
i'm new to Silverlight and it's totally driving me crazy!
i'm using SL4 and trying to bind datagrid with a Domain Service Query:
what i did exactly is i generate an ado.net entity model from my database,
then create a domain Service class from this model.
then in my page i put Datagrid & button.
in the click event for this button i write this code:
RRDomainContext rr = new RRDomainContext();
this.dataGrid1.ItemsSource = rr.Rooms;
this.rr.Load(this.rr.GetRoomsQuery());
when this method executed the nothing changed at the datagrid.
can anyone help me with this ?
Silverlight is async only, so if the query is a large one it can either take a long time to return, or if it's a large query that goes over the 64k limit it can fail and you don't know about it.
As a thought, if you're using RIA services to bind to something reasonably simple, use the "Data Source" for the entity, it's a lot less likely to give issues.
Link
im using subsonic 3.0.0.3 activerecord and everything is fine and i get no error but when i update a database, it never seems to actually happen, can anyone spot anything i am missing here?! ta
code:
var myquote = createNewQuote();
var gross = 36.00;
myquote.totalcost = gross; // set the new value in my model
UpdateModel(myquote); // update the model, something to do with dirty columns??
if(ModelState.IsValid) // check to make sure i have no errors in my model after changing it
myquote.Update(); // command that is supposed to update the database????
i have put a break point on UpdateModel and looked into the model and the value is in the model as directed, i get no error and modelstate is valid too, but when i look into the database the totalcost has not actually been changed for that record????
i have also tried .Save() but this too does not seem to change anything ?????
am a little puzzled
not sure if this fixes your problem but I know there a few bugs fixed here that helped me out with a few isssues I was having.
Goto: http://github.com/subsonic/SubSonic-3.0/tree/master
There was a bug introduced in 3.0.0.1 that I removed quickly with 3.0.0.2 that inhibited updates in certain scenarios with ActiveRecord. Our current version is 3.0.0.3 - you should update.
Subsonic is not yet stable.. You will end up wasting your time googling around.. (peace!)
to solve your problem try this.
myquote.SetIsLoaded(true);