Issues while selecting values into nullable type property in Simple.Data - simple.data

I have a integer column in the database which is nullable. So while creating the datamodel i made the property "int?". This way my insert/update statement is working as expected.
However when I try to select data from the table into the same datamodel, the field is not getting populated. What am i doing wrong ? Can somebody please suggest ?

There was a mistake. The database column was in fact "VARCHAR". So I changed the model property to String, and things are now working fine.
So it appears that though Simple.Data is able to select VARCHAR coumn value into int model property, it somehow was failing to convert into int?. Changing model property to String sorted out the issue of both insertion/selection.

Related

Set correct data type with OpenAccess ORM Fluent API

I am using Telerik Data Access Fluent Model(Open Access) Code first approach for generating Database. Everything is going right except some issues.
• I have created a Property as Decimal in code. But in database its data type is numeric not decimal. I need to set data type as decimal but this is giving me numeric.
• Same type of issue is there with Bool property in code that gives me tinyint as datatype in database instead bit. I also set the property as Boolean in C# code and generated column is still tinyInt. I need to set it as bit in database
Here are images for my properties and generated columns(From these properties in Database)
These are properties that are written in code
http://screencast.com/t/sOXOi3as0N
And this is the image of generated table in database
http://screencast.com/t/9KmmEK1IL
it seems to be the default behavior of the product - to map decimal CLR type props to columns of numeric SQL type and bool props to tinyint cols. You need to slightly change the mapping configuration for the package persistent type by specifying the correct SQL types for underlying columns mapped to these props in the following way:
mappingConfiguration.HasProperty(x => x.BasicPrice).HasColumnType("decimal").HasPrecision(18).HasScale(2);
mappingConfiguration.HasProperty(x => x.IsActive).HasColumnType("bit");

Dynamic Data Cannot insert the value NULL

I 'm working on an ASP.NET Dynamic Data Entities Web Application.
When trying to insert a new entry in one of the tables, with some value in the column_name field, I get the following message :
Cannot insert the value NULL into column 'column_name', table 'DATABASE.dbo.table_name'; column does not allow nulls. INSERT fails.
The column properties are :
Entity Key : True
Nullable : False
Type : String
I believe Dynamic Data is trying to send null value to entity framework for some reason, but I don't know which.
Do you know why Dynamic Data is behaving that way ?
Or have you any idea how to debug the insert process ?
Thanks
I found where the problem come from. A table from the database the model is based contains a trigger. For some reason the model makes an association between the two tables involved in the trigger.

Why would servicestack ormlite save valid Guid values as NULL to SqLite database?

I am having this problem intermittently with ServiceStack.Net OrmLite on SqLite.
My model class is using a Guid for the primary Id. My code sets a Guid.NewGuid() value to my model before saving, if the model is new.
I have set a breakpoint to verify that immediately before db.Save(myModel), myModel.Id is always a populated, unique Guid. However, the value committed to the table is NULL.
It seems after I create the database, it saves a the values correctly for a while, but then just starts saving NULLs for the Id! My code is doing the same thing throughout. This seems especially extraordinary to me, since OrmLite creates the column as a non-Nullable primary key.
Has anybody seen this issue, or know what might be causing it? Thank you.
This was caused by having an [AutoIncrement] attribute on the Guid Id. Oops!

How do I force SqlDataSource to use varchar parameters?

I'm using a SqlDataSource to populate my GridView, because the two seem to be so tightly coupled together. Since this grid shows results of a search, I have a dynamic sql string being written in my codebehind that references parameters I pass in, such as below:
sdsResults.SelectParameters.Add("CodeID", TypeCode.String, strCodeID)
My problem is that the CodeID field is a varchar field. As you may have experienced, passing in an nvarchar field to be evaluated against a varchar field can be very detrimental to sql performance. However, SelectParameters.Add only takes in TypeCode types, which seems to only give me the unicode TypeCode.String as my viable option.
How do I force my SqlDataSource to use varchars? I can't change the datatype at this point--it's a main key of a large 10 year old app, and frankly, varchar is right for the application.
BTW, my workaround right now lets SqlDataSource pass in the param as an nvarchar. My first line in my SQL then converts that nvarchar param explicitly to a varchar variable, and use that new varchar variable through my script instead.
But that seems silly.
Maybe this will work:
using System.Data;
sdsResults.SelectParameters.Add("CodeID", SqlDbType.VarChar, strCodeID);
Nope, unfortunately it requires that a System.TypeCode param be passed in. Using a SqlDbType ends up in a compile error.

Cannot insert null where field is Guid (object in SqlDataSource)

I have a table which links to another table in the ASP.NET membership schema.
Problem is, all the PKs for the ASP.NET tables are uniqueidentifier so mine has to be too. When I add a SqlDatasource and call its Insert() method, I get the following error:
Cannot insert the value NULL into column 'DiscountCode', table 'CreamDb.dbo.CustomInfo1'; column does not allow nulls. INSERT fails.
The statement has been terminated.
The uniqueidentifier is also treated as an object (its data type), but there is no Guid data type. I had this problem before, but the schema was much simpler so I could fix it.
How can I go about fixing this? If I get rid of the data type part in the markup (so just leave the field/parameter name but not the data type stuff), I get another error so that is not possible.
Thanks
What do you mean by "there is no Guid data type"? What's wrong with System.Guid? Can't you just use Guid.NewGuid(), set the field appropriately, and do the insert?
EDIT: Just to give a bit more meat: attach an event handler to the Inserting event, and populate the field then, via the DbCommand returned by SqlDataSourceCommandEventArgs.Command. Or change the SQL used by the INSERT command to ask the database to populate the GUID field for you.
A popullar approach when dealing with references to the ASP.NET Membership Provider's data is, instead of keeping a proper foreign key to the GUIDs, instead store something like the LoweredUserName in your table. Then, use the Membership Provider's API to interact with the object you need. In some cases, you need an ObjectDataSource abstraction layer to accomplish CRUD scenarios.
Set the default value of the column in SQL Sever to "newid()".
Asp.net won't send the value, and the field will get a new guid.

Resources