Entity Framework executes both INSERT and UPDATE stored procedures when you do INSERT - entity-framework

I have this stored procedure, which is mapped in an Entity Framework 4.1 object. The call is made within the
using (TransactionScope transaction = new TransactionScope())
{
try
{
DbEntity.Car.AddObject(CarInfo);
DbEntity.SaveChanges();
/* Other object savings */
transaction.Complete();
DbEntity.AcceptAllChanges();
}
catch (Exception exp)
{
throw exp;
}
finally
{
DbEntity.Dispose();
}
}
I see the stored procedure mapping done currently. If I execute the stored procedure alone on MS SQL server, it executes it correctly.
Here is the stored procedure
ALTER PROCEDURE [dbo].[Carinsert] #Qty INT
,#StyleID INT
,#TFee MONEY
,#HWayTax MONEY
,#OFees MONEY
,#OFeesDescription NTEXT
,#MUp DECIMAL(18, 4)
,#BAss MONEY
,#PriceMSRP MONEY
,#PriceSpecial MONEY
AS
BEGIN
SET nocount ON
DECLARE #PTotal MONEY
DECLARE #TaxFeesNet MONEY
DECLARE #CarID INT
SET #TaxFeesNet = Isnull(#TFee, 0) + Isnull(#HWayTax, 0)
+ Isnull(#OFees, 0)
IF( #PriceSpecial IS NULL )
BEGIN
SET #PTotal = #PriceMSRP + #TaxFeesNet
END
ELSE
BEGIN
SET #PTotal = #PriceSpecial + #TaxFeesNet
END
INSERT INTO Car
(Qty
,StyleID
,MUp
,BAss
,PriceMSRP
,PriceSpecial
,TFee
,HWayTax
,OFees
,OFeesDescription
,PriceTotal)
VALUES (#Qty
,#StyleID
,#MUp
,#BAss
,#PriceMSRP
,#PriceSpecial
,#TFee
,#HWayTax
,#OFees
,#OFeesDescription
,#PTotal)
SELECT Scope_identity() AS CarID
END
If I execute this like on MS SQL it calculates the PriceTotal column in the table as 3444.00, which is correct.
#Qty= 5,
#StyleID = 331410,
#TFee = NULL,
#HWayTax = NULL,
#OFees = NULL,
#OFeesDescription = NULL,
#MUp = 4,
#BAss = 10000,
#PriceMSRP = 20120,
#PriceSpecial = 3444
When I run the MVC web application, and I debug & see these are the values passed and the PriceTotal comes to 20120.00
I couldn't figure out why it does not do the IF ELSE calculation & use the price.
Does anybody else see something weird? This has been daunting for few days now. Any help appreciated. Thanks
Update
I updated the title to better guide others

After few minutes of posting the question, I figured it all out.
There was actually 2 bug.
Entity framework used the UPDATE stored procedure even when I try to INSERT new records. Just for records, the UPDATE stored procedure required the CarID [primary key]. May be EF first does the INSERT and then does the UPDATE right away, even for creating new records?
The UPDATE sproc had a bug checking for NULL using <>. It should have been IS NOT NULL
In anycase, now This is a bigger issue. How to force EF to use INSERT sproc and not the UPDATE when I only want to do is create a new record?
I tried
DbEntityContext.ObjectStateManager.ChangeObjectState(carInfo, EntityState.Added);
and still EF kept on calling the UPDATE sproc.

Related

Firebird dynamic Var New and Old

I need validate dynamic Fields from a Table. For example:
CREATE TRIGGER BU_TPROYECTOS FOR TPROYECTOS
BEFORE UPDATE AS
DECLARE VARIABLE vCAMPO VARCHAR(64);
BEGIN
/*In then table "TCAMPOS" are the fields to validate*/
for Select CAMPO from TCAMPOS where TABLA = TPROYECTOS and ACTUALIZA = 'V' into :vCAMPO do
Begin
if (New.:vCAMPO <> Old.:vCampo) then
/*How i get dynamic New.Field1, New.Field2 on query return*/
End;
END ;
The question is : How can I put "The name of the field that the query returns me " in the above code .
Ie if the query returns me the field1 and field5 , I would put the trigger
if ( New.Field1 < > Old.Field1 ) or ( New.Field5 < > Old.Field5 ) then
There is no such feature in Firebird. You will need to create (and preferably) generate triggers that will reference all fields hard coded. If the underlying table changes or the requirements for validation, you will need to recreate the trigger to take the added or removed fields into account.

update data using loop in sql syntax

I work with postgreSQL
I want to update email of all my users using sql
I have a table named user that contains 500 users,
so I think that I should use a loop in my sql syntax
For example when the table contains 4 users, I want the email for these users to become :
user1#hotmail.fr
user2#hotmail.fr
user3#hotmail.fr
user4#hotmail.fr
in java it should be like this
String newValue=null;
for(int i=0;i<list.size();i++)
{
newValue="user"+i+"#hotmail.fr";
// make update
}
I think that I should use plsql syntax
updated :
I try without success with this code :
BEGIN
FOR r IN SELECT * from user_
LOOP
NEXT r;
UPDATE user_ SET emailaddress = CONCAT('user',r,'#hotmail.fr')
END LOOP;
END
I solved the problem using this query :
UPDATE user_ SET emailaddress='user' || col_serial || '#hotmail.fr' FROM
(SELECT emailaddress, row_number() OVER ( ORDER BY createdate) AS col_serial FROM user_ ORDER BY createdate) AS t1
WHERE user_.emailaddress=t1.emailaddress

Entity Framework and Stored Procedure

I'm using EF 4.3 to execute a stored procedure which just deletes some records and returns 0 when successfull else 1. When i execute the sp (using this.DbContext.Database.ExecuteSqlCommand(sql, id)) i'm getting -1 which is not what i'm expecting.
Can someone tell me what's wrong?
Basically the stored procedure is very simple:
BEGIN TRY
-- Delete records here
END TRY
BEGIN CATCH
IF ##TRANCOUNT > #BeginTranCount
ROLLBACK TRANSACTION
RETURN 1
END CATCH
IF ##TRANCOUNT > #BeginTranCount
COMMIT TRANSACTION
RETURN 0
Also the sp doesn't return value with out variable.
We really need to see your code but here is the general idea:
SqlParameter parm = new SqlParameter() {
ParameterName = "#MyID",
SqlDbType = SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
};
Database.ExecuteSqlCommand("exec #MyId = dbo.MyProc", id);
int retyrnedId = (int)id.Value;

Metadata about a column in SQL Server 2008 R2?

I'm trying to figure out a way to store metadata about a column without repeating myself.
I'm currently working on a generic dimension loading SSIS package that will handle all my dimensions. It currently does :
Create a temporary table identical to the given table name in parameters (this is a generic stored procedure that receive the table name as parameter, and then do : select top 0 * into ##[INSERT ORIGINAL TABLE NAME HERE] from [INSERT ORIGINAL TABLE NAME HERE]).
==> Here we insert custom code for this particular dimension that will first query the data from a datasource and get my delta, then transform the data and finally loads it into my temporary table.
Merge the temporary table into my original table with a T-SQL MERGE, taking care of type1 and type2 fields accordingly.
My problem right now is that I have to maintain a table with all the fields in it to store a metadata to tell my scripts if this particular field is type1 or type2... this is nonsense, I can get the same data (minus type1/type2) from sys.columns/sys.types.
I was ultimately thinking about renaming my fields to include their type in it, such as :
FirstName_T2, LastName_T2, Sex_T1 (well, I know this can be type2, let's not fall into that debate here).
What do you guyz would do with that? My solution (using a table with that metadata) is currently in place and working, but it's obvious that repeating myself from the systables to a custom table is nonsense, just for a simple type1/type2 info.
UPDATE: I also thought about creating user defined types like varchar => t1_varchar, t2_varchar, etc. This sounds like something a bit sluggy too...
Everything you need should already be in INFORMATION_SCHEMA.COLUMNS
I can't follow your thinking of not using provided tables/views...
Edit: As scarpacci mentioned, this somewhat portable if needed.
I know this is bad, but I will post an answer to my own question... Thanks to GBN for the help tho!
I am now storing "flags" in the "description" field of my columns. I, for example, can store a flag this way : "TYPE_2_DATA".
Then, I use this query to get the flag back for each and every column :
select columns.name as [column_name]
,types.name as [type_name]
,extended_properties.value as [column_flags]
from sys.columns
inner join sys.types
on columns.system_type_id = types.system_type_id
left join sys.extended_properties
on extended_properties.major_id = columns.object_id
and extended_properties.minor_id = columns.column_id
and extended_properties.name = 'MS_Description'
where object_id = ( select id from sys.sysobjects where name = 'DimDivision' )
and is_identity = 0
order by column_id
Now I can store metadata about columns without having to create a separate table. I use what's already in place and I don't repeat myself. I'm not sure this is the best possible solution yet, but it works and is far better than duplicating information.
In the future, I will be able to use this field to store more metadata, where as : "TYPE_2_DATA|ANOTHER_FLAG|ETC|OH BOY!".
UPDATE :
I now store the information in separate extended properties. You can manage extended properties using sp_addextendedproperty and sp_updateextendedproperty stored procedures. I have created a simple store procedure that help me to update those values regardless if they currently exist or not :
create procedure [dbo].[UpdateSCDType]
#tablename nvarchar(50),
#fieldname nvarchar(50),
#scdtype char(1),
#dbschema nvarchar(25) = 'dbo'
as
begin
declare #already_exists int;
if ( #scdtype = '1' or #scdtype = '2' )
begin
select #already_exists = count(1)
from sys.columns
inner join sys.extended_properties
on extended_properties.major_id = columns.object_id
and extended_properties.minor_id = columns.column_id
and extended_properties.name = 'ScdType'
where object_id = (select sysobjects.id from sys.sysobjects where sysobjects.name = #tablename)
and columns.name = #fieldname
if ( #already_exists = 0 )
begin
exec sys.sp_addextendedproperty
#name = N'Scd_Type',
#value = #scdtype,
#level0type = N'SCHEMA',
#level0name = #dbschema,
#level1type = N'TABLE',
#level1name = #tablename,
#level2type = N'COLUMN',
#level2name = #fieldname
end
else
begin
exec sys.sp_updateextendedproperty
#name = N'Scd_Type',
#value = #scdtype,
#level0type = N'SCHEMA',
#level0name = #dbschema,
#level1type = N'TABLE',
#level1name = #tablename,
#level2type = N'COLUMN',
#level2name = #fieldname
end
end
end
Thanks again

EF4 - update [Table] set #p = 0 where

While going through SQL profiler, I noticed the following query generated by EF4.
exec sp_executesql N'declare #p int
update [dbo].[User]
set #p = 0
where (([UserID] = #0) and ([RowVersion] = #1))
select [RowVersion]
from [dbo].[User]
where ##ROWCOUNT > 0 and [UserID] = #0',N'#0 int,#1 binary(8)',#0=1,#1=0x000000000042DDCD
I am not sure why EF4 generates this while I am actually not updating any columns of the User table in that UnitOfWork. Running this query updates the RowVersion column (timestamp datatype) which leads to OptimisticConcurrencyException in the next UnitOfWork.
A quick googling led me to this link, which confirms that others have also run into this scenario without finding a solution yet.
Would greatly appreciate any pointers.
Edit: A sample code to replicate the issue.
User and Session tables have a foreign key relationship. Also, in EF4 I have set the "Concurrency Mode" property of RowVersion columns of both entities to Fixed.
Below is a sample method to replicate the scenario.
private static void UpdateSession()
{
using (var context = new TestEntities())
{
context.ContextOptions.ProxyCreationEnabled = false;
var session = context.Users.Include("Sessions").First().Sessions.First();
session.LastActivityTime = DateTime.Now;
context.ApplyCurrentValues("Sessions", session);
context.SaveChanges();
}
}
I see from Sql profiler the following queries being genrated by EF4.
exec sp_executesql N'update [dbo].[Session]
set [LastActivityTime] = #0
where (([SessionID] = #1) and ([RowVersion] = #2))
select [RowVersion]
from [dbo].[Session]
where ##ROWCOUNT > 0 and [SessionID] = #1',N'#0 datetime2(7),#1 int,#2 binary(8)',#0='2011-06-20 09:43:30.6919628',#1=1,#2=0x00000000000007D7
And the next query is weird.
exec sp_executesql N'declare #p int
update [dbo].[User]
set #p = 0
where (([UserID] = #0) and ([RowVersion] = #1))
select [RowVersion]
from [dbo].[User]
where ##ROWCOUNT > 0 and [UserID] = #0',N'#0 int,#1 binary(8)',#0=1,#1=0x00000000000007D3
Not sure if this is still problem for you but here is the hotfix by MS http://support.microsoft.com/kb/2390624
Found this link referenced on another forum and was able to obtain a download for the hotfix mentioned by Kris Ivanov.
http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2390624
Not sure about EF4, but with 4.1 we turned off the rowversion/timestamp by setting it to concurrenttoken = false.
We did this because
It was a calculated field in our db
It should never be changed by the application (in our case)