Navigation Properties get overwritten when I update my Entity framework from database - entity-framework

I have a table called Matches and a table called Teams in my database where my Matches table has two foreign key HomeTeamID and AwayTeamID link to Teams table.
When I generate edmx file I got Team and Team1 in my Navigation Properties in my Matches class. I overwrote them to HomeTeam and AwayTeam in the design panel, and everything works fine.
But next time when I update my model from database, their names changed back to Team and Team1.
How can I solve this?
Thank you in advance =)

Add real foreign keys to your DB.

Related

Update edmx after adding additional column to junction table

I'm using .Net 4.5, entity framework 5, database first. I have a junction (many-to-many) table in my database. For this example lets say the table is "StudentsCourses":
Students
-PkStudentId
-Name
Courses
-PkCourseId
-CourseNumber
StudentsCourses
-FkStudentId
-FkCourseId
This works just fine right now. The 'generate model from database' creates a Student entity with a navigation property to the Course entity. But here is where the trouble is:
I need to add another column to the StudentsCourses table. Lets just call this column "CourseYear". So our junction table would now look like this:
StudentsCourses
-FkStudentId
-FkCourseId
-CourseYear
So, I've added this column to the database and ran "Update Model from Database" on the edmx. I would expect to see an entity created for StudentCourses, with a navigation property to both Students and Courses. But no such entity is created. I still see the same two tables (Students & Courses) with the same navigation property as before.
I've done a lot of reading and researching, but haven't really come across a clear-cut answer. There is a wealth of information on code-first which I can't apply to my scenario. Is there a way to get what I'm after? Is it as simple as adding a PkId to the StudentCourses table? SQL Replication is preventing me from doing this. I would think the composite should suffice, but maybe EF needs a PK to do it's magic? I read a little bit about manually setting relationships, but could not find anything speaking to my particular situation. It could be that I am just missing a simple step in the process of updating the edmx from database. I've done this plenty of times when: adding new tables, adding columns, deleting columns, etc. I'm following the same steps as I always do, but maybe I need to do something different in this case?
Thanks ahead of time for any help. It is greatly appreciated. Please let me know if any more information would help.
From what I've gathered it appears as though EF will not generate a model for a table that doesn't have a Primary Key.
I'm a bit late for this, but you have the answer in this thread Updating Entity Framework Model after adding a field to a previous look up only table
As they say here, you have to delete the relationship between Students and Courses in the designer. Then update your model from the database, and make sure StudentsCourses table is checked in the Tables branch of the Add tab.

Entity Framework - Entity not added to design surface

I attempted to update my edmx file by selecting a table. The tool spit out a info message that said the table did not have a primary key.
The entity did not get added to the design surface but it did get added to the .edmx file. In addition, using the model browser I see an Entities.Store and an Entities. My table got added to Entities.Store, but not to Entities.
I cannot access the table that was "added" in the code.
What do I do?
Steps to reproduce:
Create a SQL table with two columns that are both defined as foreign keys to other tables. Make sure the tables that the FKs point to already exist in the model on the design surface.
Right click and choose Update Model from Database...
Next. Under the Add tab, mark the new table under Tables
Click Finish.
An association will be created and it will be selected on the design surface, but it won't start with FK_, it will just be the name of your table. Go to the Model Browser and look under Entity Types. The table will not be there. Look under Associations and you will see your table name there as an association, but it will look out of place (because of the name).
Entity Framework was too smart for me. It created an association instead of an entity. Odd, but it works for how I need to use it.

Create One-to-One relationship based on PK of both tables

I'm really new to Entity Framework (currently using EF5) and vs2012 and am having difficulty trying to figure something out.
I have an .edmx that was generated from my database. It has two tables in it: Item and 3rdPartyItem. In short, the Item table is the main table for all the company items while the 3rdPartyItem table is a table that is used to hold additional attributes for items. This 3rdPartyItem table was created by an outside company and is used with their software for the company, so I can't mess with either table. What I'm trying to do is show a list of Items in a grid but I need to show a combination of all the fields for both tables. In vs2012, if I create a relationship and make it 'zero-to-one' (because for each record in the Item table, there doesn't necessarily have to be one in the 3rdPartyItem table), vs complains about not being mapped correctly. When I set the mapping, it then complains that there's multiple relationships. I did some research and found that you can't create a relationship with 2 primary keys, so I was thinking that was the problem. But how can I change the .edmx so that in code, I can access Items and 3rdPartyItem like so:
var items = dbContext.Items;
items.3rdPartyItem.SomeField <--field from 3rdPartyItem table.
not too sure if it's even possible, but it would be very, very helpful if so. Any ideas?
What you're looking for is table-per-type (TPT) mapping inheritance. You can view an MSDN walkthrough here (although you'd want your base type to be instantiable):
http://msdn.microsoft.com/en-us/data/jj618293.aspx

Removing Entities from Database table via Navigation Property using RIA Services and Entity Framework

I have 3 normalised tables consisting of Employees, Departments and EmployeesToDepartments. I wish to be able to assign an Employee to one or more Department, hence the link table (EmployeesToDepartments). I can successfully query the database and extract the full hierarchy of entities via the Navigation properties using
this.ObjectContext.Employees.Include("EmployeesToDepartments").Include("EmployeesToDepartments.Department")
plus the [Include] attribute in the metadata, thus allowing me to access the Departments for a given Employee. Upon trying to remove a link between an [Employee] and [Department] in the [EmployeesToDepartments] table I was given a Foreign Key Constrain error.
I have simplified my model to include just one navigation property between [Employees] and [EmployeesToDepartments]. A Foreign Key constraint between[Employees].[ID] and [EmployeesToDepartments].[IDEmployee] was preventing me from updating the EmployeesToDepartments table. With this removed via a Relationship setting I can now update the table. I can now execute the following code
foreach (var rel in _employee.EmployeesToDepartments)
{
_employee.EmployeesToDepartments.Remove(rel);
}
_domainContext.SubmitChanges();
without error.
I was expecting to see the entries in the RelEmployeesToDepartments with the IDEmployee to have been deleted. What I see in the table are the value 0 where the IDEmployee previously was.
Is it possible to force a DELETE statement to be issued? Am I misunderstanding the basic concepts here?
Any help would be much appreciated.
Removing entities in navigation property only breaks the link between entities. You have to delete from the EntitySet to achive what you want.
ex)
myDomainContext.EmployeeDepartments.Remove(employeeDepartmentToRemove);
myDomainContext.SubmitChanges();

MVC Entity Type Missing

I'm brand new to ASP.NET MVC (Literally just two days of learning on the fly). I'm trying to create a website using MVC, however, I've screwed something up in my solution.
I created this table that had foreign key constraints but I initially forgot to set up a primary key on the id field and forgot to set it identity specification to yes. So when I updated my DataModel and brought this table in, it showed every field as being the primary key and it showed the relationship to the other tables. So I went back to the database and changed the id field for the table to be the primary key and identity specification to yes.
I went back to the DataModel, tried to Update Model From Database, went to the refresh tab, expanded the tables, and selected the table I wanted to refresh, then click on "Finish".
The data model appeared to make the changes but it showed the relationships to the other tables twice.
So I thought I'd delete the table (entity) from the DataModel diagrahm and bring the table back in through Update Model From Database. But when I went back to the Update Model From Database...
Under the Add tab, the table isn't under the Tables
Under the Refresh tab, the table is listed, but when I select it and click on "Finish" the table doesn't reaapear in the diagrahm.
In the model browser...
Under Models
Entity Types, The entity isn't listed
Under Associations, The relationships no longer appear (obviously because the entity is gone).
Under Models.Store
Tables/Views, The table is listed.
Constraints, the relationships are shown.
I've tried dragging the table over to the diagrahm from the Model Browser, but that doesn't appear to work.
I've tried to right click in the diagrahm, add Entity but my table doesn't appear as a Base Type.
I've tried to google but I'm coming up with nothing. With this entity missing, I cannot even build my solution. Please help.
Try to delete the entity file and add it again. But you have to name it as previous entity and don't forget to delete the connectionString in web.config before add it.
If you don't know what I am saying just let me know.