create one to one REFERENCES in EF designer? - c#

I have two tables employees and aspnetUser. I'm trying to create an Associate between tables employees and aspnetUser in EF designer in VS2013 using email from employees and username(email).I createUNIQUE Constraint on employees email to have UNIQUE email for each employee I have Problem when click ok I have this error message "verify that the navigation property name is unique" I have couple of Problems.
1-I try to use ID from asp.net membership but it is varchar(128) not guid because of that I have to change my employees ID from int to varchar(128) to very table have relation with employees.
2- how to solve this problem apically I don't have access to database because the easy thing is to create REFERENCES CONSTRAINT between users and employees I really don't have access to database because of company ?

The error tells you that you're introducing new navigation properties with names that already exist in the types you're trying to associate. In the text boxes below the Navigation Property checkbox you're not selecting properties. You're entering names for the new ones you've indicated you want to create by checking the checkboxes.
This is not going to work. If the types of the primary keys are different, EF won't be able to create a "soft" association between them (where a "soft" association is one that is not backed by a hard foreign key in the database).
You only option is to manually join the two tables whenever you need data from both.

Related

EF Foreign Key Associations Deleted

The picture show relation with two tables in my database;
Well as you can see there's a field called "DeviceTypeID" in the right side table.
Normally EF adds CompanyTypeID column to the ServiceLaburDefinitions model but it's disappeared last time I updated model from the database.
I am searching for a solution for a couple of hours but not able to find any solution. Could anyne suggest a solution?
Thanks.
ServiceLaburDefinitions is the depend entity and has the DeviceTypeID foreign key property defined.
So Entity Framework creates a navigation Key under the hood between the 2 tables based on DeviceTypeId key.
You can see for example how a navigation key is created also in the following example between the foreign key and the primary key
More information about navigation properties can be found here
When you create the model from the database, there is a checkbox marked "Include Foreign Key columns In The Model" - Make sure this is checked.

Entity Framework association with multiple tables

I have the following model with 3 tables and I'm attempting to create an association to the third table CustomerPart which uses a compound key from both of the other tables.
CustomerPart should be accessed via a key of CustomerNo from ShopOrder and Site/CatalogNo from SalesPart.
The only thing I've been able to come up with so far is to create an association from SalesPart to CustomerPart and in code manually select records where CustomerNo on CustomerPart matches the CustomerNo on ShopOrder.
Is there a way to create an association and navigation property for this or am I out of luck?

Telerik Data Access One To Zero One association

I am trying to make One To Zero One association between two tables in Telerik Data Access, but can't make it work. Here are my tables:
Student
Id (PK)
Name (string)
BackPack
Id (PK)
StuffInside(string)
StudentId (Unique, Foreign key)
StudentId in Backpack references to Id in Student. When I do the mapping for some reason Telerik is making it One to Zero Many. I need One To Zero One.
I assume you are using the Telerik Data Access Visual Designer to model your database? If so, in order to create one-to-one association you need to specify that the ID from one table (Students) matches the ID from the other table (BackPacks). This way each student will have exactly one (or zero) backpack. Please refer to this documentation article which demonstrates the approach.
If this is not applicable in your scenario and you have to match the Student ID to the BackPack StudentId to achieve the same effect you could create one-to-many association and then manually create the unique constraint on the database server side. Alternatively you could switch to Fluent Mapping which allows you to create custom indexes in you mapping.

EntityDataModel and Association table

I have an authors -> authorsbooks <- books in my database. I created an entity data model from this and I noticed that the association entity didn't show up in the model(I think it's inferred).
I want to drag 2 EntityDataSources onto the designer, a gridview of authors, and when a user clicks to select an author another gridview below will show all the books assoiciated with that author.
How do I configure that second entitydatasource because when I try to configure it I don't see the authorsbooks association entity in the entitysetname dropdownlist. I'm thinking to set this as the datasource to the books gridview? Am I misunderstanding something?
Thanks,
rodchar
Entity Framework will infer the relationships based on the Foreign Keys in your database.
Does your AuthorBooks table have a foreign key to the Author table?
Also, when you add your tables onto your EDMX, make sure you tick "Include Foreign Keys in Model".
If done correctly, when you type Author., you should see AuthorBooks as an EntitySet.

How to access foreign key related column values using LinqToSql?

I'm trying to wire-up LinqToSql to be my data access layer and running into a few issues, probably due to my lack of experience with LinqToSql.
I have two tables, one called Project and one called Employee. The Project has fields for OpenedBy and ClosedBy which are foreign key references to the Employee table, which has fields for EmployeeId and Name.
When I fetch a Project I would like for it to fetch the EmployeeName for the OpenedBy and ClosedBy. I would like to access these like the following:
// assuming data is of type project
this.OpenedByName.Text = data.OpenedByName;
this.ClosedByName.Text = data.ClosedByName;
Is it also possible to set these values whenever OpenedBy or ClosedBy changes? Is this possible? Sample code would be much appreciated!
Clarification
I would like to do this without having to use stored procedures.
If you have 2 relationships coming from the Employee table, I think you'll have 2 child properties, project.Employee, and project.Employee1 in each Project entity.
You can change the name of the association, just go to the relationship properties, select Child Property and there change the name of each child Employee to be more descriptive.
You can name the child properties as you want, for example you could:
this.OpenedByName.Text = data.OpenedByEmployee.Name;
this.ClosedByName.Text = data.ClosedByEmployee.Name;

Categories

Resources