Entity Framework 4 many to many relationship issue - entity-framework

Considering this database:
This is what EF generates:
I think that HeroesItem class, is useless, and there should be a navigation property Items on Hero class and a navigation property Heroes on Item class.
I saw this can be done easily with code first, but how to get it done using database first?

It can be done only if your HeroesItems table does not contain Id column and instead uses IdHero and IdItem as composite primary key. Once you add any additional column to junction table you must map it as entity to have control over that column.

Related

Entity Framework inheritance moving a foreign key to a child in TPH

I'm using table per hierarchy inheritance within my enitity model and I have a parent table that contains relationships to other tables via foreign keys.
My parent table (Product) has a FK relationship to another table which in EF resolves to a Navigation Property. I can easily move that navigation property to my child table (Fragrance) and delete it from the parent which is great. However I also want to move the foreign key property (FragranceId) to the child but I can't work out how to do this because the FK relationship requires that my parent table has that property.
The diagram below illustrates how far I've got, basically what I'm trying to do is move the FragranceId into the Perfume entity.
It seems like this should be possible but since Perfume is not a table it can't take on the database relationship and while that relationship exists between Fragrance and Property I can't remove FragranceId from Product.
Moving the FragranceId leads to an EF error 'there is no property with the name FragranceId defined in the type referred by Role Product.
The relationship between Fragrance and Perfume is 1 to 1.
Any help would be awesome. Thanks.
So it turns out the answer is pretty simple I just needed to delete the database foreign key relationship from my entity model and I could move the FragranceId to the perfume entity.
I then needed to manually remove all reference to the FK from the edmx file's XML to resolve error 3015 and finally re-map the FragranceId to fix error 3004.

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

Entity Framework force single row table

Using Entity Framework 4.1 code first and POCO entities in MVC3; I would like to create a single-row table called ActiveBlog, which has an id, and holds a reference to a blog table.
ActiveBlog
PK: ID, FK: BlogID
How can I define a table, both in the database and in my POCO entity, that is constraint to only hold one row?
Set a trigger in your database. See http://social.msdn.microsoft.com/forums/en-us/sqldatabaseengine/thread/FCC7CECA-CF2F-42E2-8E79-1DCB5A3B82EC
You can't/shouldn't - you would/should have to check its row count every time.
Ideally you want to write this into your repository or whatever class you wrap this Ef object with.

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();

Entity Framework: Exclude columns from the selection in Entity Framework?

I want to have an ObjectQuery that returns tracked entities (not static data), but I don't want it to load all the columns, I want some columns to load as null, I don't want to use select, since this will return an IEnumerable of the values, not tracked objects.
Is there a way to do it?
If yes, how do I then complete reloading those columns on demand?
Have you tried creating a view and then mapping the view?
By creating a view you can select the columns that you really want and only those will show up on the Entity Model.
I think the only way is to create new entity type which will not contain columns you don't need. You will map this entity type to the same table. On demand (lazy) loading works only for navigation properties.
Edit:
My previous idea doesn't work but in some special cases you can use idea from this article. Instead of modeling single entity from single table you will model multiple entities related with 1:1 relations. Entities will not overlap in properties (except the primary key) as my previous idea assumed because it doesn't work. You will than have main entity with fields you want to load immediately and related entities which will be lazy loaded when needed.