Entity framework 4 - Table with Identity column addition - entity-framework-4

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.

Related

Edmx does not contain AspNetUserRoles table even after I have added an additional nullable column in AspNetUseRoles table

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.

Should every table have a primary key using ADO.NET?

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

Clustered Columnstore with Entity Framework

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

Why does Database First EF skip mapping some tables?

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.

Handling lookup tables in Entity Framework v1

I am just getting started with Microsoft's Entity Framework, using it for an MVC project since MS seems to be really pushing it, and I'm running into some issues. In my database there are multiple lookup tables tied to a single table via foreign keys. Within the entity framework I am trying to combine these into one so that I have a simplified single view for this data in my model. However, this doesn't seem possible from the designer view. Is there something obvious I'm missing? Is there a way that I can edit the edmx file manually to produce this sort of model?
At the moment, Foreign keys and lookup tables in Entity Framework are a PAIN.
EF with LINQ makes getting your data super-easy, and on the surface it looks easy to update, but with lookup tables things get difficult (for now... read on...)
I'm not sure how you would "combine" your lookup tables into a single table. If each table contains a different type of "lookup entity" then IMHO they should be represented separately in your EDM. I'm guessing you're having headaches updating a record's foreign keys to the lookup tables. That's because it is a headache.
Changing foreign key values:
MyDBEntities _db = new MyDBEntities();
//get a Person
MyDBEntities.Person person = (from p in _db.Persons
where p.Id = 1
select p).First();
// This sets the foreign key value in the Person table on the PersonType field
person.PersonTypeReference = new EntityKey("MyDBEntities.PersonType", "PersonTypeId", 3)
The next release version of the Entity Framework will have a new concept called "FK Associations." This will bring back the sanity of setting the foreign key value directly rather than having to create and set an EntityKey.
HTH.

Resources