I'm using database-first approach with Oracle. One of my table doesn't have a primary key. It only has 2 columns which are foreign keys of other tables.
I have generated model in ASP.NET MVC project from database (Add - New Item - ADO.NET Entity Data Model).
But there is a problem - I get an error:
Error 159: EntityType 'DbModel.Store.SomeTableWithoutPK' has no key defined. Define the key for this EntityType. E:\Git_repo\ZZ\ZZ.Domain\DAL\DbModel.edmx
Does this mean that each table must have a primary key? Can I avoid this? Or I will be forced to add new column with a primary key to this table? Of course there is also possibility to apply primary key to multiple column, but is it necessary?
Every table should have a primary key for database efficiency and so that you can edit records.
You don't need to create a new column for the primary key in your 2 column table
In designer, select both columns and use both together as the primary key. As long as nulls are not allowed and there are no duplicates you should be OK.
Since this is a many to many table and you are using EF, you may find later that adding a datetime column to the table with getdate() as the default value will make data maintenance easier
Related
I have read other posts regarding edmx does not show the table which contains only Foreign keys, So I have added an additional column note(nullable) to the table AspNetUserRoles. Then in edmx diagram -> update model from database. But why it still does not show the table AspNetUserRoles in the edmx diagram?
As I am new in Asp.Net so dont want to mess with keys by creating another Primary key to the table.
I have a table with a Clustered Columnstore index. This table cannot have a primary key because of the index. It actually has an ID column, but I cannot put an index on it.
Entity Framework demands a Key, otherwise it marks it as read only. How can I tell EF what the key is?
The answer is to add a key to the edmx. Open the EDMX, right click the column and select entity key
I have a table defined in a SQL Server database as follows
Columns
MID Int foreign Key
MRUOMID Int foreign Key
All columns in this table are foreign keys and referring to other entities.
I wish to add an Identity column to this table. If I do so and update the EDMX model from the database, I get the following error.
problem in mapping fragments starting at 7462. You must specify mapping for all key properties.
I need an identity column to be added which I can refer to in my Entity model further.
Please let me know on this issues.
Thanks.
While updating, make sure you include the foreign keys too.
I'm using Entity Framework 4 and with a Database First binding, and EF is not generating the entities for a few of my tables. I'm not getting any errors, and no matter how many times I select the tables to generate from the "Update Model from Database" popup menu on the design surface, the same tables are still missing from the model.
I get no errors in the wizard. They just don't get generated. Any clues?
EF requires a primary key on the table. EF will not map tables for which it can't find or derive a primary key. If all columns are nullable, it can't assume a primary key. If one or more columns are not nullable, EF will evidently derive a primary key for the table.
EF will ignore table without primary keys.
Options I can think of:
Did you check the box next to those tables?
Did you previously add them, then delete their entities but keep the cache of the tables?
If so you can remove them from entity browser window and re-add them
or manually add entities and define the table they map to in mappings window.
Perhaps tables were classified as relations instead of entities?
You can manually add the entities and choose the table they map to in mappings window.
Actually, in my case, it doesn't work because I was using a hierarchyid field as a primary key and EF doesn't work with this field type, so, it didn't import the table, because a valid PK is required.
A possibility is when you're using tables with some different field types, as hierarchy in SQL Server.
Without Primary Key Tables where Skip Automatically on EF, OtherWise You Fix a Value as Not Null.
Suppose I have 3 tables in DB for a many to many relationship:
TabA(id1, ...): Primary Key: id1
TabB(id2, ...): Primary Key: id2
TabAB(Id1, id2,..): Foreign Key: id1, id2
So when create edmx with VS 2010 from DB, I only get two entities TabA and TabB in the model because TabAB has no primary key.
How to process this case with EF?
Are you sure EF didn't just turn TabAB into a relationship? It won't appear as a table in the model if there are no other columns. EF figures out that TabAB is a join table and treats it accordingly.
If not, the best way would be to alter TabAB to have a compound primary key of both id1 and id2. If there is some reason that combination of values is non-unique, it might be good to examine why.
The common way is to handle many-to-many relationships in EF - to have three tables in storage and only two tables in conceptual model. Third table in storage contains of columns that represent the foreign keys referencing to the main tables, and the primary key of the intermediate table is built over these reference columns. Designer just hides it :)
Read more here - http://weblogs.asp.net/zeeshanhirani/archive/2008/08/21/many-to-many-mappings-in-entity-framework.aspx.