Grails Project jdbc TSQL how to create a datetime2 column - tsql

Hi I have a Grails project and regardless what data kind for dates (java.sql.Timestamp, java.util.Date) I offer in the Domain Class it always creates a datetime column in the TransactSQL Database ( Microsoft SQL Server 2008 R2 . I need a datetime2 Field.
Does anybody know what data type I can use that the DomainClass creates a datetime2 Field?
I use this : postgresql-9.0-801.jdbc4.jar

In your mapping section:
dateField(sqlType: 'datetime2')

You can use mapping to customize database type
class Address {
Date date
static mapping = { date type: 'datetime2' }
}

Related

Entity Framework Core Data Type Conversion differences between SQLite vs SQLServer

I have a SQLite and SQL Express databases both of which have a table with the columns as below:
my simplified Entity look as below:
public class Customer
{
public string BusinessIdentifier { get; set; }
}
if you notice the datatype is different between the Database bigint vs string on my entity for an example.
I have used a Fluent API to do the mapping as shown below:
entity.Property(p => p.BusinessIdentifier).HasColumnName("CUSTOMER")
on the SQLite when i use options.UseSqlite(connectionString); this work just fine. For SQLite connectionString="Data Source=my_db.db"
however when I use SQL Server Express using options.UseSqlServer(connectionString); it starts to give me errors on the type mismatch.
I have to explicitly handle this conversion on the Fluent API as below:
entity.Property(p => p.BusinessIdentifier).HasColumnName("CUSTOMER").HasConversion(v => Convert.ToInt64(v), v => v.ToString());
SQL Server connectionString="Data Source=my_machine\SQLEXPRESS;Initial Catalog=my_db;Integrated Security=True;"
Question:
Can someone please explain why is this difference between the 2 types of databases and is it really needed to be so specific in every case?
Regards
Kiran
SQLite does not force for data type constraints, It allows you the store a value which is of different data type, so this might be the reason that your code works fine with SQLite, On the other hand, SQL Server enforces you to have to the same datatype.
You can refer to this doc https://www.sqlite.org/datatype3.html.

Data enumerations (drop down list configurations) Kie Workbench

I am using drools kie workbench 6.4.0 war deployed on wildfly server 8.1.0. I am using guided decision table editor (**GDT editor) to generate compiled .gdst file from the workbench.
To prevent manual typing and potential wrong values being typed, i am using enum classes within the project. Besides i have a mysql database setup on my local system.
Now for example if i have a enum class say City as below in my Kie workbench project
public enum City {
LONDAN("londan"),
PARIS("paris");
private String city;
City(String city) {
this.city = city;
}
public String getCityValue() {
return this.city;
}
}
Corresponding to this enum class is a City table in DB with following structure and data
If I create a rule using GDT editor which uses the City enum class as column in WHEN clause which will look like
and the source generated would be like
rule "Row 1 Sample"
dialect "mvel"
when
tp : TransactionProcess( city == City.LONDAN or City.PARIS (based on what i choose as an input either london or paris) )
then
end
Now the dropdown for city column shows 2 values as it picks it up from the enum class City. My question is how can i make use of City table defined in mysql database to fetch the distinct list of values in city column defined in City table in DB and populate the dropdown for city column in my Sample.gdst file instead of reading the values from an enum class to populate the dropdown.
To summarize i would like to know:
How can i enable kie workbench to connect to a datasource.
How can i use the datasource to query the database from the workbench and execute a SELECT on a table in DB and use the table column values to populate the dropdown to so that i can choose a value for a column in the WHEN clause of my rule when using GDT editor.
Thanks,
Ashish Sood.

Entity Framework and Default Date

I have a table in SQL, within that table I have DateTime column with a default value of GetDate()
In entity framework I would like it to use the SQL date time instead of using the local date time of the computer the console app is running on (the SQL server is 1 hour behind).
The column does not allow nulls either, currently it passes in a date value of 1/1/0001 and I get an error:
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.
Thank you!
Open edmx designer
Select your DateTime column
Go to properties and change StoreGeneratedPattern from None to Computed
That will tell EF not to insert value for that column, thus column will get default value generated by database. Keep in mind that you will not be able to pass some value.
If you're using Fluent API, just add this to your DbContext class:
modelBuilder.Entity<EntityName>()
.Property(p => p.PropertyName)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
Set columnType for that entity in OnModelCreating Method:
modelBuilder.Entity().Property(s => s.ColumnName).HasColumnType("datetime2");

Is Time Datatype in SQL Server 2008 R2 supports Entityframework with MVC4

I am using ASP.NET MVC4 with Entityframework.
Does Entityframework supports new Time datatype of SQL.
Thank you
Yes it is supported. If you have table which uses Time SQL type it will be recognized as EDM.Time and generated entity will use TimeSpan as property type. In the same way it works with code first approach. If you map property with TimeSpan type the table will contain SQL Time column.

How to manage GetDate() with Entity Framework

I have a column like this in 1 of my database tables
DateCreated, datetime, default(GetDate()), not null
I am trying to use the Entity Framework to do an insert on this table like this...
PlaygroundEntities context = new PlaygroundEntities();
Person p = new Person
{
Status = PersonStatus.Alive,
BirthDate = new DateTime(1982,3,18),
Name = "Joe Smith"
};
context.AddToPeople(p);
context.SaveChanges();
When i run this code i get the following error
The conversion of a datetime2 data type to a datetime data type resulted in an out-of-range value.\r\nThe statement has been terminated.
So i tried setting the StoreGeneratedPattern to computed... same thing, then identity... same thing. Any ideas?
You have to manually edit the edmx xml and set your SSDL StoreGeneratedPattern attributes to identity or computed. But whenever you update your edmx via the designer your changes will get overwritten.
This is a known issue. Please see the following links for more details:
Microsoft Connect Ticket
Using a GUID as an EntityKey in Entity Framework 4
I had the same problem! For me it works like this:
Database MS SQL Server express:
[RowTime] [datetime2](7) NOT NULL
ALTER TABLE [dbo].[Table_1] ADD CONSTRAINT [DF_Table_1_RowTime] DEFAULT (getdate()) FOR [RowTime]
GO
Then I import the table from database to my Entities model.
Entities will not realise the default value!
So, you have to set the StoreGeneratedPattern of the column to Computed.
Then Entities will not put there any default value any more.
Combination of:
datetime2,
NOT NULL,
StoreGeneratedPattern=Computed
Works for me!
Changing type of DateCreated to datetime2 might solve the problem.
datetime 2007-05-08 12:35:29.123
datetime2 2007-05-08 12:35:29. 12345
Ref: http://technet.microsoft.com/en-us/library/bb677335.aspx67
Here is a working workaround:
1) Change the column to datetime2 as mentioned elsewhere. This fixes the conversion error.
2) Add a trigger that sets DateCreated to getdate();
CREATE TRIGGER [TR_AS_ChangeTime] ON [AS_ApplicationSession]
AFTER INSERT,UPDATE AS
BEGIN
SET NOCOUNT ON;
UPDATE AS_ApplicationSession
SET AS_ChangeTime = getdate()
WHERE AS_Id IN(SELECT AS_ID FROM INSERTED)
END
3) If neccessary, set
p.DateCreated = DateTime.MinValue;
just to initialize it.
4) If you need the DateCreated from the database, add
context.Refresh(System.Data.Objects.RefreshMode.StoreWins, p);
just after
context.SaveChanges();
Focusing on the fact that I do not want change the database, since it is an application problem and I expect them to solve this problem one day, my solution (that is totally possible in my case) is to create a partial class of the model to correct the problem on constructor:
public partial class Log
{
public Log()
{
this.Date = DateTime.Now;
}
}
This works for me because:
I create the model on the moment I send it to database.
I use CLOUD for these services, the datetime must be the same in both Application and Database servers!
Don't forget that the namespace needs to match the Model namespace or partial should not list properties (and should no be partial as well ;])!