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?
Related
I have created one to many relationship between student and department tables using entity framework.But unfortunately in the student table the foreign key (DepartmentId) type I gave string but it should be int.How can i resolve this issue without losing data????????
N.B: I am using entity framework code first approach.
I think you cannot do that, considering that your DepartamentID is the PK of the table Departament, so you have to drop the old field and create a new one, something like this
AddColumn("dbo.Departament", "DepartamentID2", c => c.Int(nullable: false));
Sql(#"
UPDATE dbp.Departament
SET DepartamentID2 = DepartamentID
");
Sql(#"
ALTER TABLE dbo.Departament drop CONSTRAINT <The FK Constraint>
");
DropColumn("dbo.Departament", "DepartamentID");
RenameColumn("dbo.Departament", "DepartamentID2", "DepartamentID");
Sql(#"
ALTER TABLE dbo.Departament add PRIMARY KEY (DepartamentID)
");
Sql(#"
ALTER TABLE dbo.Departament add CONSTRAINT <The FK
constraint>
");
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; }
We had developed in the past some sites, from company presentation sites to eshops, in classic asp. All of these was developed in multilingual environment (el, en) most of them. From database view we had choose the following schema. For example, for products table we have two related tables one with no lingual depended fields and one for lingual depended fields with one to many relation.
CREATE TABLE ref_language (
Code Char(2)NOT NULL,
Name Varchar(20) NOT NULL,
PRIMARY KEY (Code)
);
CREATE TABLE app_product (
Id Int IDENTITY NOT NULL,
PRIMARY KEY (Id)
);
CREATE TABLE app_product_translation (
ProductId Int NOT NULL,
LanguageCode Char(2) NOT NULL,
Description Text NOT NULL,
FOREIGN KEY (ProductId) REFERENCES app_product(Id),
FOREIGN KEY (LanguageCode) REFERENCES ref_language(Code)
);
To recreate the product model we use stored procedures to join the two tables for the requested language.
Now we want to move into dot.net mvc model. And we are wondering if there is a better approach, most suitable in mvc model.
It depends on your requirements, but you can just create a class to load and cache the captions in memory, loading them using EF. Either use a static class or preferably the ASP.NET cache.
If you are doing dynamic stuff on the client side then expose the strings through a MVC of WebAPI controller if necessary.
To make this easier I would decouple your translations from products in the schema. Make your translations universal.
Just have a single table called app_translation with Id, LanguageCode and Translation field. Then reference the Id on any table that needs a translated caption.
To enforce referential integrity, you can also have a app_translation_identifier table with a single column and a unique constraint. Then FK from the app_translation.Id to app_tranlsation_identifier.Id. And also have a unique key on app_translation for the Id and LanguageCode.
I have a table 'UserLogin' which contains the columns: id and userid. Suppose I want to show userid as 'user' in Gridview then we write query in sql
select userid as user from UserLogin
But I am working with entity framework. How can we solve this problem in entity framework?
Usually you specify different Header text for that column within your GridView control.
But you can achieve similar functionality in Entity framework by using
select new { user = UserLogin.userid, id = UserLogin.id };
I have defined a model class with an ID column set as [Key], on an existing database table. the table has the field defined as primary key, int, not null. It is NOT and identity column.
When creating a new record in code, I am setting the ID column (to an unique value) in code before calling the SaveChanges() method.
The method returns with the error:
Cannot insert the value NULL into column 'xxx_id', table
'xxx.dbo.xxxxxx'; column does not allow nulls. INSERT fails. The
statement has been terminated.
It seems that EF assumes that the ID column is an Identity column and therefore doesn't pass the ID in the SQL call to the database.
Is there a way to define the ID column to tell EF that is it not an Identity column and to pass the value in the SQL call
You need to specify the DatabaseGeneratedOption for the property. In this case, the DatabaseGeneratedOption should be None.
See: http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.databasegeneratedoption%28v=vs.103%29.aspx
I have usually done this with fluent coniguration like:
Property(p => p.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
.. but it looks like this can also be specified with an attribute. See http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.databasegeneratedoption%28v=vs.103%29.aspx
[DatabaseGenerated(DatabaseGeneratedOption.None)]