The EntityEntries property will return null because a single entity cannot be identified as the source of the exception - asp.net-mvc-4

I am using Code first approach. how can i fix this problem?

I am assuming "Modified" column is a date time field in the table which is mandatory column.
You can resolve this issue in two ways
You can pass the "Modified" column field each time you do a insert.
Alter the column to accept null values if that field is not mandatory. Following is
sql query you can run to change the column to accept null values.
ALTER TABLE myTable ALTER COLUMN myColumn {DataType} NULL

Related

Filling in a datetime field as empty

I need to add a new record to a database and one of the columns in my table is 'paymentDate', which is a "DateTime" field. Now the record I need to add is one in which the paymentDate is not known. I can't put in 'NULL' as SQL says 'Column 'paymentDate' cannot be null'. Also the '""' doesn't work in a datetime field.
If you have a paymentDate column and you don't know the date, then the column should allow NULL values. So, you should fix the data model:
alter table t alter column paymentDate datetime;
This will remove the not-NULL constraint, so you can add the data that you have.
I would be a little cautious, though. Why are you trying to add a row with an unknown payment date, if the designer of the table thought the value should never be NULL?

Cannot insert the value NULL into column 'id', table 'XXX'; column does not allow nulls. INSERT fails

I am getting an insert error:
Cannot insert the value NULL into column 'id', table 'db.dbo.table';
column does not allow nulls. INSERT fails
.
I have checked the data and the column I am inserting into 'id' does not have any nulls.
Any suggestions?
The error
column does not allow nulls
is happening because you are trying to insert data which has NULL for the column id, for at least one record which you are trying to insert. It has nothing to do with the state of the column before you attempted the insert. Check the source of your insertion data and remove/replace the NULL values, or alter the id column to accept NULL.
When looking at your SQL table design, scroll through "Column Properties" to "Identity Specification". In that drop-down, change "(Is Identity)" to Yes. This setting tells the table to auto-increment the ID field.
Null is not a particular value, but rather, an indication that you don't know the value for this field. It means, "VALUE UNKNOWN". If the field is not NULLable (i.e., if "UNKNOWN" is not permissible), then the INSERT will not be allowed. This is particularly true for an ID column, which might be INDEXed — perhaps even the PRIMARY KEY of the table. Some databases do not allow NULL in INDEXes (particularly primary indexes) at all.

Insertion SQL and NOT NULL values

I've created a table schema and specified that for some attributes, values cannot be null. For one column of this table, values are to be imported from a column of some another table but the problem i am facing is that when i use insert statement to copy values from that column of another table to the column of this newly created table, the attributes of this new column start screaming because they kind of have a constraint on them that while insertion their values cannot be NULL!
How do i cope with this?
One solution is that for other attributes, just for time being, i can state that null values can be accommodated so that i can successfully import values from column of other table and then later on put condition on the remaining attributes that values are not be NULL. But how do i do do this?
You need to convert NULL to some DEFAULT values while importing.
I am not sure which DB engine you are using, in mysql:
Use something like IFNULL(column_name, "").
Reference
You may simply be looking for the default clause. When you define a column, you can specify;
intcol int not null default 0
If the column is not specified for an insert, then it will default to 0. In some databases, if a NULL value is supplied, it will also get the default value.

date syntax issue in sql

I keep getting this error message when trying to change the data type of my column:
alter table x modify column order_date date NOT NULL;
ERROR at line 1
ORA-00905 missing keyword
I not sure where I am going wrong, as I am aware there are many types of dates in sql?
Many thanks
The MODIFY clause does not take COLUMN as a keyword. This will work:
alter table x modify order_date date NOT NULL;
The syntax is documented in the Oracle SQL reference. Find out more.
We only need to include COLUMN with commands which have several different possibilities. For instance, with the ALTER TABLE ... DROP command, because we can drop columns, constraints or partitions....
alter table x drop column order_date ;
"when I tried entering NOT NULL, it said the table needed to be empty"
You should be able to apply a NOT NULL constraint, providing all the rows in the table have a value in the order_date column. The error message you get is quite clear:
ORA-01758 table must be empty to add mandatory (NOT NULL) column
This means your column has some rows without values. So, you need to update the table and populate those rows with some value; what you will use as a default depends on your business rules.

How to insert columns in between in table in sql server 2008

I want to add or update columns using alter table if i am adding a new column i want show error. I am using the code below
alter table Personal_Details alter columns DOB datetime
if i uncheck the NULL to not NULL then it will shows column does not allow nulls; update fails;
i want to insert the fields in between columns not at end.
Plese fix my bug,
Thanks in advance.
The position of the column in the table declaration has nothing to do with its being NULL or NOT NULL.
If you are adding a column (of any type) which you want to be NOT NULL, i.e. you want to prohibit NULL values in that column, and the table already contains some rows, you must also provide some default value. For example:
ALTER TABLE Personal_Details
ADD COLUMN DOB datetime NOT NULL DEFAULT (GETDATE())
Otherwise the engine will attempt to add that column with NULLs as its values, which will violate the NOT NULL property, and the change, therefore, will be reverted.
Basically, the same applies when you want to set an existing column's NOT NULL property on while the column already contains NULLs. But in this case you must explicitly eliminate the NULLs before the change by either replacing them with values or removing the respective rows.
Source:
ALTER TABLE (Transact-SQL). (The particular section related to your problem is just above this code snippet.)
1)For ur adding column with not null problem
Use
ALTER TABLE Personal_Details ADD COLUMN DOB datetime NULL
Update the DOB column with the required dates and make sure there is no null in the column
then alter the column using
ALTER TABLE Personal_Details ALTER COLUMN DOB datetime not NULL
2)For your column going to the end problem...
you should not be worried...the order in which the columns are arranged doesnt matter...unless u are using a pathetic way of accessing data by column order..in which case again..u should stop accessing it by column order...
If the column order really matters you can change it using design option in the sql management table(rightclick on table >design and drag the column to its required place.)