This was working with EF5 but there seems to be an issue with EF6.
We are trying to save a new row to a table using EF6. The primary key column is marked as an Identity value and we have an insert trigger on the table.
When we call db.saveChanges() it always tries to use 0 as the primary key column value.
If we insert a record through Toad it uses the trigger correctly. So the trigger is working.
If we change the trigger to check for 0 instead of null then it also works.
Has anybody else had this issue with EF6?
Thanks,
Joe
With 12c it works fine
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Number { get; set; }
Related
How to add auto-increment column in AspNetUsers table while implementing asp.net identity? I do not prefer to change much the existing code and any simple workaround is also fine for me.
If you are just looking for a column which value will auto increment (as per your question) then just add a column to the AspNetUsers table and make it auto incremental.
Personid int NOT NULL AUTO_INCREMENT,
By default, the starting value for AUTO_INCREMENT is 1, and it will increment by 1 for each new record.
To add an autoincrement column named "PersonId" in AspNetUsers table:
public class AppUser : IdentityUser
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string PersonId { get; set; }
}
Then add-migration and update-database.
I want to retrieve data from my sql server. in the table, contained 1 column with date format.
I have succeed retrieve the value for the other column it that table. However, the time_stamp column value is missing.
Is there anyone have this issue before? I dont know why the value for this column is not displayed
here is the detail:
model
public Nullable<System.DateTime> time_stamp { get; set; }
view
<td>
#Html.DisplayFor(modelItem => item.time_stamp)
</td>
controller
model.feedback = entities.FEEDBACKS.ToList();
I think the problem related to the data format, might be in the model or view.
Because the other column is display correctly.
I have solved this issue. It is because I use the connection created from ADO connection to edmx. And I add the time_stamp manually. So the connection is not updated.
Solution --> rebuild the edmx connection.
I have an MVC application using Code First data migrations and now, long after making a table, I'd like to change the PK column to not be an auto-generated Identity column. But I get the impression I just can't do this without having CodeFirst delete the table somehow (which will be hard given all the dependencies) and recreate it? Really wishing I didn't have to do that.
What I've tried:
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public int AssessmentID { get; set; }
and this in OnModelCreating():
modelBuilder.Entity<Assessment>()
.Property(e => e.AssessmentID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
When doing add-migration, it doesn't add anything. I tried manually adding this line but it does not update the db:
AlterColumn("dbo.Assessment", "AssessmentID", c => c.Int(nullable: false, identity:false));
I've concluded that this is indeed not possible. Like the post below says, there is no SQL Alter command to change the Identity status on a column, so CodeFirst can't create one. I plan to remove all migration files and rescaffold (things have gotten messy in other ways).
Remove Identity from a column in a table
Hi I am trying to create a new customer record. The primary id customerid is autoincremental column.
When I try to insert a new record I get this error :
Message=Cannot insert explicit value for identity column in table 'Customer' when IDENTITY_INSERT is set to OFF.
This is the same code I am using :
NewCustomer.Name= user.Name;
NewCustomer.Address= user.Address;
NewCustomer.Phone = user.Phone;
NewCustomer.Email= user.Email;
db.Customers.Add(NewCustomer);
db.SaveChanges();
I read somewhere that I have to set autoincrement on, but I am using entity framework and not any direct sql.
Can some one please help how I could turn on autoincrement so I could insert records?
Is it possible to alter tables in code-first migrations with nocheck constraints? I haven't found any way to do this except of getting SQL script via Update-Database -Script and modifying the sql statements.
You can try creating your own MigrationSqlGenerator and use it with migrations (you can set custom SQL generator in DbMigrationsConfiguration) - I think it should be enough to inherit SqlServerMigrationSqlGenerator and add NOCHECK after adding foreign key = overriding Generate(AddForeignKeyIperation operation).
Anyway using NOCHECK with EF is way to disaster. If you want to have database without checked constraints don't use EF because it will crash every time it reaches inconsistency.
Another way to do it for one-off occasions is to call the SQL for creating the FK manually, like so, from within your Up() method:
// AddForeignKey("dbo.EFElementGroupEntries", "ConstraintCode", "dbo.EFElementConstraints", "Code");
Sql(#"ALTER TABLE [dbo].[EFElementGroupEntries] WITH NOCHECK
ADD CONSTRAINT[FK_dbo.EFElementGroupEntries_dbo.EFElementConstraints_ConstraintCode] FOREIGN KEY([ConstraintCode])
REFERENCES[dbo].[EFElementConstraints]([Code])");
Sql(#"ALTER TABLE [dbo].[EFElementGroupEntries] CHECK CONSTRAINT [FK_dbo.EFElementGroupEntries_dbo.EFElementConstraints_ConstraintCode]");
If the goal is to create a required foreign key but update-database fails due to existing data, just do it in two stages. For example, suppose I want to add a foreign key for TEAM in a PLAYER class for a 1:m relationship where all players must be assigned to a team. Assuming all classes have integer identity primary keys...
1. Add the usual two properties to PLAYER but use a nullable int for the foreign key. ie:
public int? TeamID { get; set; }
public virtual Team Team { get; set; }
Add a migration and update the database. All of the TeamID values will be null but you will be allowed to create the relationship.
Next, fix your data so every player is assigned a valid TeamID. If you are seeding player data, you will need to supply TeamID values there as well.
Modify the PLAYER class so the int is no longer nullable:
public int TeamID { get; set; }
Add another migration and update the database again. You should be good to go.