EF 4.1 is trying to insert values in identity column despite StoreGeneratedPattern being set to Identity in SSDL - entity-framework-4.1

I have a table called ScanMasters in SQL Server 2008 which has a column called PresentationAuditId as the primary key. This column is also a foreign key. The table also has an identity column called ScanPartId. I am using Entity Framework 4.1 to generate POCOs based on my database. The EF SSDL represents the entity in this manner:
<EntityType Name="ScanMasters">
<Key>
<PropertyRef Name="PresentationAuditId" />
</Key>
<Property Name="ScanPartId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="RackNum" Type="int" />
<Property Name="LoadDatetime" Type="datetime" />
<Property Name="PresentationAuditId" Type="bigint" Nullable="false" />
<Property Name="Item" Type="varchar" MaxLength="31" />
</EntityType>
When I add new data for this entity and do SaveChanges(), I am getting the following error:
Cannot insert explicit value for identity column in table 'ScanMasters' when IDENTITY_INSERT is set to OFF.
Can we have a non-primary key column as an identity column in EF 4.1? How can I go about this?

Related

How to set column default value to today's date in SQL using XML

I want to add a column to one of my SQL schema's tables.
I have tried the following:
<property name="now" value="now()" dbms="mysql"/>
<changeSet author="Trey Collier" id="SCHEJ-376">
<addColumn tableName="schedule">
<column name="dateLastPublished" type="DATETIME" defaultValueDate="${now}">
<constraints nullable="false" />
</column>
</addColumn>
</changeSet>
However, this inserts the date in the format YYYY-MM-DD hh:mmm:ss, but I only want the YYYY-MM-DD part of it. How can I change my current XML code to make this happen? Thanks
You should change defaultValueDate="${now}" with defaultValueDate="(DATE(CURRENT_TIMESTAMP))" and type="DATETIME" with type="DATE"

How to manually increment UNIX_TIMESTAMP() when insert a new row with liquibase?

I have a cart table with a cart_number column which I would like to be the current time in milliseconds.
I've created a database change log file and everything works fine but the problem is that the span of time between 2 inserts is too short to get a "unique" cart_number. Here is the liquibase insert:
<property name="now" value="UNIX_TIMESTAMP()" dbms="mysql"/>
<changeSet id="2" author="Me">
<insert tableName="cart">
<column name="user_id" value="1"/>
<column name="cart_number" valueDate="${now}"/>
</insert>
<insert tableName="cart">
<column name="user_id" value="2"/>
<column name="cart_number" valueDate="${now}"/>
</insert>
</changeSet>
And I know this isn't a solution, but I've tried to write something like ${now+1} but it didn't worked. Any help would be appreciated. Thank you in advance.

Liquibase - Add defaultValueComputed as CURRENT_TIMESTAMP to timestamp column

I am using liquibase 3.5.3 to run liquibase update command on MySql 5.5. I have below changeSet to create a table which has a column as Created_Time that should have a default value as CURRENT_TIMESTAMP.
<changeSet author="authorName" id="AutoGeneratedId">
<createTable tableName="aTable">
<column autoIncrement="true" name="Id" type="INT">
<constraints primaryKey="true"/>
</column>
<column name="Code" type="VARCHAR(45)"/>
<column defaultValueComputed="CURRENT_TIMESTAMP" name="Created_Time" type="TIMESTAMP(19)"/>
</createTable>
</changeSet>
While firing liquibase command it throws an exception as
Unexpected error running Liquibase: Invalid default value for 'Created_Time' [Failed SQL: CREATE TABLE aTable (Id INT AUTO_INCREMENT NOT NULL, Code VARCHAR(45) NULL, Created_Time TIMESTAMP(19) DEFAULT NOW() NULL, CONSTRAINT PK_ATABLE PRIMARY KEY (Id))]
Liquibase is converting CURRENT_TIMESTAMP into NOW() that might be causing this issue.
Can some please provide me any solution or alternative to this issue?
Add type as 'TIMESTAMP' as following
<column defaultValueComputed="CURRENT_TIMESTAMP" name="Created_Time" type="TIMESTAMP"/>
This is a weird scenario causes by how liquibase handles DateTime defaultValueComputed or defaultValueDate.
In short anything that starts with current_timestamp or the default function on your target engine to get the current timestamp will replace *the whole string* by only the default function call ignoring anything else you put.
We ended up with something like this:
<column name="created_at" type="datetime"
defaultValueComputed="NOW()">
<constraints nullable="false" />
</column>
<column name="updated_at" type="datetime"
defaultValueComputed="NOW() ON UPDATE NOW()">
<constraints nullable="false" />
</column>
LiquibaseDataType helper function

Can Spring Batch use MySQL for its internal BATCH_ tables?

I have been using Oracle for this without any trouble but I then had to switch it all over to use MySQL and am seeing this error during initialization:
org.springframework.dao.DataAccessResourceFailureException: Could not obtain sequence value; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown table 'BATCH_JOB_SEQ' in field list
The tables are present so its something else going wrong here. After debugging I captured the actual sql it was trying to perform to get the sequence:
select BATCH_JOB_SEQ.nextval from dual;
Which is obviously an Oracle statement!
My config states this to setup the connection:
<bean id="springDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://10.252.205.5:3306/MASKNG" />
<property name="username" value="MASKNG" />
<property name="password" value="maskng" />
</bean>
Any help appreciated...
jobRepositoryFactoryBean.setDatabaseType(“mysql”)
Seems like there is no BATCH_JOB_SEQ created here in MySQL.
You need to create the sequence for that. Refer How do I create a sequence in MySQL? for creating sequence.

NHibernate : Store VARBINARY to MAX

I am using following mapping to store a Serializable object to SQL Server 2008:
<class name="EMSApplication.Data.Domain.Configuration, EMSApplication.Data" table="ems_Configurations" proxy="EMSApplication.Data.Domain.IConfiguration, EMSApplication.Data" lazy="true">
<id name="Id" type="System.Int32">
<column name="Id" not-null="true"/>
<generator class="native"/>
</id>
<property name="Settings" type="Serializable">
<column name="Settings" not-null="true"/>
</property>
</class>
It is generating a varbinary(8000) for column type of the database. How can I make it to use varbinary(max)?
If I use:
<property name="Settings" type="Serializable" length="2147483647">
<column name="Settings" not-null="true"/>
</property>
It is also truncated to 8000. I am using NHibernate3.2(not fluent).
According to the nHibernate documentation, "length" is not an attribute/property of <property> but instead should be used in <column>.
This section shows that "length" is not a part of <property> :
http://nhibernate.info/doc/nh/en/index.html#mapping-declaration-property
This section shows that "length" is a part of <column> :
http://nhibernate.info/doc/nh/en/index.html#toolsetguide-s1-2
That last section (20.1 and the Table 20.1 Summary) shows that "sql-type" is also part of <column> so try one of these variations:
<property name="Settings" type="Serializable">
<column name="Settings" not-null="true" length="2147483647"/>
</property>
or
<property name="Settings" type="Serializable">
<column name="Settings" not-null="true" sql-type="varbinary(max)"/>
</property>
Edit:
This question seems to be a duplicate of:
How do I get fluent nhibernate to create a varbinary(max) field in sql server
but that information is almost 3 years old and the newer version of nHibernate might have corrected for this (I have no way to test this).
The following page appears to also be the same issue and is much more recent:
Binary Blob truncated to 8000 bytes - SQL Server 2008 / varbinary(max)