Unwanted column is part of index in generated DBContext - c#

I have created Web API core project and generated entity framework core related components as detailed in the article here
It generated all the entities properly, but few indexes were created with an extra column. Here is an example
entity.HasIndex(e => new { e.Column1, e.Column2, e.Column3 })
.HasName("ix_Table1_Column2_Column3");
In the above example, column1 is not actually part of index when i checked DB directly. I am puzzled as to what is causing this issue.
This presence of unwanted column is causing below exception.
An exception of type 'System.InvalidOperationException' occurred in
Microsoft.EntityFrameworkCore.dll but was not handled in user code
Additional information: The indexes {'Column1', 'Column2', 'Column3'}
on 'Table1' and {'Column2', 'Column3'} on 'Table1' are both mapped to
'able1.IX_Column2_Column2' but with different columns ({'Column1',
'Column2', 'Column3'} and {'Column2', 'Column3'}).
Any help is much appreciated. Thanks in advance!

Related

MongoDb Indexing with deference of 2 dates

In my Mongo db collection have 2 date fields TimeStamp, OpEndTime, I need to take order by OpEndTime-TimeStamp. It is working with a small amount of data. For huge data throw exception out of ram space. So need to create the index with difference of OpEndTime and TimeStamp. I don't know how to create it. I am using C# mongo db driver
I tried like below
DBContext.ClientDb.Repository(collection).Indexes.CreateOne(Builders.IndexKeys.Ascending(i =>i.OpEndTime- i.TimeStamp));
but am getting error like below
An exception of type 'System.InvalidOperationException' occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: Unable to determine the serialization information for i => Convert((i.OpEndTime - i.TimeStamp)).

C# System.Data.SQLite : Concurrency violation: the UpdateCommand affected 0 of the expected 1 records

I have a .net winforms application which is using SQLite DB and I am using System.Data.SQLite dll.
This is how I load a Datatable:
DataTable table = new DataTable();
SQLiteDataAdapter m_readingsDataTableDataAdapter;
SQLiteCommandBuilder m_readingsDataTableCommandBuilder;
m_readingsDataTableCommandBuilder = null;
m_readingsDataTableDataAdapter = null;
// Create fresh objects
m_readingsDataTableDataAdapter = new SQLiteDataAdapter(sql, Database.getInstance().connectionObj);
m_readingsDataTableCommandBuilder = new SQLiteCommandBuilder(m_readingsDataTableDataAdapter);
m_readingsDataTableDataAdapter.Fill(table);
return table;
This data table has one primary key and no other constraints.
I set it as a data source for DataGridView and after all the edits, I update the DataTable like this:
m_readingsDataTableDataAdapter.Update(table);
Occasionally, the updates fires an error and I don't know when, it stops throwing errors - probably after system restart ( not sure ). And then, the updates go fine until another situation where this update throws an error again. When the error occurs, it happens for all updates from then on even after application restart.
Error:
An unhandled exception of type 'System.Data.DBConcurrencyException' occurred in System.Data.dll
Additional information: Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.
I would appreciate any help or questions as this is quite a critical section of my project.
Thanks.
Update:
Based on suggestions, to ensure no other part of the program is editing the row, I loaded the DataTable, updated the row immediately and updated it as per the following code. And I still got the error. There is no other program running on my machine so the row is not updated by any other program except my application:
DataTable table = new DataTable();
SQLiteDataAdapter m_readingsDataTableDataAdapter;
SQLiteCommandBuilder m_readingsDataTableCommandBuilder;
m_readingsDataTableCommandBuilder = null;
m_readingsDataTableDataAdapter = null;
// Create fresh objects
m_readingsDataTableDataAdapter = new SQLiteDataAdapter(sql, Database.getInstance().connectionObj);
m_readingsDataTableCommandBuilder = new SQLiteCommandBuilder(m_readingsDataTableDataAdapter);
m_readingsDataTableDataAdapter.Fill(table);
// Update immediately
table.Rows[0]["RS485_ADDRESS"] = "400";
m_readingsDataTableDataAdapter.Update(table); // - Still throws error
return table;
I had a similar issue that drove me nearly mad. I even debugged into System.Data.SQLite . My issue was caused by the dynamic typing of SQLite:
Dynamic typing means that the column type is just a hint for the type of the stored value but not an enforcement. Here is what happened in my application: An integer column contained an empty string. Fetching the data triggered an implicit conversion to the column type so the empty string was converted to zero. The DbDataAdapter used this zero value in the WHERE clause of an UPDATE or DELETE statement which failed miserably because the conversion of zero to a string is not an empty string. I changed the affected integer columns to contain NULL and everything is fine now.
The sad fact about that: It is not a bug in SQLite or ADO.NET . DbAdapter and DbCommandBuilder expect strong typing and SQLite doesn't provide that by design.
I've seen this problem, it seemed to appear in some cases and not others. I have resolved it a different way. Since this is primarily a data issue, it involves changing the update and delete statements. There may be cases where your code might need to be validated, I'm not referring to those cases, this is for when all else seem to be in order and "Concurrency Violation" persists. And a good starting place before you spend hours debugging is to address how the updates are handled.
Open the Data Set Designer.
Click on the Table Adapter.
In the properties, expand the UpdateCommand
Edit the SQL so the Where-Clause refers only to the primary-key.
Example: Suppose the column EmployeeID is the primary-key in our table
Change to:
update ..... WHERE EmployeeID=#EmployeeID
By default it's usually constructed as:
update ... where ID=#ID AND col1=#col1 AND col2=#col2 AND col3=#col3
The where-clause is formed with all the columns to handle multi-user updates, to enforce an implicit concurrency. So if one user has a copy of the record, and another user updates that version by the time the first user updates it, first user's version is not current. Otherwise, primary-keys should be used to refer to a specific record.
But, this is an update/delete policy decision that the development and database teams need to coordinate. For a single developer/dba in-one, have a little meeting with yourself. Don't take the Microsoft where-clause without your review and understanding and explicit implementation. Need to specifically address what happens when 2 users have a copy of the same record, both from the application and database levels. Database "Transaction Isolation" Levels in SQL Server is a way to address it. ADO, ADO.NET, OLE DB, ODBC all have isolation level settings. More on SQL Server Transactions and isolation levels
In some cases multiple users are not involved, or the application is updating internally, concurrency is fully controlled, where-clause can be formed with the primary-key.
In my case, the update is done without a user involvement, so it makes sense to use the primary key. If you understand the reasoning and the cause of the behavior, it's a matter of adjusting to fit your environment.

Exception when filtering on DateTime field in Azure Table Storage

I'm having a kind of bizzare issue while trying to filter results from Azure Table Storage by a DateTime field. I have two different queries for two diffferent tables with two different models. Both need to filter on a DateTime field but only one works, the other retuns an exception:
Microsoft.WindowsAzure.Storage.StorageException was unhandled by user code
HResult=-2146233088
Message=The remote server returned an error: (400) Bad Request.
Source=Microsoft.WindowsAzure.Storage
StackTrace:
at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)
The queries:
var fromDate = DateTime.UtcNow.AddDays(-14);
Working one:
from entity in AnalyticsStorage.ConversionTable.CreateQuery<ConversionAnalyticsModel>()
where entity.Date > fromDate
select entity
Not Working:
from entity in AnalyticsStorage.Table.CreateQuery<AnalyticsTableModel>()
where entity.ResponseTime > fromDate
select entity
I've made sure the the DateTime field is actually populated in the table. Commenting out the where makes the query run, though obviously that's not very useful.
Has anyone run into this? Are there gotachas in Azure Table Storage that I could be running into but can't see?
This is built against the current version of the Azure Storage DLL (3.0.3) and both queries are side by side in the same class, just in different methods.
I can't say I know why this is happening but after some time with Fiddler I figured out that it was using an anonymous type for the field name:
() gt datetime'2014-02-27T21:55:16.9605195Z'
Should be:
RequestTime gt datetime'2014-02-27T21:55:16.9605195Z'
Once I noticed this I wrote a query using the pre-LINQ syntax:
var query = new TableQuery<AnalyticsTableModel>().Where(
TableQuery.GenerateFilterConditionForDate(
"ResponseTime",
QueryComparisons.GreaterThan,
fromDate));
And this worked perfectly. So there seems to be a consistency issue with the current LINQ library that is causing it to not always map the field in the model to the field in the table. It could be that both RequestTime and ResponseTime are protected words to so my using their names is breaking the implementation without it properly warning me.

How do you delete a row from a split table?

I have two tables. One contains binary data and the other contains the metadata. I am attempting to delete the entire row from both tables, but keep getting the error:
Invalid data encountered. A required relationship is missing.
Examine StateEntries to determine the source of the constraint violation.
The rest of the info is not very helpful. Here is my code currently.
var attachment = _attachmentBinaryRepository.Single(w => w.Id == id);
_attachmentBinaryRepository.Delete(attachment);
_unitOfWork.Commit();
return true;
I was handed this project, but understand the basics of table-splitting. I am just lost in regard to deleting both. I assume, this code is just trying to delete from the one table, but on the one containing the binary data.
Anyone have suggestions?
I don't have the code with me, but I ended up fixing this by retrieving corresponding rows from all of the tables in the relationship. The rows then delete without any trouble.

Input array is longer than the number of columns in this table

I've recently started to use SQLite and began to integrate it into a C# project I'm working on.
However, randomly my project will throw the exception:
Input array is longer than the number of columns in this table
I'm having a hard time trying the trace the problem because it seems to be thrown on a random basis.
DataTable table = new DataTable();
//exception is thrown here
table = Global.db.ExecuteQuery("SELECT * FROM vm_manager");
Some of the data that gets returned from this query is as follows:
http://i.stack.imgur.com/9rlLN.png
If anyone has any advice, I'd be grateful.
EDIT: I'm unable to show the execute query function as it resides inside a dll from the following sql lite wrapper http://www.codeproject.com/KB/database/cs_sqlitewrapper.aspx
EDIT 2 Problem stems from the new record array function inside this particular sql lite wrapper.
Based on your SQLite wrapper's implementation, it is adding columns to its own internal DataTable before returning to yours. I suspect your defect is in the wrapper, and not in your code. I dug into the source of your SQLiteWrapper from CodeProject; here it is at PasteBin: http://pastebin.com/AjGaX0kL
I suspect the error is occurring in that helper method ExecuteQuery(), or its helpers: ReadFirstRow() or ReadFirstRow(), and not your code. Wrap your code in a try catch. Inspect the Exception, and the properties will tell you which method this exception is actually being created in.
Likely you're encountering a defect in this wrapper class.
try
{
DataTable table = Global.db.ExecuteQuery("SELECT * FROM vm_manager");
}
catch (Exception ex){
//who threw this from which method and line?
}
If this SQLite provider isn't working, suggest evaluating:
System.Data.SQLite - An open source ADO.NET provider for the SQLite database engine
ADO.NET 2.0 Provider for SQLite at SourceForge -- and here's a nice tutorial by Mike Duncan on this SQLite provider.

Categories

Resources