I'm having trouble trying to use the 'Auto-Sync' feature of DBML with SQLite. I have a class in my data model that contains a primary key (id). This key is defined as "INTEGER PRIMARY KEY", which SQLite maps to the rowid of the row. To support this, I have set "Auto Generated Value" to 'True' and "Auto-Sync" to 'OnInsert'.
The problem is, when I commit a new entry to the database, the SELECT string used by the LINQ to SQL classes is not supported by SQLite:
SELECT CONVERT(BigInt,SCOPE_IDENTITY()) AS [value]
Instead, SQLite has the last_insert_rowid() function, which I cannot seem to point to.
Does anyone know how to remedy this? Possibly by changing the statement used to select the last row ID or some other trick I'm missing?
EDIT There appears to be some traffic on the provider's website, but no resolutions.
EDIT Since I seem to have confused the question, here's how I've set up my application. Hopefully it helps shed some light on my thought process, and maybe an underlying issue.
Add new "LINQ to SQL Classes" file to my solution
Model my database in the designer, named DataModel
Open a database using a System.Data.SQLite.SQLiteConnection
Initialize the DataModel instance using this connection
You are adding a "Linq-to-SQL" data model to your project, but you're using it against SQLite - that'll never work of course! Linq-to-SQL only ever supports SQL Server (always has, always will), and thus its SQL statements that it generates are SQL Server T-SQL Statements - nothing else.
If you want to use the Entity Framework with SQLite, you need to use "ADO.NET Entity Data Model" (file with the .EDMX extension) as your data model.
Only that will support third-party database drivers like SQLite and others!
Devart implementation of LINQ to SQLite does not contain this problem. The autoincrememnt column is treated correctly.
I ran into the same issue and found the following workaround - use the SQLiteConnection.Changed event to intercept the query and fix it up:
SQLiteConnection.Changed += (obj, eventArgs) => {
var cmd = eventArgs.Command;
if (eventArgs.EventType == SQLiteConnectionEventType.NewDataReader && cmd != null)
{
cmd.CommandText = cmd.CommandText.Replace( #"SELECT CONVERT(Int,SCOPE_IDENTITY()) AS [value]"
, #"; SELECT last_insert_rowid() AS value");
}
};
Related
I wonder if this is even possible, and I can't seem to find an answer. I am using Entity Framework / C# for a database application. I want to ensure that my front-end entities automatically match their counterparts in SQL for attributes like "StringLength". So, inside SQL, in my tables, if I specify, for example, "nvarchar(50)", I want to make sure that the user doesn't type more than 50 characters. I don't want to have to code this each time, so I'm looking for a way to have the C# application read the StringLength property from SQL and then apply logic in the front-end. I can't seem to find a way of getting this information by entity type/DbContext from the SQL database itself.
I thought I'd struck gold with this thread here: How to get the maximum length of a string from an EDMX model in code? but this seems to read from the entity schema defined in the C# application, not SQL.
I have attached a .png of the info I am trying to get from SQL for clarification. Any advice would be appreciated:
use this query to return Maximum length of column Description in table PaymentType
SELECT
CHARACTER_MAXIMUM_LENGTH
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
TABLE_NAME = 'PaymentType' and COLUMN_NAME = 'Description'
If I have a database in each table where the ID field and its appropriate function in any field do not take the administrator behavior so that tables and field contents can be fetched until the serial number is unified without duplicate values
Appropriate in this context using except.
Is there a code that can fetch tables either in sql or in the Entity Framework ؟
Eexcept_Admin_except_List
List<int> tempIdList = answeripE.Select(q => q.ID).ToList();
var quslist = db.Qustion.Where(q => !tempIdList.Contains(q.ID));
\Thanks for the creator of "daryal" Get All Except from SQL database using Entity Framework
I need to do this without asking for each table and querying it. And also request SQL from the database as a whole without exception such as
select*
IDfield
FROM
MSDB_Table T
WHERE
T.id == MaxBy(T.OrderBy(x => x.id);
can replace "where TABLE1.id 'OR' Table2.id" decode all the tables and give a result.
All I'm looking forward to is that I can query one database on a whole, get it on a list without the use of tables or a composite key because it serves me in analyzing a set of data converted to other data formats, for example when representing a database in the form of JSON There are a lot of them on more than one platform and in a single database and to avoid the repetition of the data I need to do this or a comprehensive query may be compared or to investigate or like Solver Tool in Excel, so far did not get the answer to show me the first step is because it does not exist originally or because it is not possible?
If you want Entity Framework to retrieve all columns except a subset of them, the best way to do that is either via a stored procedure or a view. With a view you can query it using LINQ and add your predicates in code, but in a stored procedure you will have to write it and feed your predicate conditions into it...so it sounds like a view would be better for you.
Old example, but should guide you through the process:
https://www.mssqltips.com/sqlservertip/1990/how-to-use-sql-server-views-with-the-entity-framework/
I absolutely do not get this. The column exists in the table, I've ensured that the application is executing the query against the proper table in the proper database, and it still reports that it's an invalid column name.
I'm running NET 4.0, SQL Server 2008 Express Edition. Does anyone have any similar experience?
Executing queries against any other column name in the same table in the same database works extremely excellently. I added this column today and for some reason my application refuses to acknowledge the existence of this column.
Relevant column definition:
Relevant code:
(from x in flightDataContext.FlightDatas
where x.FlightDataId == FlightDataID && x.Departure == true
select new
{
x.ArrivalStationCode,
x.ArrivalStationName,
x.DepartureTime,
x.DepartureGate
}).SingleOrDefault();
I also faced the same problem.You can try the following solutions.
You should update your classes after the database changed(drag and drop your tables to the linq to sql file), so that the column is accessible with object name. if still problem exists then
the value you are saving in the coloumn is greater then the size specified for the coloumn. try varchar(MAX) if your coloumn is of varchar type.
When I encountered this error, I discovered that I had some spurious [Key] attributes on one of my Model objects that wasn't actually part of the unique key definition for that object's table. They had been innocuously sitting there until I refactored another of the object's relationships. Once I removed them, the error about the columns being invalid vanished.
Try this. Before this , delete the table and drag drog the table again on the DBML file and save the DBML.
var flight= flightDataContext.FlightDatas.SingleOrDefault(x=>x.FlightDataId == FlightDataID && x.Departure);
Now you can access:
flight.ArrivalStationCode,
flight.ArrivalStationName,
flight.DepartureTime,
flight.DepartureGate
i have a problem where i can't apparently find a solution even after hours of debugging.
I have a simple SQL command where i want to update a row with some value(the value is a text of about ~5mb), after executing the SQL Command, the 'ExecuteNonQuery()' on the C# code side, returns '1' and no exception but the changes are sometime reflected in the Database row and sometime not.
I tried debugging to see if the values that will replace the old one are correct and they are. I am using MySQL 5.5.11.
Could it be a MySQL setting or something?
Losing my mind on this problem, if you have any idea it would be greatly appreciated.
EDIT, include code:
The code is as simple as this:
cmd.CommandText = "UPDATE user SET data = #data WHERE id = #id";
then i add params to the DBCommand object for the SQL Query. The 'data' is about ~5mb big.
this command always returns '1' even if the changes are not reflected in the database(sometime it works, sometime it doesnt):
cmd.ExecuteNonQuery();
Thanks in advance!
I don't know if the mysql provider uses autocommit? If not then you have to call commit on the Transaction object you can get with BeginTransaction on the connection object.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx
The Microsoft .NET Framework Data Provider for SQL Server does not support the question mark (?) placeholder for passing parameters to a SQL Statement or a stored procedure called by a command of CommandType.Text. In this case, named parameters must be used.
For example:
SELECT * FROM Customers WHERE CustomerID = #CustomerID
Edit:
Just noticed this was MySQL, and while i can't find anything simliar about it quickly, i would suggest you use named parameters anyways
I had this problem and it was related to autocommit.
Problem: earlier in the app lifespan autocommit was set to 0 by another process in the app, and failed. The connection with autocommit turned off is then reused by the connection pool.
That will lead to this type of error at unpredictable times.
I'm working on a WPF system which hooks onto an old legacy database using the Entity Framework. We hook onto a backup of the live data when debugging and an empty database to run tests against. I'm receiving the following error only when trying to delete from a backup of the live data.
This code:
License license = ReadLicense(id);
entities.DeleteObject(license);
entities.SaveChanges();
produces this SQL:
exec sp_executesql N'delete [dbo].[Product]
where ((([Group_ID] = #0) and ([Parent_ID] = #1)) and ([Prod_ID] = #2))',N'#0 nvarchar(32),#1 nvarchar(32),#2 nvarchar(32)',#0=N'someIdValue1',#1=N'someIdValue2',#2=N'someIdValue3'
which in turn produces this error:
Msg 208, Level 16, State 1, Procedure TrackDeletedProduct, Line 4.
Invalid object name 'DeletedRecords.dbo.Product_Deleted'.
If you change the generated SQL to a 'select' query it returns a row so, 'ReadLicense' is returning a valid entity. I can't really understand why this doesn't work, especially when it's only against live data. The 'License' entity is one of two inheriting from a base 'Product' entity.
Cheers.
From what I can see, it would appear as if your table Product has a trigger on it that will fire when you delete a row, and which calls that stored procedure TrackDeletedProduct.
That procedure will (just guessing here) try to copy the product entry to the Product_Deleted table in the DeletedRecords database, but fails on that - either that other database or the table don't seem to exist.
So I don't think this has really anything to do with ADO.NET Entity Framework or LINQ-to-Entities, but a lot with SQL Server - check your backend database configuration!
Marc