Entity Framework One-To-One Mapping Issues - entity-framework

Using VS 2010 beta 2, ASP.NET MVC.
I tried to create an Entity framework file and got the data from my database.
There were some issues with the relationships, so I started to tweak things around, but I kept getting the following error for simple one-to-one relationships
Error 1 Error 113: Multiplicity is not valid in Role 'UserProfile' in relationship 'FK_UserProfiles_Users'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be *. myEntities.edmx 2024
My Users table is consists of some other many-to-many relationships to other tables, but when I try to make a one-to-one relationship with other tables, that error pops up.
Users Table
UserID
Username
Email
etc..
UserProfiles Table
UserProfileID
UserID (FK for Users Table)
Location
Birthday

For one-to-one relationships, EF expects that the tables are using the same primary key. And really, if it's a true one-to-one they probably should. So in your example, if you make UserID the primary key on the UserProfiles table, your one-to-one will work.

I have a similar issue, but with a sale and layby scenario.
A layby can exist without a sale, and a sale can exist without a layby. This means I have a 0 or 1 to 0 or 1 relationship.
Layby references sale, but layby cannot use the primary key of Sale, and Sale cannot use the primary key of Layby.
I solved the problem by using a 0 or 1 to many relationship, configured the 'Laybys' getter and setter on the sale as private, and then provided my own 'Layby' getter and setter in my POCO.

Make composite primary key set for two columns UserProfileID and UserID

Related

ADO.NET Entities not picking up an FK relationship

I've got a table "Persons" (PersonId, Name, Address) which contains information about people. I then subclass this information with tables "Clients" (PersonId, DateJoined) and Victims (PersonId, DateAssassinated).
In SSMS I have established an FK relationship FK_Clients_Persons and FK_Victims_Persons where the Primary Key is Persons.PersonId and the foreign key is the eponymous field in the Clients and Victims tables respectively. In SSMS I cannot see any obvious functional difference between these relationships.
However, in ADO.NET Entities when I create the model from the database, the tool does not identify FK_Victims_Persons but it does recognise FK_Clients_Persons. It just treats Victims.PersonId as a simple field and doesn't generate relationship members for it. The missing FK relationship does not appear in the Constraints tree of the Model Browser, but the other one does.
I have no idea why this is, has anyone faced this problem before?
No matter how many times I start over, I can't get it to work.

Linq Mapping Problem with 1 to 0.1 relationships

I have an linq to entity mapping issue. I have three tables.
Table 1 - ItemsB(ID(key), Part_Number(will be null until built), other item b information)
Table 2 - ItemsA(ID(key), Part_Number(will be null until built), other item a information)
Table 3 - WebItems(Item_id, web item information) *Consists of items from both ItemsB and ItemsA after they are built and pushed over to this table.
ItemsA/ItemsB will have a 1 to 0.1 relationship with WebItems. Part_Number maps to Item_id.
I am using EF4.0.
Problem is after i set up the association/mappings as stated above i get an error message stating: "Problem in mapping fragment at lines so and so: Column [Part_Number] are being mapped in both fragments to different conceptual side properties."
Usually i know what to do in this case. Get rid of the property [Part_Number]. Problem is i need to access [Part_Number] in both ItemsB and ItemsA quite frequently without going to webitems. Not to mention webitems will not always have the [Part_Number] populated at certain points depending upon whether the items have be pushed to webitems.
Does anyone know how to get around this?
Thanks in advance.
As I understand it ItemA and ItemB to WebItem in one-to-one relation. One-to-one relation in EF always demands that relation is build on primary keys and one side is mandatory (principal) because dependent entity will have primary key and foreign key in one column. Once you assign primary key in dependent entity you must have principal entity with the same primary key otherwise you will violate referential integrity defined by foreign key.
The problem is that your Part_Number is primary key and foreign key in the same time. To allow such mapping in EFv4 you must use Foreign Key association instead of Independent association. Here is brief description how to create foreign key association in the designer. Once you define referential constrain get back to mapping window of association and delete the mapping.

Entity Framework - ObjectContext.DeleteObject() when related entities are orphaned

I have two entities in a many to one relationship. Widget (1) <--> (*) Users.
If for some strange reason there has been a Widget deleted in the database, where there may not be a foreign key or other referential constraint that would prevent a Users from existing if there was not a corresponding Widget, I cannot use EF to ObjectContext.DeleteObject(). The message is
System.Data.UpdateException: Entities in '<Users>' participate in the '<UsersWidgets>' relationship. 0 related 'Widgets' were found. 1 'Widget' is expected.
at System.Data.Mapping.Update.Internal.UpdateTranslator.RelationshipConstraintValidator.ValidateConstraints()
Is there a recommended way to deal with this in code?
Thanks!
You should modify your Entity Data Model (EDM) to conform to the rules expressed in the database schema.
If there aren't any referencial integrity constraints between the User and Widgets tables in the database and the foreign key column in User is nullable, then the association between their corresponding entities in the EDM should have a multiplicity of 0..1:* (zero or one-to-many).
Right now it is probably set to 1:* (one-to-many) which is causing the validation error, since according to that a User is always expected to have exactly one associated Widget.
Related resources:
Association End Multiplicity (Entity Data Model)
This Exception usually occurs, if you have an FK on Users to Widgets, which is NOT nullable in the DB. So the first thing you should check is, if the FK is defined as nullable on Users.
Which Database do you use? I am only aware with MSSQL Server 2008. If so check which properties are set on the relation in the database. You can define an UPDATE and DELETE action on a relation. If you dont want the user to be deleted when the Widget is deleted you should check if cascade delete is disabled. There is also an option to set the Widget on the User to null.

Mapping one to one foreign key relationships in Entity Framework 4.0?

I'm sure I'm missing something very simple, but let's say I have two entities, Employee and EmployeeType.
Employee type would contain values like 'Full time', 'Contractor', 'Intern', etc.
An Employee entity would contain one, and only one EmployeeType value.
So I am designing a new .edmx model using the Model-First approach and generating my actual sql server data schema from the model.
I want to add an integer type foreign key id into my Employee entity, EmployeeTypeId, which will map to the primary key of the EmployeeType entity.
So I've gone ahead and done that in my Employee entity. Where I'm stuck is how, though the Entity Framework designer, to enforce the 1:1 referential constraint on that EmployeeTypeId property? Or does the EF handle that automatically behind the scenes?
thanks in advance,
John
Think I figured out the answer to my own question. In the EF .edmx surface designer, I needed to right click on the scalar property I wanted to set as a foreign key id to the other entity and choose 'Entity Key'.
Once that was done, I could go into the referential constraints dialog box and point my new foreign key property to the other entity.
If you don't explicitly set your foreign key property as 'Entity Key', EF will think you want to point your primary key id to the other table.
cheers
You first create a new association (if you haven't done this already) between the two entities. Just right-click on the edmx designer and choose Add -> Association.
When you click on the association you have just created in the model designer, in the properties window, you can set the End1 Multiplicity and End2 Multiplicity properties to 1. This will ensure that you can set only one relation entity while using the entity framework. This does not get enforced in SQL server by the way, because SQL server does not implicitly support 1:1 relationships.

Entity Framework - Change Relationship Multiplicity

I have a table [User] and another table [Salesperson] in my database. [Salesperson] defines a unique UserID which maps to [User].UserID with a foreign key. When I generate the model with Entity Framework I get a 1-to-Many relationship between [User]-[Salesperson], meaning that each User has a "Collection of Salesperson", but what I want is a 0..1-to-1 relationship where each User has a nullable reference to a "Salesperson".
I tried fiddling around with the XML and changing the association's multiplicity settings, but that only produced build errors. What I am trying to achieve is no different than having a nullable SalespersonID in [User] that references [Salesperson].SalespersonID, but because salespeople only exist for specific users it feels like I'd be muddying up my [User] table structure just to get the relationship to point the right way in Entity Framework.
Is there anything I can do to change the multiplicity of the relationship?
Make the PK of Salesperson itself a FK to User. The EF's GUI designer will then get the cardinality correct, since PKs are unique.