LINQPad "The name SubmitChanges does not exist" - virtual-machine

Working with LINQPad 5 on virtual machine win 10. Select queries work, now I try to use update query and it gives me an error about SubmitChanges() method.
CS0103 The name 'SubmitChanges' does not exist in the current context.
Basically all answers that I found are like this: write SubmitChanges() or SaveChanges() if you are using EF. Also I tried to change from C# Statements to C# Program.
How to submit changes in LinqPad
In one answer user might not add a connection, which I did. (screen 1)
LINQpad: Global function SubmitChanges not found
Then I tried to pass a "this" to a method, and in my case "this" is an UserQuery, not a DataContext. (screen 2) Which is wierd.
linqpad - SubmitChanges Extension
void Main()
{
var ret =from t in Tbblankimages where t.Comment == "1234" select t;
var im = ret.First();
MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream("E:\\saved.jpg",FileMode.Open,System.IO.FileAccess.Read))
{
file.CopyTo(ms);
}
byte[] imageData= ms.ToArray();
im.Image = imageData;
Test(this);
}
static void Test(System.Data.Linq.DataContext c)
{
c.SubmitChanges();
}
Screenshots with error messages:
https://imgur.com/a/18a7lSo (screen 1)
https://imgur.com/a/Nipp7t5 (screen 2)
Also I should add that the database is PostgreSQL and I use custom driver to it from here:
https://github.com/fknx/linqpad-postgresql-driver
Version of Postgresql is 11.3-4-windows-x64

With postgresql driver it works differently:
https://github.com/fknx/linqpad-postgresql-driver
var user = Users.Single(u => u.Id == 42);
user.Emailaddress = "john.doe#abc.com";
this.Update(user);

Sometimes Linqpad disconnects and causes this problem.
I click on the top left Refresh button to solve this problem.

Related

MonoGame/XNA Mouse.GetState() always returns 0,0 position

I'm trying to get the position of the cursor by calling the mouse class and using the GetState method but the return value is always 0,0. I've searched everywhere and all the code looks the same on other examples. I've tried alternative ways of declare the class but I get the same results.
public void Update() {
var ms = Mouse.GetState();
cursorPos = new Vector2(ms.X, ms.y);
}
If you are using Mono, it's possible that Mouse.GetState method is extended. On some past versions there was problems Mouse.SetState method, could be that also problem was also in Mouse.GetState... so I suggest you take latest Mono framework.
Or you can try to access directly to that method.
var ms = Microsoft.Xna.Framework.Input.Mouse.GetState();
var mp = new Point(ms.X, ms.Y);

Error when trying add data to RavenDb

I'm using autofac and the interfaces are correctly resolved but this code fails with "No connection could be made because the target machine actively refused it 127.0.0.1:8081"
using (var store = GetService<IDocumentStore>())
{
using (var session = store.OpenSession())
{
session.Store(new Entry { Author = "bob", Comment = "My son says this", EntryId = Guid.NewGuid(), EntryTime = DateTime.Now, Quote = "I hate you dad." });
session.SaveChanges();
}
}
Here is the registration
builder.Register<IDocumentStore>(c =>
{
var store = new DocumentStore { Url = "http://localhost:8081" };
store.Initialize();
return store;
}).SingleInstance();
When I navigate to http://localhost:8081 I do get the silverlight management UI. Although I'm running a Windows VM and vmware and Silverlight5 don't play together. That's another issue entirely. Anyways does anyone see what I'm doing wrong here or what I should be doing differently? Thanks for any code, tips, or tricks.
On a side note, can I enter some dummy records from a command line interface? Any docs or examples of how I can do that?
Thanks All.
Just curious, are you switching RavenDB to listen on 8081? The default is 8080. If you're getting the management studio to come up, I suspect you are.
I'm not too familiar with autofac but, it looks like you're wrapping your singleton DocumentStore in a using statement.
Try:
using (var session = GetService<IDocumentStore>().OpenSession())
{
}
As far as dummy records go, the management studio will ask you if you want to generate some dummy data if your DB is empty. If you can't get silverlight to work in the VM, I'm not sure if there's another automated way to do it.
Perhaps using smuggler:
http://ravendb.net/docs/server/administration/export-import
But you'd have to find something to import.

Linq to Sql in mvc 4

I'm having some problems editing an object in the mvc4 framework using linq to sql.
The "tbBoeking" object has been generated by Visual Studio 2010 and resides in a .dbml file. It has just been generated and no alterations have been made to it or the database.
Code in BoekingController.cs:
//This class has been generated and resides in a .dbml file
private DataClassesDataContext db = new DataClassesDataContext();
//Display edit form
public ActionResult Edit(int id = 0)
{
tbBoeking boeking = db.tbBoekings.Single(p => p.boeknummer == id);
if (boeking == null)
{
return HttpNotFound();
}
return View(boeking);
}
//Process changes made in form
[HttpPost]
public ActionResult Edit(tbBoeking boeking)
{
if (ModelState.IsValid)
{
db.tbBoekings.Attach(boeking, true);
db.SubmitChanges();
return RedirectToAction("Index");
}
return View(boeking);
}
Displaying the edit form works fine but when I press submit and the second Edit() is called things go wrong:
On db.submitchanges() I get an error which simply states:
"Row not found or changed".
I have read a few other posts about this error but they were not helpful for me. I think I'm making some basic mistake with Linq-to-sql or concurrency. Am I using Attach() in the wrong place or is it something else?
Thanks in advance,
Blight
Is all of the required information filled in? Also, do you have some sort of timestamp that might need updated. I have heard of the code ignoring the asUpdated flag if a versioning system is in place. That error can be very generic, so checking some other things can help
If the above suggestions do not help, then I would run a SQL profiler trace to see the SQL that is being fed to the server.

Execute a SQL stored procedure before every query generated by EntityFramework

I need to execute a SQL stored procedure every time before I query my ObjectContext. What I want to achieve is setting the CONTEXT_INFO to a value which will be later on used with most of my queries.
Has anyone done that? Is that possible?
[EDIT]
Currently I'm achieving this by opening the connection and executing the stored procedure in my ObjectContext constructor like this:
public partial class MyEntitiesContext
{
public MyEntitiesContext(int contextInfo) : this()
{
if (Connection.State != ConnectionState.Open)
{
Connection.Open(); // open connection if not already open
}
var connection = ((EntityConnection)Connection).StoreConnection;
using (var cmd = connection.CreateCommand())
{
// run stored procedure to set ContextInfo to contextInfo
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "[dbo].[SetContextInfo]";
cmd.Parameters.Add(new SqlParameter("#ci", _contextInfo));
cmd.ExecuteNonQuery();
}
// leave the connection open to reuse later
}
}
Then in my integration test:
[TestMethod]
public void TestMethod1()
{
using (var ctx = new MyEntitiesContext(1))
{
Assert.AreEqual(2, ctx.Roles.ToList().Count);
Assert.AreEqual(2, ctx.Users.ToList().Count);
}
}
But this requires me to leave the connection open - this is error prone since I will always need CONTEXT_INFO, and another developer might easily do:
[TestMethod]
public void TestMethod2()
{
using (var ctx = new MyEntitiesContext(1))
{
// do something here
// ... more here :)
ctx.Connection.Close(); // then out of the blue comes Close();
// do something here
Assert.AreEqual(2, ctx.Roles.ToList().Count);
Assert.AreEqual(2, ctx.Users.ToList().Count); // this fails since the where
// clause will be:
// WHERE ColumnX = CAST(CAST(CONTEXT_INFO() AS BINARY(4)) AS INT)
// and CONTEXT_INFO is empty - there are no users with ColumnX set to 0
// while there are 2 users with it set to 1 so this test should pass
}
}
The above means that I can write the code like in my test and everthing is green (YAY!) but then my colleague uses the code from TestMethod2 somewhere in his business logic and it's all f'd up - and nobody knows where and why since all tests are green :/
[EDIT2]
This blog post certainly does not answer my question but actually solves my problem. Maybe going with NHibernate will be better suited for my purpose :)
We have used this pattern.
But the way we did it was to call the stored procedure as the first opperation inside each db context.
Finally I found the answer. I can wrap the connection using the EFProvider wraper toolkit from EFProviderWrappers.
To do this I mostly have to derive from EFProviderWrapperConnection and override the DbConnection.Open() method. I already tried it with the Tracing provider and it works fine. Once I test it with my solution I will add more information.

LinQ to SQL : InvalidOperationException in a Windows Service

I have implemented a small Windows Service which runs every 9 minutes and writes the data which comes througth a webservice to the db.
Do do the db work I use Linq To SQL
using (var db = new DataClasses1DataContext())
{
var currentWeather = this.GetWeatherData();
//////TODO Add the data correct
var newEntry = new WeatherData()
{
test = currentWeather.dateGenerated.ToShortTimeString()
};
//var test = db.WeatherDatas.First();
db.WeatherDatas.InsertOnSubmit(newEntry); // this throws Invalid Operation Exception
db.SubmitChanges();
}
Why does it throw this exception? the same codeblock in a console programm runs nice
alt text http://img687.imageshack.us/img687/7588/unbenanntxb.png
Have you set up the connection string in app.Config correctly?
IIRC, the default constructor on an L2S DataContext reads the connection string from the config file. If the connection string points to the the wrong database (or one that doesn't exist) you may very well receive an exception.
This could also explain why this piece of code works when executed in a different context.