Entity Framework translating number(1) to Boolean help? - c#

Im using Devart dotConnect for Oracle - Entity Framework + .NET 3.5. When i create the Entities, the framework change those colums type number(1) into bool. I read that it is a feature of every kind of entity framework. But i don't want it.. We use here number(1) to specify status of an db object.. like Clients - Situation.
I tried to set manually the .ssdl "bool" to "int" and .csdl "Boolean" to "Int32".. it just keep the same error of convert... if i use the "Run Custom Tool" it changes back the field to bool and Boolean.
I tried to change de property manually too in the Diagram.. w/o sucess.

In your Model explorer you will need to change the datatype of the column in the table in the ".Store" part of the model as well. The datatype of the class.property in your xxxModel part needs to match with the datatype of the table.column in your xxxModel.Store. If you choose Int32 in your class, you could choose Decimal in the table.

Related

Change underlying enum value in Postgres

I have .NET Core application which communicate with a Postgres database using entity framework.
There was a enum used in model which create a Postgres enum type in postgres.
Now I need such migration that will change the enum into flags, so I need to change enum integer value in C#.
I feel I should also change the underlaying enum values in Postgres, but I don't see any numbers there, I just see only enum names in type definition.
How the Postgres stores enum internally?
How can I migrated it if integer value of enum is changed in C#?
I am not aware how Postgres stores enum internally and prefer to not rely on it.
Assuming that your enum type is called _enumtype and that column is called _enumcolumn then you can alter the table structure (enum -> integer) and keep the data in it like this:
alter table _mytable
alter column _enumcolumn type integer
using array_position(enum_range(null::_enumtype), _enumcolumn) - 1;
The enum type words will be replaced with 0, 1, 2, .... respectively.
So you are free to make whatever changes and updates you need after that.
This probably does not answer your question directly, but useful for understanding under the hood. Postgres stores enums with both numeric and string components. You use the string component for all references both setting and testing, references the numeric component generates an exception. Internally Postgres uses the numeric component for sorting (perhaps other uses I am not aware of). This numeric is (fyi) stored as a floating point value. Adding value(s) to an emum is a simple Alter type. Removing them cannot be done directly - it evolves deleting and recreating along with managing all dependencies. You can see the component values, both numeric and string with the query (see Demo)
select * from pg_catalog.pg_enum;
That gives you all values for all enums, unfortunately getting a specific one is more complicated.

Does Entitiy Framework set Default value

Hi my problem is that my EF 4.0 mapping does not replicate the Default value of some columns.
For example
in my Question object i have an int type field Status for which i have set Allow null = false (un-check the allow null checkbox) and set its default value to be 1.
Now the strange behavior is when i insert the record in sql server and doesn't set the status field, it works fine and set the value 1 in record after commit.
But my entity framework mapping (.edmx file) doesn't set the default value and it says "Default value = (None)" in property window.
Is that a known issue or i am missing something?
I had this exact same issue a few months back and I did not find any easy resolution to the problem.
I can only suggest that you manually decorate your entities from within your edmx or if possible upgrade to EF 6 and use the Code-First from database option which from my experience works much better.

NHibernate query with filter by timestamp field

I have table with versioning field of type Timestmap. Now I'm trying to make a query with search by this field, but when I'm passing parameter in next way:
query.SetParameter("TimeStamp", lastTick, NHibernateUtil.Timestamp);
it pass it as DateTime to the sql query. Also tried NHibernateUtil.Binary, but nhibernate pass it as varbinary.
The NHibernate TimestampType specifies:
This is almost the exact same type as the DateTime except it can be used
in the version column, stores it to the accuracy the database supports,
and will default to the value of DateTime.Now if the value is null.
This type is simply not for use with the MS SQL type TIMESTAMP. In fact, that column type is deprecated:
The timestamp syntax is deprecated. This feature will be removed in a future version of Microsoft SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
rowversion (Transact-SQL)
You should use NHibernateUtil.Binary or NHibernateUtil.BinaryBlob.

Entity Framework and MySql Stored Procedures

I am working with Entity Framework 4.0, VS 2010, MySql server database, and mysql-connector-net-6.4.4 for connection purpose. It works fine, that said, it can generate Model classes, csdl, ssdl etc files well. But, for stored procedures it doesn't work.
Here is what happens..
Right clicked on an SP from Model Browser, select [Add Function Import]. This opened a dialog box
Filled appropriate values like, Function Import Name, Stored Procedure Name
Click on [Get Column Information]. This results into some Grid filled up at the bottom of this button. In the grid, there is a column named [EDM Type]. This column shows [Not supported] due to some unknown reason :(
Now, clicked on [Create New Complex Type]. This goes OK, without Error
Now, clicked on OK button
After doing all above steps, there is no Complex Type created in the code however, which is the problem.
Can anyone please help?
The generated Complex Type is in the Complex Types folder in the Model part of the Model Browser.
To access ti in code, use it as any other Entity
MyComplexType ct = new MyComplexType();
And you can use it as a result type for your stored procedure:
List<MyComplexType> info = ctx.GetAllEmployees().ToList<MyComplexType>();
You must ensure that the GridView's columns mappings match the Complex Type's properties, or that the GridView's property AutoGenerateColumns is set to true.

How to assign default values and define unique keys in Entity Framework 4 Designer

I've had a look at the Entity Framework 4. While generating code for the SQL Server 2008 I came to the point where I want to define some default values for some fields.
how to define in the designer for a Created DateTime Field the DateTime.Now default value?
-> Error 54: Default value (DateTime.Now) is not valid for DateTime. The value must be in the form 'yyyy-MM-dd HH:mm:ss.fffZ'
how to make for code generation a string Field unique. Like E-Mail or Username can exists only once in the table.
I've search alot in the internet and also checked my books Pro Entity Framework 4.0 and Programming Entity Framework. But none of them seems to come up with the default value issue, or using sql commands as default values for database generation.
Another thing is, how to prevent on database generation always from droping tables?
Instead i want to append non existing fields and keep the data.
Thanks for any suggestions.
You can always define partial class and initialize value in costructor:
public partial class MyEntityClass
{
public MyEntityClass()
{
CreationDate = DateTime.Now;
}
}
If field is unique, you'll have to check it yourself and include in your validation logic.

Categories