Unable to retrieve metadata for model. One or more Validation errors were detected during model generation: table has no key defined - entity-framework-4.1

Following on from this question here:Entity Framework Reverse Engineer using Power Tools - No Primary Keys
I've reverse engineered a database using Entity Framework Power Tools Beta 2.
This has created a large number of POCO's and a mappings folder with the entity mappings.
When I attempt to create a controller using the Add Controller Dialog I get the following message box come up:
Unable to retrieve metadata for cruise model. One or more Validation errors were detected during model generation:
\tSystem.Data.Entity.EmdEntityTypes:: cruise table has no key defined. Define the key for this EntityType.
This repeats many times for all the related tables to the original one for which I was trying to create a controller.
The Controller itself is never created and on pressing OK I'm returned to the Add Controller Dialog.
The model in question definitley has a primary key defined in it's mapping file:
// Primary Key
this.HasKey(t => t.cruise_ID);
What am I doing wrong?
How does the controller find the mapping classes?

I've been able to replicate the error your receive, and it's related to trying to create a controller for the Model itself, rather than for the entity type.
The MVC scaffolding is designed to target an entity, then the related context - are you sure you're selecting the right items in the drop down?
In This image:
Your model drop down (Mine shows A (MVCExamples.Models)) should refer to what I assume is your cruise entity.
Your Data Context should be the DbContext that gets created (Mine shows Entities (MVCExamples.Models))

ADD :
using System.ComponentModel.DataAnnotations;
and a primary key to db class is calling'

Related

Yii2: How to generate CRUD from MySQL/MariaDB view automatically with Gii

I can quite easily generate a model from MySQL/MariaDB view with Gii, but when I try to generate CRUD, than I recieve the following error message:
The table associated with frontend\models\MyModel must have primary key(s).
See also the discussion in Yii Framework Forum.
The solution is:
add an ID in the view, e.g. using the CONCAT function,
overwrite the method primaryKey in the generated model.
Here is the code:
public static function primaryKey()
{
return array('view_id');
}
It is not a standard solution and should be used carefully. Yii does not officially support using active record with views, because different DBMS have different view specs and they usually don't support DB write (2).
Working on yii2,
There was issue with primary Key, so I search to add primary key in VIEW TABLE but I can't because -
Create view with primary key?
Adding in a primary key to an SQL view
Then moved to create CRUD on VIEW Table. I view many articles like-
http://www.yiiframework.com/forum/index.php/topic/9544-create-a-model-and-crud-from-mysql-view-instead-of-table/
https://code.google.com/archive/p/yii/issues/1274#c0
so finally,
public static function primaryKey()
{
return array('my_view_id');
}
Worked for me.

Inaport Lookups can't find target entity - CRM4 Connector

The documentation for inaport states you can just map lookup fields and it will work out what types they are.
I am mapping from CRM 4 to CRM 2011 (using the CRM Connectors), however all my lookups fail with
A lookup value was mapped to account.{field name} but no target entity name was supplied and no default is available.
I have to fall back to adding a custom field, checking if their is a lookup id in the field, and then making a lookup value as per the documentation of guid::entityname using expressions which is painful.
Is this feature working for anyone else? Do i need to set up a child-parent relationship? I only ever add a map for the entity I'm working on.
Inaport will try to work out what the correct entity reference is and default it. For example, if the lookup is the foreign key in a child table, the entity reference will default to the parent.
There are some circumstances where a lookup may reference multiple entity types, and Inaport cannot infer the correct type. For example, and activity "regarding" lookup may reference 12 different entity types.
It could do a better job when a custom lookup is only referencing a single entity type, and a change request has been put into the system.
As you noted, when Inaport does not correctly infer the entity type you can force it by appending "::entityname" to the GUID you are mapping to the lookup field. This is discussed in more detail in the help.
HTH
Regards
David Evans

Entity Framework 4.2 - How to realize TPT-Inheritance with Database-generated Primarykey Value?

I want to use the EF (4.2) in the following scenario:
There exists a database already (so I chose the database-first approach) and it is a SQL Anywhere DB.
I want to use persistence-ignorant business objects, so I use the DbContext Template to generate POCO classes from the EDM.
There is one simple inheritance hierarchy among my entities: an abstract base entity and two concrete derived entities.
In the database there is one table for each type of the inheritance hierarchy (Table-Per-Type Strategy).
Each of these three tables has a primary key column (Id, type:integer), and the association of a concrete entity to the base entity is done by having the same Id in both tables (that means that the primary key (Id) of the concrete type tables is at the same time a foreign key to the base table; a pretty common approach I think).
I had to define the Inheritance manually in the designer, since the EDM assistant does not automatically recognize, that is want to have an inheritance association between the described entities.
Until this point there wasn't any bigger problem. Now to the issue at hand:
There is a restriction for the database I use: Primarykey values have to be generated by the database, using a database function.
I want to call this function in a before-insert-trigger defined on the base-table.
To let the entity framework know that a value is generated by the database, I set the StoreGeneratedPattern property of the Id Property of the base-entity to Identity (As I understood, this is the way to tell EF to get the generated value after inserting a new instance of an entity).
When I create a new instance of a derived entity, add it to the corresponding DbSet of the DbContext and call SaveChanges on the context, a DbUpdateException is thrown, stating that a foreignkey constraint is violated.
By checking the request-log of the DB, I see that the base entity got inserted in the base table, but on inserting the row in the derived table, the above mentioned error occurs, because it obviously doesn't use the newly generated Id of the new entry in the base table.
Since I don't think there is much I can do on a database level against that, the question is, if the EDM or DbContext can be configured (or modified) to insert the base row first, then take the generated Id and use it for insertion of the derived row.
I know there are several way to avoid this situation (not using inheritance, using a stored procedure to insert a new derived entity, calling the id-generating db-function before inserting and set the Id property myself on the entity), but at the moment the above-described behavior would be the most preferable, so I want to make sure not to overlook something before deciding for any "plan B".
Any suggestions on this topic are much appreciated,
Thanks in advance.
Here is the code of the trigger:
ALTER TRIGGER "TRG_GENERATE_ID" before insert order 1 on
BASE_TABLE
referencing new as NewEntry
for each row
begin
declare NewID integer;
set NewID = F_GET_NEW_ID('BASE_TABLE', NewEntry.SOME_OTHER_ID);
set NewEntry.ID = NewID
end
The function "F_GET_NEW_ID" is called in the trigger to generate the new ID for a new entry in the base table. It has two parameters:
"Tablename" -> The name of the table for which a new ID should be generated,
and a second parameter that takes the value of a standardcolumn in all tables of the database (it is required to generate the new ID).

Navigating by foreign keys in ADO.NET Entity Framework/MySQL

I am using ASP.NET MVC2 on top of a MySQL database in VS2008. I am using the MySQL ADO.NET connector 6.2.3 to provide the connection for the ADO.NET Entity Data Model.
This is mostly working ok, however navigating via foreign keys is causing me a real headache!
Here is a simplified example..
Car (Table)
CarID PK
Colour
Doors
ManufacturerID FK
Manufacturer (Table)
ManufacturerID PK
Name
In the edmx file I can see the 1-many relationship shown as a navigation property in the both the Car and Manufacturer tables. I create a Models.CarRepository that allows me to returns a IQueryable.
At the View I want to be able to display the Manufacturer.Name for each car. This is not accessible via the object I get returned.
What is best way to implement this? Have I encountered a limitation of the Entity Framework/MySQL combination?
Eager loading of the related records needs to be enabled in the Model Repository. Something like:
var allCars = from c in automobileEntites.Car.Include("Manufacturer")
select c;
This then makes the related records available for subsequent query/display.

MVC 2 EditorForModel() rendering foreign key table names

I have an interesting problem and wanted so see if anyone else has seen this. I've created a MVC 2 site using Visual studio 2010 beta 2. I'm using linq to sql data model objects with data annotations.
In my data model objects I'm using [ScaffoldColumn(false)] attribute to exclude the foreign key ID's from rendering to the UI when I use the EditorForModel method. For some reason the UI is rendering the foreign key table name.
e.g. if the foreign key is AccountID, i see the "account".
I wonder if this is a bug in the editorformodel or if I need to use a different/additional data annotation attribute to instruct editorformodel to not render anything.
You Linq to SQL classes will have a property for the foreign key itself (AccountID) as well as an EntitySet property for the related records in the Accounts table. If you open up the auto-generated designer.cs file under your linq to sql dbml - you can view all the properties of each class. I think by default the templated helpers are only supposed to generate an editor for the first level of properties. There is a "deep-dive" option that will extend the generated editor to more levels of properties. Check out Brad Wilson's blog on the subject (near the end of the post).
In general, trying to throw your auto-generated Linq to SQL classes into your View or annotate them with attributes gets pretty hairy. It might be worth checking into strongly typed view models where you could specify only the properties you're interested in displaying in your view.