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
Related
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
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 have a fact table with 8 foreign keys (referencing 8 dimensions), but even a combination of all eight keys does not uniquely identify a row. Do I need to add another attribute from the original data (i.e. "project-id" attribute, which is useless for anything), so that I can have a primary key, or I can leave fact table as it is, without a primary key?
The first rule of a fact table is to declare your grain - what uniquely identifies a row.
It sounds like you haven't declared your grain for this table. If the grain of the table is "one row per project", then you need to include project as a degenerate dimension in the table.
Every table must have a primary key. That's relational rule #1.
You can always add a surrogate key, but I like the idea of a fact table having attributes that satisfy a unique constraint. I second your idea: add more attributes until you have a unique constraint.
Along with those 8 foreign key include a simple surrogate key (like a row index) to each row. This will identify every row of the fact table uniquely
For a surrogate key you may start from an index say 1 for the first row and then increment the index by one each time you make a new entry to the fact table
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.
I have a table with a key and another field named "CardId" .
Now I want to make CardId unique. I mean just unique value should be store in CardId.
How can I do it?
thanks
Assuming you use SQL Server, you can simply add an unique index to your table, e.g.
CREATE UNIQUE NONCLUSTERED INDEX [IX_TableName] ON [dbo].[TableName]
(
[CardId] ASC
) ON [PRIMARY]
GO
This will ensure that the column only allows unique values to be stored.
As far as I'm aware, the only way to specify a column as "unique" in EF4 is to flag it as the Entity Key (please correct me if I'm wrong), but this could lead to some confusion down the line as your Entity Key should ideally map to the primary key of your table.