Telerik Data Access One To Zero One association - c#

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.

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.

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?

Representing a list in a SQL database

I'm doing an application in C# with an SQL database.
I've got a model:
Person()
{
string id;//unike key
string name;
List<string> responsableOf;//list of id
}
and I want to represent it into a table.
Which are the right fields for the tables?
It depends on what kind of relation is there between the person and the other persons that he will be responsible for.
If it is a parent and child relation i.e a composition you can use a self reference table. Something like:
Persons with the following columns:
Id,
name.
ParentId Foreign key to the same table.
If the relation between the person and the others is an aggregation, and a person may be responsible for many other persons:
Persons:
Id,
name.
usersresponsibilities:
Id,
PersonId,
ResobonsiblePersonID.
Later, in both the two cases, in your front end application you will need to deal with the data in these table as an objects not as rows. You should think in terms of objects.
In your application your current class Person should be mapped to this table.
The solution is going to depend a little on your constraints :
If a single person can have several people responsible for them
What you need is a many to many relationship between persons (and a table for the person containing the other fields, ID and name)
You would represent that using a table associating two IDs (Foreign Keys) For any given row, the table would associate :
The ID of the responsible
The ID of a person he is responsible for
Building your list is as simple as querying this table for rows with a given responsible ID.
If a single person can only have one person responsible for them
You need a Foreign Key to the same table in your Person table (Responsible ID).
Building your list is then as easy as querying the person table for rows with a given responsible ID.
create a table with id and responsableOfID as integers while responsableOfID is a foreign key to another table which has an id and name

Multiple joins to the same table with the Entity Framework

If I have a table with two foreign key fields to another table, I.E.
Table: User
Field: FK_PrimaryItem_ID
Field: FK_SecondaryItem_ID
Table: Item
Field: ItemID
When I'm using the entity framework, the generated objects become:
User.Item
and
User.Item1
and I can't differentiate between the two of them. I can map back to the name of the foreign key, but this is a difficult way to go about it. How can I find out which one, Item1 or Item is which field?
I would like to leave my EDMX file auto generating if possible.
I've not found any problems with updating my model once I'd changed the name of the Navigation Properties on the design surface.
In general, User.Item would be represent the first column the model came to with that foreign key, and User.Item1 would represent the second column.
But as I said, I just went into the model, and changed the name of the Navigation Properties to more usable names based on the association listed in the Mapping Details.
I had the same problem with a self-referencing key:
PageID
Parent_PageID (refers to PageID)
Until I renamed the Navigation Properties to "Parent" and "Children" respectively. The toughest part was figuring out which is which, which I did by noting the Multiplicity property on the NavigationProperty objects (0..1 for parent, * for children)

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