entity framework without primary key - asp.net-mvc-3

I have created an entity class for my mvc application. But, my table doesn't have a primary key and the VS throws back an error:
"The table/view does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view."
Is there a way to still use the model even if it has no primary key??
or Do I have to add a pk? or should I just use execute command?
Any suggestion/comment is highly appreciated. Thanks in advance. :)

Related

How can I have a table without primary key in Magento?

In Magento, it is necessary that you create a primary key in order your grid and module to work. But, I don't have primary key in my table and don't want to create it. Is it possible to make my module work properly without primary key?
Magento is giving error Column not found: 1054 Unknown column 'main_table.modulename_id' in 'field list' as it doesn't find primary key id in the table while displaying grid.
Without auto-increment it can be done with:
$this->_isPkAutoIncrement = false; in the model. Is there anything like that for primary key as well?
I searched on net without any luck. Any help will be highly appreciated.
If you will not use model/collection for this table, you can do this. If not -- you cann't (without rewrites). See newsletter queue and queue_link tables -- there are no model for queue_link table (it has primary key, but you can use your table without it if you will wok in such way), all things are done in queue resource.
The Magento ORM is setup in a way which will require you specify a primary field. You should always have primary key anyway, I can't imagine a situation where you would want one as it would no longer be suited to a database...
I would imagine you may want a natural/composite key rather than a surrogate key, but I am not sure if that's what you meant?

Entity Framework 4 doesn't recognize referential integrity in my database

Trying to test the option of replacing our internal data access layer with Entity Framework 4 in our existing web application, I have started a new project and added an ADO.NET Entity Data model, then let it generate from the existing database.
It recognizes the tables in my database but it doesn't show any relationships between these tables, they are totally separated from each other. Is there any preconditions on the database so one can use the database first method?
DBMS is SQL Server 2005, PK and FK are defined in the database.
Thank you in advance
The foreign key constraints are part of the storage model. You can use the model browser window to view the constraints. This window is probably only available with Service Pack 1 of Visual Studio.
The Entity Framework uses a memory model and a conceptual model. These are stored in a. Edmx file. It is a common XML file that can be edited using any text editor. The foreign key constraint appears in elements like
<Association Name="FK_X_Y_NNNN">; ... </Association>
In the Model Editor foreign key references are indicated by lines between the entities. At the ends of the lines the cardinality is indicated.
If you see no foreign key constraints, then the database may not define them. Check your Database please.

MVC3/Razor Add Controller "Get-PrimaryKey" is failing to find the Primary Key

I've created an Entity Framework Model based on an existing database. The Entity Framework is using the ADO.NET DbContext Generator.
I've also created a MVC3/Razor project that is using the DLL from the first project. When I click on the option "Add -> Controller" and fill out the required fields I get an annoying error:
Scaffolding GroupController...
EMR_MasterEntities already has a member called 'Groups'. Skipping...
Get-PrimaryKey : Cannot find primary key property for type 'CHS.CCC.DataModel.Group'. No properties appear to be primar
y keys.
At C:\Users\adriangilbert\Desktop\CHS.Monitor\packages\MvcScaffolding.1.0.6\tools\Controller\MvcScaffolding.Controller.
ps1:74 char:29
+ $primaryKey = Get-PrimaryKey <<<< $foundModelType.FullName -Project $Project -ErrorIfNotFound
+ CategoryInfo : NotSpecified: (:) [Get-PrimaryKey], Exception
+ FullyQualifiedErrorId : T4Scaffolding.Cmdlets.GetPrimaryKeyCmdlet
To get around this, I need to go to the Groups.cs that was generated by Visual Studio and add 'using System.ComponentModel.DataAnnotations;' and then add [Key] to the declaration the Groups field. However this is generated code. If I recompile the Entity Framework Project, my changes will of course be lost.
So - My question is:
Am I doing something wrong that is causing Visual Studio to not be able to figure out what the Key field is, or is this just a bug with the Scaffolding Code that is preventing it from figuring out that the Key is.
I should mention that this only fails with string-based Primary Keys. If the field had been declared as an Integer, then everything works perfectly.
Here is the problematic table:
CREATE TABLE [dbo].[Groups](
[group_name] [varchar](45) NOT NULL,
[dbname] [varchar](45) NOT NULL,
[user] [varchar](45) NULL,
[CompatibilityVersion] [nvarchar](20) NULL,
...
PRIMARY KEY CLUSTERED ([group_name] ASC)
) ON [PRIMARY]
Here's My Environment:
Visual Studio 2010
Entity Framework 4.1
MVC 3
SQL Server 2008 with SP3
I believe that MVC Scaffolding, as a convention, expects that the primary key has "Id" on the property's name, but I'm not sure.
If possible, in your case I would create a surrogate key to use as the primary key for the table, so that when the group_name needs to be changed, I would not have to handle the issues that would arise.
By default EF considers property as PrimaryKey, only if it is in format of ID or {TableName}ID...in your case it should be GroupNameId or Id.
The second options is Group_name by adding/decorating [Key] Attribute above the field.

entity framework returning only one value but the list size is correct

Entity framework returning only one value but the list size is correct
I have a table that does not have primary id and I need to get or select all the values in it.
What I see is when I do the selection with linq the number of objects is correct but it is the first row over and over.
I am simply doing something like this
List<MyValueType> valuesInDB = myDb.MyValueTypes.ToList();
Problem is I may get thousands of rows (which is correct) but the rows all have the same exact data.
I am using VS 2010 and used the wizard to create my EF object.
The problem is that entity framework is not able to work with entity without a key. So if your table doesn't specify a key, entity framework will infer its own. The key created by EF is composed of all non-nullable non-binary columns.
So if you for example have single non-nullable column in your entity which have only very small set of values (like enum) you will be able to load only single entity "per value". The reason is an inner implementation of the context and the state manager which uses Identity map pattern. When data record is retrieved from database, EF will first check an entity key and tries to find an object with the same key in its internal storage. If an object is found it will use that object instead of data record retrieved (despite of different data). If an object with the key is not found a new object is materialized and added to internal storage.
That is the purpose of Identity map - object with given key should be created only once by each context. Identity map is core pattern in ORM.
I wrote about Identity map also in this question.
I would suggest searching for the word "Warning" in your EDM's designer.cs file. It might tell you if Entity Framework is having any issues with your table.
I really can't comment much in the absence of the table design. I tried replicating your problem but wasn't able to do so. Here is what I did:
Created a table with no primary key but it had a unique key on an ID column. Entity Framework was able to infer a primary key and when I fetched the data, I not only got the correct number of rows but also the corrects data in those rows.
Created a table with no primary key and no unique key. Also there was no column called ID. Entity Framework excluded this table in the EDM that was generated. Consequently I wasn't able to query this table at all.This was displayed as a warning in the EDM designer file.
It would be better if you can share the create script for your table.

ActiveRecord fundamentally incompatible with composite keys?

I have been attempting to use subsonic for a project on which I'm working. All was going quite well until I encountered a link table with a composite primary key. That is a key made up of the primary keys of the two tables it joins. Subsonic failed to recognize both keys which was problematic. I was going to adjust subsonic to support compound keys but I stopped and though "Maybe there is a reason for this". Normally active record relies on a single primary key field for every record, even in link tables. But is this necessary? Should I just give up on active record for this project or continue with my modifications?
Ruby on Rails does not support composite primary keys in model object out of the box. However, there are plugins that accomplish that, for example this.
You can have composite primary key on a join table, but Rails will not create that primary key, you have to create it manually.
See this guide.

Resources