I am trying to incorporate Telerik Data Access (using SQLite) into some new development.
One of the requirements is to try to insert data into a table, but only if it does not already exist (using a Unique "Code" value to check the existence, rather than the PK id).
The plan was to use the DataContext as a transaction, so that multiple records can be committed at once, and easily roll backed if an error occurs.
For this to work, the entities to be inserted need to be queried along with the entities already in the database.
I was under the assumption that this is a standard feature of an ORM, and was expecting this to work straight out of the box. Am I missing a setting or something?
Here's some code that may help explain what I'm talking about:
[TestMethod]
public
void
when_a_record_is_inserted_it_should_be_retrievable_BEFORE_save_changes_is_called()
{
var testTable =
new
TestTable()
{
Id = 1,
UniqueValue =
"Test"
,
OtherValue =
"Testing 123"
};
using
(var dataContext =
new
TestFluentModel.TestFluentModel())
{
dataContext.Add(testTable);
var toBeInserted = dataContext.GetChanges().GetInserts<TestTable>().SingleOrDefault(x => x.UniqueValue ==
"Test"
);
Assert.IsNotNull(toBeInserted); //THIS WORKS, BUT WOULD MEAN ALL MY "GETS" WOULD NEED TO SEARCH THIS AS WELL AS GETALL
var contextOnlyRecord = dataContext.GetAll<TestTable>().SingleOrDefault(x => x.UniqueValue ==
"Test"
);
Assert.IsNotNull(contextOnlyRecord);
//THIS FAILS
dataContext.SaveChanges();
var dbRecord = dataContext.GetAll<TestTable>().SingleOrDefault(x => x.UniqueValue ==
"Test"
);
Assert.IsNotNull(dbRecord);
//THIS WOULD WORK, BUT ONLY BECAUSE THE RECORD IS NOW IN THE DB
}
}