LINQ insert ignoring default values, what about stored procedures? - c#

Unfortunately I found out that LINQ ignores the default values when inserting rows. The work around is to change the Auto Generated Value property of each column in the DBML to True
This is fine and dandy for tables that only have a few of these columns, but i have a table that has 20+ that need to default.
Before I spend the time creating a stored procedure, will a stored procedure even respect/use the default values?
EDIT: LINQ ignores the default values when inserting rows
This means if I have 10 columns, and I only give values for the first 3 columns with my LINQ insert, the remaining 7 columns will have a value of null instead of the default values defined by the table

Any SQL insert will respect the default values as long you don't explicitly pass in NULLS for the column values.
This will work fine in a stored procedure.

Related

Enforce Entity Framework to follow constraints defined in database

I have a table with 10 columns, in which 7 of them are not null with constraints on it.
ALTER TABLE [dbo].[MyTable]
ADD DEFAULT (1) FOR [Column1]
Now, when I am inserting 3 columns from Entity Framework like below, it is not inserting the default values into the table:
Table obj1 = new Table();
obj1.Column7 = someValue;
obj1.column8 = someValue;
obj1.column9 = someValue;
context.Entry(obj1).State = EntityState.Added;
context.SaveChanges();
Unlike my expectation, above statement is just updating 3 columns and not using the default values for the other 7 columns.
How can I enforce Entity Framework to insert default values defined by the constraint in database?
The default values as defined in your SQL Server table structure are only applied if you have an INSERT statement that leaves out those columns - e.g. doesn't provide any value (including NULL) for those columns.
EF however will always supply values for all columns as defined by the entity - e.g. it will supply NULL for any values not explicitly set. There's nothing you can do about this, as far as I know.
There are some workarounds:
you could create a stored procedure which would take only those values you want to supply, and would run a SQL INSERT statement that would leave out the columns with default values, e.g.
INSERT INTO dbo.MyTable (colA, colB, ..., colZ) -- *exclude* "Column1" here!
VALUES ( .........)
With this, SQL Server would then use the configured default values for those column that have one defined and which are not mentioned in the column list of the INSERT INTO statement
you could probably do something similar on the EF level, by specifying a second class, that would be almost identical to your current entity class - but that would leave out the columns you want to have set by SQL Server's configured default values. If you save this "slimmed down" entity, EF would create a SQL statement that would include only those columns that it knows about (in terms of the fields of that slimmed down class) and this would result basically in the same as above - an INSERT INTO statement that excludes those columns which are automatically set by a configured SQL Server default constraint

Column added to view not getting picked up by Entity Framework?

I added a new column MyColumn to vwMyView in MyDB. MyDB has a stored procedure MySproc which returns vwMyView.* in a select statement.
When I execute MySproc, vwMyView.MyColumn is included in the result set. However, when I recreate my EF6 .edmx data model, MyColumn is not reflected in the EF vwMyView object or in MySproc_Result.
Is there a special trick I need to use so my new column MyColumn is reflected in a recreated EF6 .edmx? Is this a quirk that you encounter sometimes?
Happens sometimes, especially when the output is generated runtime as a polymorphic behavior (different result sets based on conditions)
In EF, for a result set, you may need to explictly create an output mapping. No need to delete the whole EDMX, just select the mapping (MySproc_Result) and update the result set to include the new column.
Are you creating the view inside procedure and then returning its value?
Inside the view, check if the columns are added conditionally (eg, if type=Sales, include 4 columns, if type=Mgmt, include 5 columns). The EF will pick up the 1st matching case in this condition.
Best way is to update the mapping and have the new column set as nullable, just in case some conditional makes it unavailable.

How to pull the data from a table with columns which allow nulls to the table of not null-able columns

i need to pull the data from table which has all columns of allowing null type to another table which have columns matching with previous table but of not null type.
So i need a query for pulling entire data of all the matching columns.with a default value in the position of null row.
I am assuming that a given column is of integer type and default value is 0. You can use below based on your RDBMS:
SQL Server
ISNULL(COL_1,0)
Oracle
NVL(COL_1,0)
or
COALESCE(COL_1,0)
MYSQL
IFNULL(COL_1,0)
or
COALESCE(COL_1,0)
Replace the col_1 with your column name and default value 0 with appropriate value

Dynamic Update stored procedure SQL Server

I have a table with 30+ fields. Many of the fields allows NULL. My question is if I want to update the field(s) that changed value, how do I do that with stored procedure? Currently I am selecting a specific row and storing all the current values in the table. Next I would have to compare each variable to the new one to see if they are different, and if it is, then add that field name and the new variable to the string query. Then execute. Is there a more efficient way of doing this? As you imagine, it is a pain to compare all 30+ fields, especially since many are Nullable. Thank you.
So here is my logic so far in my stored procedure:
Have all the parameters that are nullable and set them to null (since it is optional).
DECLARE a variable for each field which I will be testing (30+ fields)
SELECT the row identify by the ID passed in from the parameter (primary key).
Store all the values into each variable.
Check each value to see if it IS NULL. If so, that means the user did not provide the field so no update in necessary for that field. If the parameter value IS NOT NULL, check if it is == to the value SELECTED from the database. If the value !=, then add it to the query string to be updated.
Execute the update query string.

Query on Identity Column on MS SQL DB

I have created table in MS SQL 2008 with one identity column(Start Value - 1 and Increment is also 1) and 4 other columns. I am accessing this DB from C# ASP.NET. Used to push data only for the non identity column. Identity column will auto increment itself.
As of now i am manually querying the column value with the remaining for columns. But I am facing problem if all the other four column values are equal i am not getting the exact value which i am looking for
Now my query is, Is there any why in C# where I can get the value of the newly created identity column whenever new record is created.
Thanks.
You can use
SCOPE_IDENTITY()
Which will returns the primary key value of the recently inserted row
The answer to your question actually lies in SQL Server. You can run:
SELECT ##identity
after your insert to get the last inserted row identity.
http://technet.microsoft.com/en-us/library/aa933167(v=sql.80).aspx
EDIT BASED ON COMMENTS:
Consider using SCOPE_IDENTITY() as referenced here:
http://technet.microsoft.com/en-us/library/aa259185(v=sql.80).aspx
In SQL terms you can output the records back if you wish it. But how you might apply this to C# is up to you. Example:
INSERT INTO TABLE_A (SOMETHING, SOMETHINGELSE, RANDOMVAL3)
OUTPUT inserted.A_ID, inserted.SOMETHING, inserted.SOMETHINGELSE, inserted.RANDOMVAL3
SELECT 'ASD','DOSD', 123
But unless you're using merge, you can't use OUTPUT to print out any values from joining tables from an INSERT. But that's another matter entirely, I think.
Also, it's hardly good practice to bounce this data between the application and the DB all the time, so I'd look to alternatives if possible.

Categories

Resources