How to access foreign key related column values using LinqToSql? - c#

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;

Related

Entity Framework 6 specific column mapping

In SQL, I would tell the database what the foreign key constraint is.
But fluent EF6 apparently does not have a way for me to specify what column to use when binding collections.
Is it not possible to tell DbModelBuilder exactly what column to bind relationships on? Or does it demand to be the primary key at all times?
Table_Person
id int // pkey. Multiple people records
UniqueID int // the unique person
sometext varchar(256) // database therefore tracks changes to this, since unique person can have many records (pkeys).
Table_Address
id int //pkey
fk_unique int // should map to UniqueID of person, NOT the pkey.
line1 varchar(512)
state varchar(64)
etc
One unique person has many records, and their uniqueID (not pkey) has many associated addresses. Actual structure is far more complex than that. But am looking for a way to do this fundamentally...
Would very much so like to have an ICollection<Address> Addresses within the Persons model. But to enable such a thing for code-first migrations... seems impossible?
Yes I could Add-Migration and then modify the generated code/sql manually. But doesn't that defeat the point? Or is that common practice?
If you're able to modify the DB schema you could put UniqueIDs for people into their own table named "Person" and rename the existing table to "PersonVersion". Then have FKs to the new "Person" table on "PersonVersion" and "Address". And finally, create the Person, PersonVersion, and Address models in your app code and EF should bind without problem.

Self Reference Data insertion with Parent and Child Id

I have a table which is used to add couples. The table schema like below
MyTable:
Id
Name
SpouseId
My aim is to get the spouse details also from the same table using spouseId. Here i can relate the spouseId to Id of same table as self reference.
Issue:
From UI, i got object of MyTable which also includes the object of spouse which is also MyTable type.Here when EF inserts the data, 2 entries created and updated the SpouseId of child object as its parentId.
But here i also need to update SpouseId of parent object as child object id.
Any advise on this? or my design is wrong?
Your schema looks funny mate! I would suggest you change the design have a separate table which holds relationships.
Your design might cause problems while inserting as well.

create one to one REFERENCES in EF designer?

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.

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.

Problems with automatic property creation when manually mapping relationships between tables with no foreign keys in L2S

Pardon the massive headline.
I'm in the situation of having to build an application on top of a database, that I cannot make any changes to. The database does not have any primary- or foreignkeys set.
I'm using linq-2-sql, and I'm interested in having some properties exposed on the entities generated from my dbml. For instance, in the hypothetical example of a one-to-many relationship between table education and student - where each student record has a reference to an education id, I'd like to be able to go:
var student = GetAStudentFromContextOrWhatever();
var studentsEducation = student.Education;
It is my experience, that this kind of property is automatically generated when I drag'n'drop tables with foreignkey relationships from the server explorer.
However as previously mentioned, in this case I do not have these foreign key relationships - rather I am adding the relationships manually in the dbml file, specifying parent and child class.
When I add these relationships, I expect the involved entities in the designer.cs of my context to get populated with properties of a kind like those described above.
This, however, does not happen.
What must I do for my dbml to create these properties for me - based on these manually mapped associations between entities/tables that, on a database level, do not have foreign key associations?
Cheers!
L2S is just that Linq-to-SQL. If it isn't in SQL it won't be generated. The expression trees behind L2S just can't understand what you are doing. The place for your association is in a partial class file which you will have create manually. Also it probably won't update or insert through the association.
I know this is a very old question, but I just ran into the same problem. In order for the relationship in the DBML designer to automatically create the association properties for you, you need to have primary keys on your objects. If you click the column name in the designer, you'll see that your PK field has PrimaryKey = false. Switch that to True and build; all should be well.
Patrick

Categories

Resources