Entity Framework 5.0 1:1 relationship Model-First [duplicate] - c#

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
1:1 relationship problems with EF Model First
Is there a way to define 1:1 relationships on a Entity Framework .edmx without get this annoying:
Error 1 Running transformation: Multiplicity is not valid in Role
'PESSOACPF' in relationship 'FK_CPF_PES'. Because the Dependent
Role properties are not the key properties, the upper bound of the
multiplicity of the Dependent Role must be
*.
Already defined the FK as PK on my table, removed the pk, tried to re-create the project over 10 times and doesn't help AT ALL.

Your Foreign Key must be defined as UNIQUE.
To enforce a 1:0 or 1:1 relationship.
You can create Table in SQL DB like this (Lets take Order and OrderDetails Tables)):
CREATE TABLE OrderDetails (
DetailsId INTEGER IDENTITY NOT NULL,
orderId INTEGER NOT NULL UNIQUE,
PRIMARY KEY (DetailsId),
FOREIGN KEY (orderId) REFERENCES Order(orderId)
)
For more details Implementing one-to-zero-or-one relation in SQL Server
I hope this will help to you.

Related

EntityFramework: 1:0..1 relation deserialized as collection

I have a problem with entityframewrok wich serializes a 1:0..1 relation between two tables as a collection.
I have 2 tables on my Database:
CREATE TABLE `revisiones` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
---Irrelevant columns here---
PRIMARY KEY (`Id`),
---Irrelevant constraint and foreign keys here---
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
CREATE TABLE `ficha_deposito` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`IdRevision` int(11) NOT NULL,
---Irrelevant Columns Here---
PRIMARY KEY (`Id`),
UNIQUE KEY `IdRevision_UNIQUE` (`IdRevision`),
CONSTRAINT `fk_ficdep_rev` FOREIGN KEY (`IdRevision`) REFERENCES `revisiones` (`Id`) ON DELETE CASCADE ON UPDATE CASCADE,
---Irrelevant constraints here---
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
As you can see a revision may be related as none or 1 ficha_deposito, due the unique restriction.
However on the edmx file, the relation is serialized as a collection:
If i try to change it manually (I would prefer to not do so, because if i have to regenerate the model, i will have to set the value again manually), then i get an exception:
Running transformation: Multiplicity is not valid in Role 'ficha_deposito' in relationship 'fk_ficdep_rev'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *.
Why can't I change the multiplicity of the relation? revision.ficha_deposito should be null or a simple object.
This is a EF6 limitation. EF naturally supports one-to-one relationship via so called Shared Primary Key Associations, where there is no separate FK in the dependent entity, but the PK is also used as FK to the principal table.
When the dependent entity is using a separate FK field, even if it's backed with an unique constraint, from EF perspective it's still one-to-many relationship. I can't explain it better than in the One-to-One Foreign Key Associations:
As you may have noticed, both associations in the fluent API code has been configured as a many-to-one — not one-to-one, as you might have expected. The reason is simple: Code First (and EF in general) does not natively support one-to-one foreign key associations. In fact, EF does not support any association scenario that involves unique constraints at all.
and then
The second limitation that this lack of support impose to us is more important: one to one foreign key associations cannot be bidirectional (e.g. we cannot define a property for the User on the Address class).
The good news are that such support has been added to EF Core, so when it becomes usable (v1.1 or later) you'll be able to set up such relationship. Until then, you should live with one-to-many.

Entity Fluent API many-to-many table access [duplicate]

This question already has answers here:
Create code first, many to many, with additional fields in association table
(7 answers)
Closed 7 years ago.
When using many-to-many with fluent api, EF creates an extra couple-table for primary keys. This table is not in DBSet<>, so how do I access this table within my code? Should I add it to DbSet<>, or should I define this table and must not leave EF to create it?
If you let the model as is, not declaring any relation entity between A and B, then you can't access that table. But maybe you don't need it, because you can use navigation properties (collections) from A to B and B to A.
So if you need all B's associated with A, you will use a.Bs, and vice versa.
You can achieve any desired query result between A and B even if you don't access the relation table, and you would need to declare that table only if you need to add extra data to the relation.

Entity Framework 1to1 relationship via association table not working in EDMX

I have the following tables in SQL Server database
Which has a 1-1 association table (FooBar) which has unique indexes on corresponding FooId, BarId, and the primary key is (FooId, BarId).
To be clear FooBar does not allow any FooId (due to unique constraint) to be in the table more than once neither can any BarId (due to unique constraint) be in the table more than once. This is what makes it a 1-1 associative table.
I want to have this association table instead of 1-1 relationship between Foo and Bar because in my real world scenario, Bar will have other relationships to different unrelated tables and I will want similar association tables (as opposed to adding new FK columns to Bar for each new table)
Which I then bring these tables into my EDMX designer. The relationship is brought in as a Many to Many instead of One to One.
Which of course isn't what I want. I can manually change the model to a 1-1 relationship.
But then I get an error (in the designer).
Is this a bug or is it not possible to create a 1-1 association in this manner in EF?
It is a "bug" with the entire EF design: Entity Framework 4-6.1x Only Honors Multiplicity on Primary Keys.
Thus even though we know (and the RA models) that it is a 1-1 relationship due to a Candidate Key Constraint, EF does not and will not like it. Tough luck.
The "solutions" include:
Changing the model to something EF understands (EF understands Code First, not RA). Granted this may indicate an "issue" with the selected RA model, but such is orthogonal to the question ..
Live with the incorrectly generated multiplicity rules and "use with care"; the dirty work can be wrapped, but has to be added manually outside of the auto-generated model.
.. Hmm, others?
Shameless plug to unresolved questions deal with the same core lack-of feature:
How to get EF6 to honor Unique Constraint (on FK) in Association/Relationship multiplicity?
How to add an EF6 Association to a Candidate Key / Unique Key which is not the Primary Key?
The relationship you've shown in your first graphic is not a 1to1 relationship as far as EF is concerned.
It's a many to many relationship between Foo and Bar
Think about it this way:
Possible combinations with the following Foo and Bar values
Foo
1
2
3
Bar
1
2
3
FooBar
1, 1
1, 2
1, 3
2, 1
2, 2
2, 3
3, 1
3, 2
3, 3
Your FooBar table is a composite key, meaning it's a combination of the Foo and Bar values making up the key - not a 1 to 1 relationship
to define a 1 to 1 relationship between Foo and Bar, your schema should look something more like this:
Foo
FooId PK
Bar
FooId PK FK to Foo.FooId
the FooBar table is not needed for a 1to1 relationship between foo and bar.
As you stated in your question/comments - yes you put a unique constraint on the individual parts of your composite key, but EF doesn't take into account unique constraints for your model when determining a relationship. If you want a 1to1 relationship, you should create a 1to1 model, rather than mocking a 1to1 relationship via unique constraints.

Error 112 and Error 113 after adding foreign keys to database

This is my table ( part of the whole thing , activity table still have other relationships with other tables) :
After adding foreign key and update my model EDMX from database in my visual studio , it came up this 2 error :
Error 2 Error 112: The types of all properties in the Dependent Role
of a referential constraint must be the same as the corresponding
property types in the Principal Role. The type of property
'ActivityID' on entity 'istellarModel1.singalong' does not match the
type of property 'ActivityID' on entity 'istellarModel1.activity' in
the referential constraint
'FK_singalong_activity'.
and
Error 1 Error 113: Multiplicity is not valid in Role 'activity' in
relationship 'FK_singalong_activity'. Because all the properties in
the Dependent Role are nullable, multiplicity of the Principal Role
must be '0..1'.
I checked my ActivityID in singalong have the same type as activity table's Activity ID , and i don't understand what error 113 actually means , i am new to database , at first i have many tables that don't link so i link them up after sometime and update my model ( EDMX ) in visual studio ( using entity framework ) and it gave me errors .
Any guidance on this please?
The 113 error sounds like your activity ID in singalong is NULLable.
It's stating that many-to-one is not valid if the dependent role is nullable.
That also seems to suggest the reason for the 112 - it's most likely complaining because it considers NOT NULL part of the type as well, so the two columns are different.
A quick fix may be to ensure that singalong.ActivityID is marked a NOT NULL but this will be problematic if you want singalong records with no corresponding activity.
I know how I'd fix it, at least initially, but it may be frowned upon be more knowledgeable DBAs: I'd simply add a dummy activity (eg, activity id = 0) to use for those cases where you would normally have NULL in the singalong table. I'm not suggesting you do that, but it's a possibility I would examine as a temporary fix, being far more of a pragmatist than dogmatist :-)
I know this is a old question, but run into the same issue, and here is the fix.
If you open your .edmx file (using entity framework), you will see your different tables. If you click on the line linking the tables:
It will show the properties. Select Multiplicity and set to 0..1
Your foreign key will need to be set as nullable.

Add Navigation Property Manually to Entity Framework

I need to add a navigation property between two Entities TableA and TableB
TableA
ID : Primary Key
Code: String (Allows Null)
TableB
BID: Primary Key
Code: String (Allows Null)
Now I want to add a navigation property to these Entities which are related by the code which is not a foreign key. Can anyone tell me how this is possible
It is not possible because code is not PK in any of your tables. Navigation properties follows same rules as database relations - in principal table you must use PK and in dependent you specify FK. Databases also offers selecting unique key in principal table but EF doesn't support unique keys yet.

Categories

Resources