Bltoolkit oracle clob type - oracle

I use bltoolkit as orm and i had a problem with clob type.
I have a long string value and i got error while update operation.
Error: ORA01704 - String literal too long.
Checked table and my column type is clob.
There is no clob option in bltoolkit table class design.
I set this column like that:
[MapField("MSG_BODY")]
public string MsgBody { get; set; }
What is wrong ?

I find a solution, post only clob columns and it works !
//update only body
value = db.Schedule
.Where(x => x.Rowversion == _zaman
&& x.ScheduleId == this.ScheduleId)
.Set(x => x.Rowversion, x => _zaman)
.Set(x => x.MsgBody, x => this.MsgBody)
.Update();

Related

EF Core Value Converter In Oracle Wont Work

i use Ef core and Oracle
i have a Column in Oracle Database With Data Type NVARCHAR2(20)
but in my Class Model i want Convert It Decimal
this is Config For ValueConverter :
entity.Property(e => e.ColumnName)
.HasMaxLength(20)
.HasColumnType("NVARCHAR2(20)")
.HasColumnName("COLUMN_NAME")
.HasConversion(
v => v.ToString(),
v => v=="" ? 0 : decimal.Parse(v, NumberStyles.Any, CultureInfo.InvariantCulture));
and this my My Model :
public decimal? ColumnName{ get; set; }
but after use this query this error show :
ORA-00932: inconsistent datatypes: expected NUMBER got NCHAR'
I Convert Column Via Oracle Query with check for number combability
(case when REGEXP_LIKE(ColumnName, '^[[:digit:]]+$') then to_number(ColumnName) else 0 end) as ColumnName

Setting Linq2Db to read and update with CHAR type and NOT NULL constraint in Oracle

Reading value of fixed CHAR string from table, using Linq2db Oracle provider:
CREATE TABLE mytable
(pk NUMBER(15,0) NOT NULL,
fixed_data CHAR(20) DEFAULT ' ' NOT NULL)
Although in database, length of FIXED_DATA filed is 20,
SELECT LENGTH(fixed_data) FROM mytable WHERE pk = 1
-- result is 20
When same field is read using Linq2Db, value gets truncated to empty string:
var row = (from row in database.mytable where row.pk == 1 select row).ToList()[0];
Console.WriteLine(row.fixed_data.Length);
// result is zero
This causes problem when record is updated using Linq2Db, Oracle converts empty string to NULL, and UPDATE fails:
database.Update(row);
// Oracle.ManagedDataAccess.Client.OracleException: 'ORA-01407: cannot update ("MYSCHEMA"."MYTABLE"."FIXED_DATA") to NULL
Is there any setting in Linq2Db for read->update cycle to work with CHAR type and NOT NULL constraint?
Found a solution, thanks to source code openly available. By default, Linq2Db calls expression IDataReader.GetString(int).TrimEnd(' ') on every CHAR and NCHAR column. However this can be easily customized, by implementing custom provider, which overrides field value retrieval expression, with one that does not trim:
class MyOracleProvider : OracleDataProvider
{
public MyOracleProvider(string name)
: base(name)
{
// original is SetCharField("Char", (r,i) => r.GetString(i).TrimEnd(' '));
SetCharField("Char", (r, i) => r.GetString(i));
// original is SetCharField("NChar", (r,i) => r.GetString(i).TrimEnd(' '));
SetCharField("NChar", (r, i) => r.GetString(i));
}
}

linq Command not getting the answer for DB

i have a table(UserQuestions) in my DB(WebSiteUsers) which contains QuestionID field as a Primary key and QuestionContext field which holds the Questions that are asked as its value.
Now i want to have a textBox that show me the QuestionContext Value by getting QuestionID.
I used these linq commands and none of them bring me the correct answer :
string Questioncontext = new WebSiteUsersEntities().UserQuestions.Where(p => p.QuestiuonID.ToString() == QuestionID).Select(p => new { p.QuestionContext}).ToString();
string Questionx = (from q in new WebSiteUsersEntities().UserQuestions where q.QuestiuonID.ToString() == QuestionID select q.QuestionContext).ToString();
QuestionCntxt.Text = Questionx;
the outcome is like this :
SELECT
[Extent1].[QuestionContext] AS [QuestionContext]
FROM [dbo].[UserQuestion] AS [Extent1]
WHERE CAST( [Extent1].[QuestiuonID] AS nvarchar(max)) = #p__linq__0
I guess your QuestionID variable is of type string, while the database column is of type int.
So rather than using
q.QuestiuonID.ToString() == QuestionID
criteria inside the query, convert the variable to int and use that as criteria.
Also ToString just gives you the SQL query text, not the result. Use ToList if you expect more than one result or FirstOrDefault if you expect zero or one results:
var questionID = int.Parse(QuestionID);
string Questioncontext = new WebSiteUsersEntities().UserQuestions
.Where(p => p.QuestiuonID == questionID)
.Select(p => p.QuestionContext)
.FirstOrDefault();
Note that I also changed the select to return directly QuestionContext string rather than anonymous object having QuestionContext property.

Entity Framework Pagination (Select records using string datetime column)

I have a table which has startdate (in format "yyyymmddhhss")and the corresponding entity attribute is string type. I would like to use LINQ To SQL get all the records with StartDate >= SelectedStartDate. Since this table has more than 10 million records, I need to use pagination as well.
Could anyone please suggest on how to implement this/
Regards,
Raaj
The best solution would be of course to migrate the string values to a DATETIME column and query the data from that column.
But if you don't have enough privileges to do that, you're lucky you have the format yyyymmddhhss in your table because that preserves the natural order of dates; all you have to do is to convert the parameter to a string in the required format:
public IEnumerable<YourDataObject> GetData(DateTime date, PageInfo pageInfo)
{
var startDate = date.ToString("yyyyMMddHHss");
return _dbContext.Table
.OrderBy(x => x.StartDate)
.Where(x => String.Compare(x.StartDate, startDate) >= 1)
.Skip(pageInfo.PageSize * pageInfo.PageIndex)
.Take(pageInfo.PageSize)
.Select(x => new YourDataObject
{
//
});
}
Here is a sample output in LINQPad:

Null value cannot be assigned - LINQ query question

I have the following LINQ query:
DataClassesDataContext dc = new DataClassesDataContext();
var query = from contact in dc.Contacts
select new
{
ContactId = contact.ContactId,
LastName = contact.LastName,
FirstName = contact.FirstName,
Addresses = contact.Addresses,
Phones = contact.Phones,
DOB = contact.BirthDate,
LastNote = contact.Notes.Max(n => n.Created), //this line causes the error
Status = contact.ContactStatus.ContactStatusName,
EmailAddress = contact.Emails
};
The line where I get the maximum created date for the notes collection causes the following exception to be thrown:
Exception: The null value cannot be assigned to a
member with type System.DateTime which
is a non-nullable value type.
How do I write the query to allow null values into the LastNote field? The DOB field is defined as DateTime? and has not problem handling nulls.
Think I figured it out.
If I cast the maximum note value to a nullable DateTime it seems to eliminate the exception. The following change worked for me:
LastNote = (Nullable<DateTime>)contact.Notes.Max(n => n.Created)
As others have pointed out, it can also be written using the shorthand notation for a nullable DateTime as follows:
LastNote = (DateTime?) contact.Notes.Max(n => n.Created)
Rewrite that line as:
LastNote = (DateTime?) contact.Notes.Max(n => n.Created),
LastNote = contact.Notes.Max(n => (DateTime?)n.Created)
Couldn't find this on the net so i hope this helps others.
In VB is something like:
LastNote = CType(contact.Notes.Max(n => n.Created), Global.System.Nullable(Of Date))
I think...
You could do that, or you could alter your database schema so that the column 'Created' does not allow nulls.
The scenario is arising because one of the rows comes back with a null value for Created.
If the db didn't allow nulls, the scenario would never occur.

Resources