Asp.net Mvc Scaffolding Has No Key Defined Database Generated Model
- Asp.net Mvc Scaffolding Has No Key Defined Database Generated Model Of Life
- Asp.net Mvc Scaffolding Has No Key Defined Database Generated Model Of Data
By default, Entity Framework assumes a key property called Id exists in your model class. Your key property is called CustomerID, so Entity Framework can't find it. Either change the name of your key property from CustomerID to Id, or decorate the CustomerID property with the Key attribute. Jun 09, 2016 @ar27111994 Do your projects OpenBizBLL and OpenBizDal both contain the type OpenBizBLL.Entities.Inventory.Brand? If that is the case, then please rename one of them (move to a different namespace) Also currently there is an issue with scaffolding, that it doesn't support model classes outside of the current project properly.
-->A key serves as a unique identifier for each entity instance. Most entities in EF have a single key, which maps to the concept of a primary key in relational databases (for entities without keys, see Keyless entities). Entities can have additional keys beyond the primary key (see Alternate Keys for more information).
By convention, a property named Id or <type name>Id will be configured as the primary key of an entity.
Note
Owned entity types use different rules to define keys.

You can configure a single property to be the primary key of an entity as follows:
You can also configure multiple properties to be the key of an entity - this is known as a composite key. Composite keys can only be configured using the Fluent API; conventions will never setup a composite key, and you can not use Data Annotations to configure one.
Primary key name
By convention, on relational databases primary keys are created with the name PK_<type name>. You can configure the name of the primary key constraint as follows:
Key types and values
While EF Core supports using properties of any primitive type as the primary key, including string, Guid, byte[] and others, not all databases support all types as keys. In some cases the key values can be converted to a supported type automatically, otherwise the conversion should be specified manually.
Key properties must always have a non-default value when adding a new entity to the context, but some types will be generated by the database. In that case EF will try to generate a temporary value when the entity is added for tracking purposes. After SaveChanges is called the temporary value will be replaced by the value generated by the database.
Important
If a key property has its value generated by the database and a non-default value is specified when an entity is added, then EF will assume that the entity already exists in the database and will try to update it instead of inserting a new one. To avoid this turn off value generation or see how to specify explicit values for generated properties.
Alternate Keys
An alternate key serves as an alternate unique identifier for each entity instance in addition to the primary key; it can be used as the target of a relationship. When using a relational database this maps to the concept of a unique index/constraint on the alternate key column(s) and one or more foreign key constraints that reference the column(s).
Tip
If you just want to enforce uniqueness on a column, define a unique index rather than an alternate key (see Indexes). In EF, alternate keys are read-only and provide additional semantics over unique indexes because they can be used as the target of a foreign key.
Alternate keys are typically introduced for you when needed and you do not need to manually configure them. By convention, an alternate key is introduced for you when you identify a property which isn't the primary key as the target of a relationship.
You can also configure a single property to be an alternate key:
You can also configure multiple properties to be an alternate key (known as a composite alternate key):
Finally, by convention, the index and constraint that are introduced for an alternate key will be named AK_<type name>_<property name> (for composite alternate keys <property name> becomes an underscore separated list of property names). You can configure the name of the alternate key's index and unique constraint:
Using MVC, Entity Framework, and ASP.NET Scaffolding, you can create a web application that provides an interface to an existing database. This tutorial series shows you how to automatically generate code that enables users to display, edit, create, and delete data that resides in a database table. The generated code corresponds to the columns in the database table.
This tutorial focuses on using ASP.NET Scaffolding to generate the controllers and views.
In this tutorial, you:
- Add scaffold
- Add links to new views
- Display student views
- Display enrollment views
Prerequisite
Add scaffold
You are ready to generate code that will provide standard data operations for the model classes. You add the code by adding a scaffold item. There are many options for the type of scaffolding you can add; in this tutorial, the scaffold will include a controller and views that correspond to the Student and Enrollment models you created in the previous section.
Asp.net Mvc Scaffolding Has No Key Defined Database Generated Model Of Life
To maintain consistency in your project, you will add the new controller to the existing Controllers folder. Right-click the Controllers folder, and select Add > New Scaffolded Item.
Select the MVC 5 Controller with views, using Entity Framework option. Nazi zombies free download mac. This option will generate the controller and views for updating, deleting, creating and displaying the data in your model.
Select Student (ContosoSite.Models) for the model class and select the ContosoUniversityDataEntities (ContosoSite.Models) for the context class. Keep the controller name as StudentsController.
Click Add.
If you receive an error, it may be because you did not build the project in the previous section. If so, try building the project, and then add the scaffolded item again.
After the code generation process is complete, you will see a new controller and views in your project's Controllers and Views > Students folders.
Perform the same steps again, but add a scaffold for the Enrollment class. When finished, you have an EnrollmentsController.cs file, and a folder under Views named Enrollments with the Create, Delete, Details, Edit and Index views.
Add links to new views
To make it easier for you to navigate to your new views, you can add a couple of hyperlinks to the Index views for students and enrollments. Open the file at Views > Home > Index.cshtml, which is the home page for your site. Add the following code below the jumbotron.
For the ActionLink method, the first parameter is the text to display in the link. The second parameter is the action and the third parameter is the name of the controller. For example, the first link points to the Index action in StudentsController. The actual hyperlink is constructed from these values. The first link ultimately takes users to the Index.cshtml file within the Views/Students folder.
Display student views
You will verify that the code added to your project correctly displays a list of the students, and enables users to edit, create, or delete the student records in the database.
Right-click the Views > Home > Index.cshtml file, and select View in Browser. On the application home page, select List of students.
On the Index page, notice the list of the students and links to modify this data. Select the Create New link and provide some values for a new student. Click Create, and notice the new student is added to your list.
Back on the Index page, select the Edit link, and change some of the values for a student. Click Save, and notice the student record has been changed.
Finally, select the Delete link and confirm that you want to delete the record by clicking the Delete button.
Without writing any code, you have added views that perform common operations on the data in the Student table.
You may have noticed that the text label for a field is based on the database property (such as LastName) which is not necessarily what you want to display on the web page. For example, you may prefer the label to be Last Name. You will fix this display issue later in the tutorial.
Display enrollment views
Your database includes a one-to-many relationship between the Student and Enrollment tables, and a one-to-many relationship between the Course and Enrollment tables. The views for Enrollment correctly handle these relationships. Navigate to the home page for your site and select the List of enrollments link and then the Create New link.
The view displays a form for creating a new enrollment record. In particular, notice that the form contains a CourseID drop-down list and a StudentID drop-down list. Both are populated with values from the related tables.
Furthermore, validation of the provided values is automatically applied based on the data type of the field. Grade requires a number, so an error message is displayed if you try to provide an incompatible value: The field Grade must be a number.
Asp.net Mvc Scaffolding Has No Key Defined Database Generated Model Of Data
You have verified that the automatically-generated views enable users to work with the data in the database. In the next tutorial in this series, you will update the database and make the corresponding changes in the web application.
Next steps

In this tutorial, you:
- Added scaffold
- Added links to new views
- Displayed student views
- Displayed enrollment views
Advance to the next tutorial to learn how to change the database.