SQL GETDATE() Inserting 01/01/001 12:00:00 AM to database - c#

In my service-based SQL database one of my table has a column 'feedbackdate' with default value GETDATE(). I am using entity framework to insert data to the table and not mentioning the said column in the insert statement but GETDATE() function is inserting 01/01/001 12:00:00 AM . I also tried current_timestamp and sysdatetime() and sysdatetime().NOW() as Default-Value but result is same.
I also changed the DataType of column from date to datetime2(0) but no luck.
Column definition in table
[feedBackDate] DATETIME2 (0) DEFAULT (sysdatetime()) NOT NULL,
Note: I am using Database-First Model and in EntityDataModel the mapped column's Default Value is NONE

I've got it. The problem is with the Mapped Column in .edmx file. I've changed the StorGenereatePattern from None to Computed of the mapped column and bingo.

Related

Entity framework with Arabic language

We trying to save article (long string) in database column using EF6 database first, the article character is more than 4000 char so we changed the column type from NVARCHAR(4000) to VARCHAR(8000) but after when we tried to save in database, We found the data saved as quationmark '???? ???? ????' , we changed the column collation to Arabic_CI_AI
as following :
ALTER TABLE laws.ArticleContent ALTER COLUMN TextAr
VARCHAR(8000) COLLATE Arabic_CI_AI
but still have the same problem also we update the Edmx after we changed the column properties but still the same, and we change the property in Edmx to Unicode = true and we got the same, so please any help
We use MSSQL2008R2 and EF6 database first
the table after insert :
the Edmx Update :
the table after update column collation :
Change the column data type to nvarchar(max)
by default this will set the field length to 4000 unless you send data exceed the 4000, it will will convert it to a pointer to a data.
Some helpful questions and answers:
What is the maximum characters for the NVARCHAR(MAX)?
Are there any disadvantages to always using nvarchar(MAX)?

is it required to pass timestamp value in entitymodel to insert a data into sql table

I am working on ado.net entity model and I want to insert data into sql table.
SQL table structure is shown below.
ID int,
Name varchar(20),
EventTime Timestamp
I am trying to insert data into SQL Table using entitymodel but data are not inserted in the table.
My passing arguments are (1,"Test") it's not working.
I figured out that when I pass (1, "test", new Datetime()) then it works.
Can anyone let me know reason for that?
I understand that Timestamps value are automatically inserted by SQL server.
The "timestamp" data type gives you a binary value that automatically gets updated every time your field changes, but it won't give you a nice date/time value.
Update:
as per MSDN: timestamp Is a data type that exposes automatically generated, unique binary numbers within a database. timestamp is generally used as a mechanism for version-stamping table rows. The storage size is 8 bytes. The timestamp data type is just an incrementing number and does not preserve a date or a time.
To record a datetime when the record has been inserted, use a datetime data type. You might add a column of type datetime to your table and set getDAte() to generate the datetime.

Entity Model - How to use Database's Default Column Values while inserting

I am using SQLServer 2008, WebForms, C#, Frameowrk 3.5, Entity Framework 1
I generated entity model from DB.
My DB has various tables & lets take example of one table user
It has fields id, name, email, created_on, is_active, is_on_leave
default values for is_active and on_leave properties are default to 0 ( zero ) in db
But When I try to insert a record in this table using entity model, it saves NULL in these fields. How to avoid this? I don't want to set a value for them from my page and want to use the one mentioned in DB.
How can I achieve that?
This columns can be nullable. Right click property in designer, choose properties and set StoreGeneratedPatern for property to computed in the property window.
I found similar question:
Set default value in EF designer datetime
The is_active and is_on_leave columns in your database are nullable. If you make them non-nullable the default values should be used when you do an insert.
ALTER TABLE [user] ALTER COLUMN [is_active] DEFAULT(0) NOT NULL
ALTER TABLE [user] ALTER COLUMN [is_on_leave] DEFAULT(0) NOT NULL
GO

Pass a NULL value to DateTime Field in LINQ

My database table is like this
CREATE TABLE MYBUDGET.tbl_CurrentProperty
(
[PropID] INT NOT NULL IDENTITY(1,1),
[UpdatedOn] DATETIME NOT NULL,
[Amount] MONEY NOT NULL,
[Remarks] VARCHAR(100) NOT NULL,
)
ALTER TABLE MYBUDGET.tbl_CurrentProperty ADD CONSTRAINT PK_CurrentProperty_PropID PRIMARY KEY ([PropID])
ALTER TABLE MYBUDGET.tbl_CurrentProperty ADD CONSTRAINT DF_CurrentProperty_UpdatedOn DEFAULT (DATEADD(MINUTE,30,DATEADD(HOUR, 5, GETUTCDATE()))) FOR [UpdatedOn]
ALTER TABLE MYBUDGET.tbl_CurrentProperty ADD CONSTRAINT CK_CurrentProperty_Amount CHECK([Amount] > -1)
GO
I'm using LINQ to SQL. In C# I need to pass only [Amount] and [Remarks] fields and other fields must be used with their default values ([PropID] and [UpdatedOn]).
In C# I create tbl_CurrentProperties object like below,
tbl_CurrentProperties currentProperties = new tbl_CurrentProperties();
currentProperties.Amount = 50.00M;
currentProperties.Remarks = "remarks";
and then submit the object to the data context. But here, Linq assigned '1/1/0001 12:00:00 AM' for UpdatedOn field. But this violate the SQL datetime range 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM and Occurring an exception. Also I can't assign a NULL value manually for a DateTime field since its a not nullable type. Anyhow I need to make this to use its DEFAULT Constraint. How do I do this?
PS: I want to use it like this because, My database is Online and Users are in different locations. So If I used DateTime.Now, the time in the user machine may be wrong, and It insert a wrong value into DB. I need to use SQL server time always.
Andrey's answer is partly right. I just tested this and here's what I found.
In your dbml designer, on your UpdatedOn column set the following:
Auto Generated Value = True
Nullable = False
Then, on an INSERT if you use SQL Server Profiler to look at the generated SQL, you'll see that UpdatedOn is not included in the INSERT. Not even a null value. This is important: for SQL Server to use a default value for that colum, the column must be omitted from the INSERT. If you set Nullable = True on the UpdatedOn, LINQ to SQL might be including the column on the INSERT with a null value.
FYI, immediately after the INSERT there should be a SELECT where LINQ to SQL is retrieving the auto-generated value, so your entity object has the latest value.
I recommend you open your DBML file using XML Editor and change that column's type from DateTime to DateTime? by allowing nulls. Save it and the code will be generated for you.
Whenever LINQ to SQL generates wrong DBML, it is better to edit it yourself.
Open your dbml and turn on
"Auto Generated Value" = true for the fields that are auto generated. You should be all good with passing nulls in

How to call stored procedure without error in ADO.NET Entity Framework?

How do I call a stored procedure without error in ADO.NET Entity Framework? If I use the code below, I get an error:
adminNameContext.AddItemCategory(12, "ggf", DateTime.Now);
Error:
The data reader is incompatible with the specified 'NetTanitimTestModel.Categories'. A member of the type, 'ID', does not have a corresponding column in the data reader with the same name.
ALTER procedure [dbo].[sp_AddItemCategory]
(
#item int,
#category nvarchar(50),
#date smalldatetime
)
as
begin
if(#item=-1)
begin
insert into Categories(PARENTID,Category,Date) values(null,#category,#date)
end
else
begin
insert into Categories(PARENTID,Category,Date) values(#item,#category,#date)
end
end
i have Categories table which has got 3 columns: PARENTID,Category,Date
It looks as if your EF data model and your database are not in sync anymore. It seems as if your "Categories" object in the EF data model has an "ID" field but the table does not.
I would update the EF data model from the database and see if that fixes the problem. To do this, open up the EDMX designer and right click on an empty spot in the design surface, and pick the "Update model from database" option. That should bring the two worlds back into sync.
Marc
Mitch Wheat gave you the answer. You're trying to use the ID column, but the table has a PARENTID column.

Categories

Resources