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.
Related
I was hoping someone could give me a bit of advice here. I am wondering if I am on track or way off base in my approach. I am using Entity Framework, database first approach. I have a link table that associates people to each other. Person 1 associated to Person 2 as a friend for example. (association_type holds a key value associated to a lookup table)
I noticed that Entity Framework creates two separate navigation properties.
[EdmRelationshipNavigationPropertyAttribute("IntelDBModel", "FK_a_Person_Person_t_Person", "a_Person_Person")]
public EntityCollection<a_Person_Person> a_Person_Person
[EdmRelationshipNavigationPropertyAttribute("IntelDBModel", "FK_a_Person_Person_t_Person1", "a_Person_Person")]
public EntityCollection<a_Person_Person> a_Person_Person1
In other parts of the application, I have successfully used Entity Framework to write data to the database. For example, I have a person to telephone relationship.
In the person to telephone scenario, I create a t_Person (p) object, then create a t_Telephone (t) object and use p.t_Telephone.Add(t);
That seems to work fine.
I am somewhat lost in terms of how to manage this person to person link table insert.
When saving to the database, I use foreach to iterate through the People objects.
foreach (t_Person p in People)
{ctx.t_Person.AddObject(p);
...
}
I know what person is associated to what person in this People object collection. However, I don't know how to utilize the t_Person navigation properties (a_Person_Person) to save the person1 and person2 values to the link table (a_Person_Person).
Any hints would be greatly appreciated.
I think the given situation will generally give you hard time when using EF, since you are linking two foreign key two one table with same Primary key, since the relationship or lazy loading would be difficult to handle you might get double records or wrong records, I would add another property to the t_person table like datecreated which would make the the EF treat t_person table as not an association, but as actual entity giving you more control over entity and insertion and deletion.
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.
I am using EF 5 with migrations and code first. It all works rather nicely, but there are some issues/questions I would like to resolve.
Let's start with a simple example. Lets say I have a User table and a user type table. The user type table is an enum/lookup table in my app. So the user table has a UserTypeId column and a foreign key ref etc to UserType. In my poco, I have a property called UserType which has the enum type.
To add the initial values to the UserType table (or add/change values later) and to create the table in the initial migrator etc. I need a UserType table poco to represent the actual table in the database and to use in the map files. I mapped the UserType property in the User poco to UserTypeId in the UserType poco. So now I have a poco for code first/migrations/context mapping etc and I have an enum. Can't have the same name for both, so do I have a poco called UserType and something else for the enum or have the poco for UserType be UserTypeTable or something?
More importantly however, am I missing some key element in how code first works? I tried the example above, ran Add-Migration and it does not add the lookup table for the enum.
If I understood properly your questions and what you're confused about,
Enums support has nothing to do with lookup tables on the Db side.
Enums are simply allowing you to have properties in your classes that are Enum-s and that is translated into 'int'-s basically - so there is nothing much else in there.
For more info you might wanna look at this video from Julie Lerman on Enum-s support
hope this helps
In my experience the enum is more important to your code than the lookup class so give it the proper name. I would also keep the look up class isolated without any relationship to the User in my Model. If it trully is only for lookup, then you don't need it hanging off of your User. Your enum with a DescriptionAttribute can fulfill the lookup in your code.
UserTypeLookup might be a good name since that sounds like what you will be using it for. Then you can use that class to maintain the table.
Assuming you don't map the relationship between UserTypeLookup and User in ef code first, the only thing you should need to create in the DB manually is the foriegn key relationship between the UserType column in your User table and the PK from the UserTypeLookup table. UserTypeLookup can still be an entity and EF should still generate the DB table for it even if you don't setup any relationships for it.
I'm trying to get a one to many relationship mapped with the EF and for some reason it's proving more difficult than usual. This is my EDMX and DB Schema. The reason I used an auto-increment key on the middle table is because I was told it's difficult to use composite keys with the EF.
What I need is to be able to do (with a Course entity) someCourse.Students or (with a student entity) someStudent.Courses. Could anyone give me some pointers as to the best way of mapping this?
alt text http://img195.imageshack.us/img195/4053/schemacz.png
alt text http://img22.imageshack.us/img22/8193/edmx.png
If you remove the AssociationID column and make both the StudentID and CourseID the primary key of the StudentCourses table, it will pick up the many-to-many relationship and generate more intuitive entity classes (ie Student.Courses, Course.Students)
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;