Entity Framework 6.x Must specify mapping for all key properties - entity-framework

I am beginning playing with EF 6.x and I've got an annoying issue.
I've designed a database with simple User,Role,Permissions tables each one bond to another with classic many-to-many relation.
EF wants me to create primary keys in indirect tables (UserPermissions etc.)
but also wants me to make some mapping for those Primary Keys but I have no clue why.
Is there something wrong with my design or there Is a workaround to this issue?
Maybe Can I in some way use Unique index instead of Primary Key to satisfy EF?
There you have diagram to make it more clear:

Well, I'm embarrased that I've not came up to solution earlier.
Just added multicolumn Primary Key on columns referencing direct tables via foreign key.
e.g. CONSTRAINT pk_IdRole_IdPermission_RolePermissions PRIMARY KEY (IdRole,IdPermission)

Related

Foreign Keys generated as NOCHECK

According this documentation page (Association Relationship), it seems that CodeFluent Entities generate Foreign keys in NOCHECK mode by default on One to Many and Many to Many relations. On the other hand, the Foreign keys on One to One relations are created in CHECK mode.
I have several questions about that:
My understanding is that NOCHECK foreign keys are disabled. If so,
what is the purpose of creating all that disabled foreign keys ?
Is there a way (and an interest) to change that behaviour?
We have some One to One relations on our application but still, all the foreign keys are disabled on our database. Why is it so ?
thanks by advance.
The foreign keys gives you some informations about the scheme even if there are not enforced. Also it can be convenient during development to not get integrity errors when you update the database. In the documentation, it is said that it's for performance reasons, but I'm really not sure about this one.
Anyway, you can instruct CodeFluent Entities to check constraint by setting defaultPersistenceEnforce="true" on the project
<cf:project defaultPersistenceEnforce="true">
From the documentation
Note: By default referential integrity is not enforced for performance reasons. However, one can enable relationships enforcement globally by setting the defaultPersistenceEnforce attribute of the project node to true.
It's also possible to enforce a specific relationship instead of the whole project. One can do so by specifying the persistenceEnforce attribute to true on the relation property.

How to find out which property is used as a Foreign Key between two entities in code first approach

I'm using Entity Framework 5, code first approach. As there's no built in support for updating child entities in disconnected scenario, I'm building my own mechanism to do that. At some point I need to get the property of an entity with which it has a Foreign Key relationship with another (principal) entity. I've tried to get access to CSpace through
((IObjectContextAdapter)dbContext).ObjectContext.MetadataWorkspace.GetItems<MyEntity>(System.Data.Entity.Core.Metadata.Edm.DataSpace.CSpace)
but here I got a warning that said there's no implicit conversion between MyEntity and System.Data.Entity.Core.Metadata.Edm.GlobalItem.
I can't look for a property that has Foreign Key attribute because in most of my entities I use EF convention to get foreign keys automatically. So how one would go about finding which property is used for foreign key relationship.
Thanks to #octavioccl's post I was able to do what I want. So I was in the right path to look inside ObjectContext.

entity framework 7 mapping attributes

Are there already mapping-annotations (attributes) for mapping foreign keys?
If no, are such annotations planned?
I've seen, that there is still a fluent-api for mapping foreign keys, but I have not found mapping-attributes for this Task.
This is not implemented yet. You can track the work here https://github.com/aspnet/EntityFramework/issues/107

Composite DB keys with Entity Framework 4.0

The re-design for a large database at our company makes extensive use of composite primary keys on the database.
Forgetting performance impacts, will this cause any difficulties when working with this db in Entity Framework 4.0? The database structure is unlikely to change and I'm not looking for "philosophical" debate but what are the practical impacts?
According to Jeremy Miller, "Composite key make any kind of Object/Relational mapping and persistance in general harder." but he doesn't really say why. Is this relavent to how Entity Framework 4.0 handles keys?
No, EF4 supports composite keys just fine.
The problem is a table with a surrogate key and composite keys. You can only set a single key on each model; that key can have multiple fields, but you can only have one from the designer standpoint. Not sure about manually editing xml or code only mapping.
You can set a field as an Identity and not a key if you need a composite and surrogate key on the same table. The Identity ( Id ) field won't be used by the ObjectContext or ObjectStateTracker but will increment and be queryable just fine though.
I have had problems with EF4 and composite keys. It doesn't support columns being used as components in more than one key in a join table.
See my previous question Mapping composite foreign keys in a many-many relationship in Entity Framework for more details. The nuts of it is that when you have a join table (describing a many-many relationship) where both of the relationships use a common key, you'll get an error like
Error 3021: Problem in mapping
fragments...: Each of the following
columns in table PageView is mapped to
multiple conceptual side properties:
PageView.Version is mapped to
(PageView_Association.View.Version,
PageView_Association.Page.Version)
The only way around it was to duplicate the column which defeats the purpose of having it there at all.
Good luck!

How can I add constraints to an ADO.NET Entity?

I know how to mark a group of fields as primary key in ADO.NET entities but i haven't found a way to declare unique constraints or check constraints.
Is this feature missing on the designer or on the framework?
Support for unique keys/constraints does not exist in ADO.NET Entities in v4.0, see the answer to "one-to-one association on a foreign key with unique constraint", where Diego B Vega says:
I know for sure we haven't added
support for unique keys other than
primary keys in 4.0.
He does, however, provide a possible workaround/hack (which comes with all the normal caveats):
As you are probably aware of, it is
often possible to “lie” to Entity
Framework and tell it in the SSDL, for
instance, that some unique key is the
primary key. I reckon this would work
very well if the actual primary key is
an surrogate key (i.e. an IDENTITY
column that was added for this
purpose) and you don’t even have to
map it in the model.