1

I am trying to make my own small application in VS MVC ASP.net using Entity Framework. Solution consists of class library and ASP MVC application that uses those classes. Using Entity Framework I created classes from database, including Context class, all is as EntityFramework created it, I did not edit source code.

I did this before and it always went pretty smooth. Table in database tbAttribute has "normal" columns and one identity column which is auto-increment and primary key. As I did before in my smaller test application, MVC generated View to Create new row for this table but this time, it also used ID column which should not be in Create, Detail or Update View at all! It does not let me leave this field empty as it is "required".

I did this before, and MVC never generated identity columns in Views. So, what could cause this time for that to happen? How can I configure my application to ignore ID columns automatically as before (not to use any bind exclude etc)? In short: why would MVC suddenly use identity columns in Views? Table is created by script similar to this (original script is longer):

CREATE TABLE [dbo].[tbAttribute](
[AttributeID] [int] IDENTITY(1,1) NOT NULL,
[ShortName] [nchar](50) NOT NULL,
[DisplayName] [nchar](150) NOT NULL,
CONSTRAINT [pk_Attribute] PRIMARY KEY CLUSTERED 
(
[AttributeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
Liam
  • 27,717
  • 28
  • 128
  • 190
Biljana M.
  • 359
  • 1
  • 4
  • 22
  • You are editing data so you should be using a view model, not the data model - refer [What is ViewModel in MVC?](https://stackoverflow.com/questions/11064316/what-is-viewmodel-in-mvc) –  May 08 '18 at 10:51
  • 1
    Is this code first? It looks like your EF and SQL are out of sync. You probably need to regenerate your EF classes (presuming this is not code first?) so it recognises that the ID column is an IDENTITY insert – Liam May 08 '18 at 10:51
  • Database already existed when I started application, I used EF to generate entity data model from database. It generated context class and all the classes from DB and also connection string. – Biljana M. May 08 '18 at 11:05
  • Maybe it's the name of ID column? If it's not called "Id", then there has to be a way to mark the column as "id" and not to mess up the code EF created??? – Biljana M. May 08 '18 at 11:54
  • @Biljana M As long as the name ends with ID (case is not important) EF will interpret the column as either a primary or foreign key field. – Philip Smith May 08 '18 at 12:34
  • @ Philip Smith in this case, that did not work. I am still wondering why. – Biljana M. May 08 '18 at 13:24
  • Does EF know that the columns are database-generated? (Check in the edmx designer or in the mapping configuration, depending on how you generated the model). – Gert Arnold May 09 '18 at 10:27

0 Answers0