Cannot generate ADO.NET Entity Data Model, model class is empty - entity-framework

So I follow all the steps to create the ADO.NET Entity data model, right click the Model folder, click Add New Item -> ADO.NET Entity Data model -> Generate from Database, and then select the database connection and enter the namespace of my models, and click Finish. However the edmx view looks like this:
I built the solution, still the same. And after the build, the model class is not generated, I can only see:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
There is no class definition is found here. I checked SQL Server, the table and its data are there. What have I missed?

The table or view does not have a primary key defined and no valid primary key could be inferred. For this reason, this table or view has been excluded. To use the entity, you will need to review your schema, add the correct keys.

If your table has been accidentally removed, add it back to the DBML file and recreate the ADO.NET Entity Data model.

Related

DB-First Entity Framework context not showing my tables

This is my first time dealing with Entity Framework. I add the ADO.NET Entity Data Model class -> Choose the database to use -> Choose the tables I want -> Name / Namespace -> Finish
When I look at the class in the solution explorer, there's no tables showing.
When I instantiate it, it doesn't show my tables as options.
Am I missing something?
Choosing tables
Empty Diagram once finished loading
Breakdown of class
I got the same problem.
Here is the solution.
In my case, there were some warnings about the primary key. That primary keys are not defined in tables so. What I did I define the primary keys and then update the table. Now I have the diagrams.

Cannot add Postgresql view to EDMX File

I ensured my tables all have primary keys which are not null fields. I ensured my views contain one of these primary key fields. Nothing works.
When I try to add the views in the wizard, Visual Studio returns me to the EDMX diagram view but the views I just added are not there! This below shows when returned to the diagram view:
The model was generated with warnings or errors.PMCPolyModel.edmx Please see the Error List for more details. These issues must be fixed before running your application.
Loading metadata from the database took 00:00:00.5619194.
Generating the model took 00:00:00.5230463.
Added the connection string to the App.Config file."
The internal error is:
Error 6013: The table/view '[My View Name]' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it."
I closed and reopened Visual Studio and the EDMX diagram view states "The Entity Data Model Designer is unable to display the file you requested. You can edit the model using the XML Editor."

Entity Framework - Entity not added to design surface

I attempted to update my edmx file by selecting a table. The tool spit out a info message that said the table did not have a primary key.
The entity did not get added to the design surface but it did get added to the .edmx file. In addition, using the model browser I see an Entities.Store and an Entities. My table got added to Entities.Store, but not to Entities.
I cannot access the table that was "added" in the code.
What do I do?
Steps to reproduce:
Create a SQL table with two columns that are both defined as foreign keys to other tables. Make sure the tables that the FKs point to already exist in the model on the design surface.
Right click and choose Update Model from Database...
Next. Under the Add tab, mark the new table under Tables
Click Finish.
An association will be created and it will be selected on the design surface, but it won't start with FK_, it will just be the name of your table. Go to the Model Browser and look under Entity Types. The table will not be there. Look under Associations and you will see your table name there as an association, but it will look out of place (because of the name).
Entity Framework was too smart for me. It created an association instead of an entity. Odd, but it works for how I need to use it.

Entity framework: Database first/Code first hybrid

I am trying to create a custom Entity Framework (4.2) entity that would be mapped to my database like it would be done in a Code first approach.
The issue is that my entity framework data model is using Database first.
How can I add my custom entity to entity framework's context?
If by the Database first you mean that you already have EDMX created from exiting database you simply cannot use code first. You must create table and update model (EDMX) from the database to include it in EDMX.
Edit based on comment:
I want to create a BriefUser entity that would basically be a lighter
version of User but it would have properties retrieved from User's
foreign keys.
Well this is possible. You can either create BriefUser as common class and use projection in query.
var breifUser = (from x in context.Users
where ...
select new BriefUser
{
// Fill BreifUser's properties here
}).FirstOrDefault();
You can even refactor former code to reusable extension method:
public static IQueryable<BriefUser> ProjectUser(this IQueryable<User> query)
{
return query.Select(x => new BreifUser()
{ // Fill BreifUser's properties here });
}
and use it like:
var briefUser = context.Users.ProjectUser().FirstOrDefault(...);
It is also possible to define your new class as "entity view". The first problem is that each table can be mapped to only one entity (except some advanced concepts like inheritance or splitting) so you cannot define your BriefUser as a new entity type because mapping both User and BriefUser to UserTbl would violate this rule. You must use special construct called QueryView.
QueryView is view in mapping level. It allows you to create new mapped type which is projection of existing mapped entities defined directly in EDMX's MSL part. The projection is defined as custom Entity SQL query. The problem is that QueryView has limitations:
It doesn't offer all Entity SQL features - for example it doesn't support aggregations (which I consider as really missing feature). Without aggregations you for example cannot create a new type which will contain property counting some related entities.
It is not supported in designer. You must edit your EDMX as XML to define QueryView and you must write Entity SQL query yourselves.
Resulting type is a "view" and it is read-only.
I want to keep the EDMX file, but also be able to add an entity
(BriefUser) to EF's context.
This is not possible. Your BreifUser is only projection / view and EF is not able to track changes back to originating tables so you cannot add BreifUser to context and persist it. In case of QueryView you can achieve it if you define custom stored procedures which will no how to decompose BreifUser and modify all related tables. These stored procedures must be imported to the EDMX and mapped to data modification operations of the view entity. Btw. same will happen if you map your entity to the database view because EF takes all views as read-only.

MVC Entity Type Missing

I'm brand new to ASP.NET MVC (Literally just two days of learning on the fly). I'm trying to create a website using MVC, however, I've screwed something up in my solution.
I created this table that had foreign key constraints but I initially forgot to set up a primary key on the id field and forgot to set it identity specification to yes. So when I updated my DataModel and brought this table in, it showed every field as being the primary key and it showed the relationship to the other tables. So I went back to the database and changed the id field for the table to be the primary key and identity specification to yes.
I went back to the DataModel, tried to Update Model From Database, went to the refresh tab, expanded the tables, and selected the table I wanted to refresh, then click on "Finish".
The data model appeared to make the changes but it showed the relationships to the other tables twice.
So I thought I'd delete the table (entity) from the DataModel diagrahm and bring the table back in through Update Model From Database. But when I went back to the Update Model From Database...
Under the Add tab, the table isn't under the Tables
Under the Refresh tab, the table is listed, but when I select it and click on "Finish" the table doesn't reaapear in the diagrahm.
In the model browser...
Under Models
Entity Types, The entity isn't listed
Under Associations, The relationships no longer appear (obviously because the entity is gone).
Under Models.Store
Tables/Views, The table is listed.
Constraints, the relationships are shown.
I've tried dragging the table over to the diagrahm from the Model Browser, but that doesn't appear to work.
I've tried to right click in the diagrahm, add Entity but my table doesn't appear as a Base Type.
I've tried to google but I'm coming up with nothing. With this entity missing, I cannot even build my solution. Please help.
Try to delete the entity file and add it again. But you have to name it as previous entity and don't forget to delete the connectionString in web.config before add it.
If you don't know what I am saying just let me know.