Linq To Entity SelfReferencing Relation Mapping - linq

How do you map a table called category with Id as Primary Key which has self reference called ParenCategoryId using LinqToEntity?

Create a FK that points from ID to ParentCategoryID. EF should pick up the navigation property from there. The name will be useless though so you will have to do some extra work (through a code generator or T4 template) to rename it to something more suiting like "ParentCategory".

Related

Very simple dillema that has been killing me scaffolding in mvc 3

Summary of the question: How do i scaffold two or more tables with linq to entities.
I cant find an example; they always scaffold only one table.
Details:
If I have two tables and I use LINQ to entities with a t4 template for dbcontext capability like such:
Table1
Name LastName PositionId
Jose j 1
Table2
PositionPrimaryKey PositionId PositionDescription
1 1 MainProgrammer
If I had these table mapped with linq to entities how would I scaffold them?
Then i put Table1 as my Model class.
I have my employeesentities as dbcontext
But that only creates the values for table 1 and not 2.
If I create a new model that contains both entities, it says is not part of Employeeentities and the class could not be modfied to add my new entity.
So, you have a 1:1 relationship between these tables? If yes, I suggest you creating an entity by hand, set its defining query to the necessary join, and map Insert/Update/Delete to stored procedures in the Mapping Details screen. It involves some (quite simple) sql, but it is the cleanest way for your code above.
If it's not a 1:1 relationship, you need to modify the t4 template to conditionally create the fields of the linked property (it has to navigate the property, and based on some condition, like you say "if property is called Table2", create the extra fields). If you have already done so and it doesn't work, maybe there's something going on with the selection of properties used by MVC scaffolding. It might use reflection and choose only primitive types.

Making entity framework treat views with many-to-many relationships, like it does tables with many-to-many relationships

I have three views that I've manually created in the DB.
First view is "Region", the second is "FIPS" and the last is a many-to-many between them called "Region2FIPS". These are all views, and I only need read access the data, so I'm not worried about having updateable views.
I have added each of these views to Entity Framework, and created the appropriate associations between them.
Region to Region2FIPS is a 1 to many.
FIPS to Region2FIPS is a 1 to many.
The "Region2FIPS" view contains only two columns, one called "FIPSID" the other "RegionID". These column are associated with their respective views in the relationships I defined above.
When this type of association is made on tables in the DB, Entity Framework knows that it is a many-to-many relationship and it creates a navigation property on "Region" called "FIPS" that I can use to navigate through the child collection of FIPS. It does likewise for "FIPS" to "Region".
However, when done manually, with views, it does not exhibit that behavior. Instead, my "Region" object has a collection of "Region2FIPS" objects, which each have a navigation property called "FIPS" which is of type "FIPS". And my "FIPS" object has a collection of "Region2FIPS" objects, which each have a navigation property called "Regions" of type "Region".
I assume this has something to do with the fact that I can't create foreign key references on the views, so entity framework doesn't realize the many-to-many relationship. But I thought that if I manually created the many-to-many relationship between the views it would recognize it and properly handle the navigation between the types. Is there a way for me to force it to do this?
It's possible, but the designer doesn't really help you here. You have to do the mapping manually.
One fairly easy way is to use Code First mapping. But this means your model has to be Code First to begin with. If you're writing a new model, just do that.
If you're using DB First mapping, however, you will have to do the mapping manually. Your SSDL will probably already be correct, once you define the "primary keys" of the views. You would then have to remove the "Region2FIPS" objects from the CSDL (not just from the designer!) and manually patch up the MSL.
Perhaps the easiest way to do this would be to use the designer to automatically map real DB tables (not views) with a similar schema and then replace the table names with view names in the EDMX, using the XML editor.

Linq to SQL .DBML Child Property Problem

I have a 1:1 relationship between table 'A' and 'B' in my .DBML. The FK in the database is in place and the .DBML diagram shows an association line between 'A' and 'B'. However, I cannot get the code generator to create a child property in the 'A' entity. All I have is the FK column. In the Association properties, I have ChildProperty set to true. However, the code generator will not create the child property. I have dropped and added the two tables several times.
Anyone have any ideas?
The O/R designer will refuse to create an association property if a primary key is missing on one of the associated tables. Make sure all of your associated tables have a primary key.
Not sure, but I think what you call 1:1 is actually seen by the DBML as 1:* because the list can "have" many of your fk-table, e.g. one empley oyee can have one city, but each city can "have" many employees.
AFAIK a primary key in each table is a prerequisite without which the DBML will not "work". An error is issued when saving it. Your project will compile, but you'll see the errors later. HTH

entity framework returning only one value but the list size is correct

Entity framework returning only one value but the list size is correct
I have a table that does not have primary id and I need to get or select all the values in it.
What I see is when I do the selection with linq the number of objects is correct but it is the first row over and over.
I am simply doing something like this
List<MyValueType> valuesInDB = myDb.MyValueTypes.ToList();
Problem is I may get thousands of rows (which is correct) but the rows all have the same exact data.
I am using VS 2010 and used the wizard to create my EF object.
The problem is that entity framework is not able to work with entity without a key. So if your table doesn't specify a key, entity framework will infer its own. The key created by EF is composed of all non-nullable non-binary columns.
So if you for example have single non-nullable column in your entity which have only very small set of values (like enum) you will be able to load only single entity "per value". The reason is an inner implementation of the context and the state manager which uses Identity map pattern. When data record is retrieved from database, EF will first check an entity key and tries to find an object with the same key in its internal storage. If an object is found it will use that object instead of data record retrieved (despite of different data). If an object with the key is not found a new object is materialized and added to internal storage.
That is the purpose of Identity map - object with given key should be created only once by each context. Identity map is core pattern in ORM.
I wrote about Identity map also in this question.
I would suggest searching for the word "Warning" in your EDM's designer.cs file. It might tell you if Entity Framework is having any issues with your table.
I really can't comment much in the absence of the table design. I tried replicating your problem but wasn't able to do so. Here is what I did:
Created a table with no primary key but it had a unique key on an ID column. Entity Framework was able to infer a primary key and when I fetched the data, I not only got the correct number of rows but also the corrects data in those rows.
Created a table with no primary key and no unique key. Also there was no column called ID. Entity Framework excluded this table in the EDM that was generated. Consequently I wasn't able to query this table at all.This was displayed as a warning in the EDM designer file.
It would be better if you can share the create script for your table.

Implementing identifying relationships with EF4

I'm currently in a situation where I need to delete entities without having access to the associated ObjectContext. I read about identifying relationships and they seem to be exactly what I need: I want to delete an object, once it is no longer referenced by its "parent" object.
I am using Visual Studio 2010 Premium to generate my database from an edmx file. As far as I understand, I need to include the foreign key of my "parent" object in the primary key of my "child" object table. However, I cannot find a way to tell Visual Studio to do this.
Can someone please help me out on this? Am I completely on a wrong path or am I just missing a setting somewhere?
I finally figured it out:
Go to your Child entity and create a scalar property ParentId. Set this property as entity key (making it a primary key, together with your Id property of your Child entity). Next go to your ParentChild relationship and add a referential constraint. Principal for the constraint is your Parent and Dependant is your Child. Dependant property must be the property you just created on your Child (i.e. ParentId). Save everything and you're good to go.
Basically this is described as "scenario 2" in this blog post: http://mocella.blogspot.com/2010/01/entity-framework-v4-object-graph.html
No, you are in the right path. What you need to do is in the EDM designer, after creating your 2 entities (Parent and Child), right click on the Parent Entity and select Add => Association... and then specify Multiplicity and Navigation property names, and click Ok. You'll see that VS create an association in between which will result on a relationship between these 2 table later on when you generate a database from your model.
Do not create a property like ParentID on your Child entity as it will be automatically created by the designer once you create the association.
Furthermore, you can right click on the association in the EDM designer and Select Properties and Select "Cascade" on "End2 OnDelete" option so that the child will be deleted when the parent is deleted.

Resources