Adding a MVC 4 controller using entity framework 5 database first - asp.net-mvc

I'm trying to configure an MVC 4 application using EF 5 against the System.Web.Providers Membership DB. The ADO.NET Entity Data Model appears correct. The classes generated by the EF 5.x code generator look ok, but do not have a [Key] attribute. When I try to create a User Controller, I get the following error:
Microsoft Visual Studio
Unable to retrieve metadata for 'Citrius.Admin.User'. One or more validation errors were detected during model generation:
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Membership' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'Profile' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Memberships' is based on type 'Membership' that has no keys defined.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'Profiles' is based on type 'Profile' that has no keys defined.
I thought I saw a walkthrough of this, using these versions, but cannot find it. I've tried to find a solution but all the examples are of previous versions.
Am I too far out on the bleeding edge?? Any help would be appreciated...

Why dont you assign the Key manually?
Go into each one and add [Key] above the field that should be the Key.
That should get rid of these errors.

Related

ASP.NET Identity 2.0 DB First Approach: Adding new Columns

I am trying to use ASP.NET Identity 2.0 with existing database.
I have created an MVC project (which uses Individual Account Authentication), then I registered for a use in order to create the DB.
Then:
I created scripts for the necessary tables and added them to my own DB
I added ADO.NET Entity Data Model (database first) which include my tables plus identity tables.
I ran the application and registered for a user, everything is going fine.
Now, I need to add a relation to AspNetUser table.
I added the Column LocationId with the relationship in DB.
I Added the following to the Application User Class:
public virtual Region Region { get; set; }
Then, I updated my Model and run the application, when I tried to register for new user, I got the following error:
AspNet UserLogin: EntityType: EntitySet 'AspNetUserLogins' is based on type 'AspNet UserLogin' that has no keys defined.
How is it possible to continue using DB First Approach in this scenario?
Identity uses Code First approach for making Identity System make customization as more as possible and you are using DB first approach for your common data access. So there are 2 contexts, one is for your data and other is for you Identity. You need to write Identity classes and make a code first migration of Identity context class by typing in the Package Manager Console as:
`Enable-Migrations -ContextNameType [Your_Identity_Context]
This will enable code first migrations just for your Identity context type. If you want to add region property in your user table, then in 'ApplicationUser' (or any class derived from IdentityUser) add the required region property and then apply the migrations to update the user table in the database.
Generating SQL script and applying to the database is not a good approach.

VB entitytype has no key defined. define the key for this entitytype

I am asking for a solution for VB not C#. Hence the "VB" tag at the start of my question.
Trying to create a controller for my model, however, I keep getting "entitytype has no key defined. define the key for this entitytype". I am using VB MVC5.
The solution is to import dataannotations and dataannotations.schema to your model, compile it and then add your controller. Also, make certain that your class has a int property.

Error during running MVC app

Please tell me the meaning of this error?
{"One or more validation errors were detected during model
generation:\r\n\r\nMvcDemo.Models.Employee: : EntityType 'Employee'
has no key defined. Define the key for this EntityType.\r\nemp:
EntityType: EntitySet 'emp' is based on type 'Employee' that has no
keys defined.\r\n"}

EF 4.1 Database first. "EntityType <Entity> has no key defined."

I have created a VS 2010, .Net 4, MVC 4 application with Entity Framework 4.1 and MS SQL Server 2008.
The application is split into an MVC web project, a services project and a number of entities projects. Each entity project contains an edmx file and a context. The edmx files have been generated from the database, not code first.
My application compiles and runs successfully until I access a page which references one specific edmx file then I get the errors:
System.Data.Edm.EdmEntityType: : EntityType '<Entity>' has no key defined. Define the key for this EntityType.
and
System.Data.Edm.EdmEntitySet: EntityType: EntitySet <Entity>s is based on type <Entity> that has no keys defined.
The edmx contains 22 tables and I get this message for 8 of them.
The tables all contain primary keys in the database and these appear to have been brought through to the edmx as the edmx shows the key symbol by the correct fields and if you view the properties of the fields Entity Key = True. All of the other entity projects work correctly and some of them contain some of the same tables that are causing problems here.
I have deleted some of the offending tables and re-added them, but this didn't help. I am loathed to delete the whole edmx and start again, as it took quite a while to get right and this might not fix the problem.
Can anybody help me with this problem please? Thank you.
Let's assume a property Id maps to your primary key, then you have to decorate its property with [key] attribute like this:
[Key]
public int Id { get; set; }

Regarding Foreign Key in MVC(3.0) Razor

I am making an application using MVC 3.0 Razor and Code First Entity Framework.
I am wondering how to define primary key and Foreign Key Relation in the Model.
I know how to define Primary Key.using Key attribute.
for example, I define [Key] attribute in the Model Table Class for Primary Key.
But, really don't know how to define Foreign Key for column in class.
Please Help..................
You can just make a List<ChildType> property in the parent class and a ParentType property in the parent class.
EF will generate the key automatically.

Resources