OneToMany relationship accessed in both side? - entity-framework-4.1

In entity framework.. when you apply an association in fluent mapping in one side of the association does both sides of the association get automatically generated by EF ?
If you take as example an OneToMany relationship. If i apply the association between X and Y in the fluent mapping configuration of X does this also apply at the Y side ? Will i be able to access X on the Y entity using a navigation property ?
Does Y will be able to access X and vise versa.
Thanks.

Yes. You configure actually a one-to-many relationship with Fluent API, not a property on one of the entities or any "access" rules of the navigation properties. If you have navigation properties on both sides of the relationship, you can configure the relationship for entity X or Y. It doesn't matter which one and you need to do it only for one, not for both. If you have only one navigation property exposed in an entity, say X, you must configure the relationship for entity X.

Related

How to draw multivalued attribute in MySQL Workbench

I am following a book on Fundamentals of Database Systems by Elmasri & Navathe. In the book there is an Entity called DEPARTMENT and it has a multivalued attribute called location. I could not figure out a way to represent location in my ER Diagram which should have an identifying relation with the department entity. Should I draw an Entity named LOCATION and create a relationship with the DEPARTMENT entity or is there another way to represent multivalued attribute in MySQL Workbench.
The EER implementation in MySQL Workbench is not a full implementation the relationship model. It rather focuses on things that can be represented in a MySQL database. That means things like inheritance or multi-valued attributes cannot be modelled there. And I'm also not aware of a way to simulate that, unless you use another entity that has all the attributes which the original attribute needed.
In your case it makes sense to have a location entity that has a relationship with the department entity. Not sure if that should be an identifying relationship, since a location is not mandatory, is it? E.g. it could be a department is stored that no longer exists and hence has no location.

Unwanted Navigation property appearing in entity framework

I am using Entity Framework to create a Model, I am pretty new to the Entity Framework so bear with me.
I want my database to always be the end all be all of what gets generated, so I dont wnat to make modifications to the model itself. I want to amke all modifications to the database and just hit "Update Model From Database." This has been working swimmingly!
However If I have a one to one relationship between two tables, and I have a foreign key constraint set in the database, I get a navigation property in the child table that goes back to the parent table.
So if I want to access the parent from the child I can do child.parent.fieldName
That sounds great in theory but my issue arises when I need to serialize the object for JSON created by the entity Framework. I always get an error because it tries to serialize the parent object along with the child object. which usually has an invalid state at this point so.. A) it cant be serialized and B) I wouldn't want all that extra info anyway.
Am I misconfiguring the database in some way? is there a way to have the database specify that I only want Parent.Child Navigation properties in the model? and not Child.Parent?
Am I misconfiguring the database in some way? is there a way to have the database specify that I only want Parent.Child Navigation properties in the model? and not Child.Parent?
No. Database knows nothing about your intention to use some tool on top of it. If you don't want the navigation property you must change it in EDMX by deleting it but sometimes you want the property but you also want a serialization. In such case you must either modify strategy used to generate your classes (if you are not using T4 templates it will be quite hard) to use some special attributes dependent on used serialization API to mark some properties as not serialized.
The correct approach is not serializing entities but creating special data transfer objects filled from entities which will contain only properties/data you are going to serialize so you will not get into this problem.

Association in Linq to SQL

I have a pretty simple model with a customer and Item, with one-to-many relation between them (One customer can have many items). I used the designer to place my entities and I do see an association between them in the designer. Its xml reflects this:
<Association Name="vgMfiCustomer_vgMfiItem" Member="vgMfiCustomer" ThisKey="CustomerLink" OtherKey="Customer" Type="vgMfiCustomer" IsForeignKey="true" />
Trouble is that in the designer.vb, there's no mention of this association, So I cannot say Customer.Items in my code. Did I miss a step in generating the model? Or maybe I need to add the navigation property manualy? I come from EF.NET background, where the navigation properties are created automatically.
It sounds like you have the association setup for one direction, but not the other. In the dbml, there should be an Association element for each type. You may want to try removing the association in the designer and re-adding it.
Ensure that both tables have a property marked as "Primary key" (in .dbml schema)

Difference Between EntitySet and EntityRef

I want to know what really is the difference between EntitySet and EntityRef in LINQ-to-SQL. As per what I a seem to have understood, EntitySet is one-many or many-many relation and EntityRef is one-one. Correct me if I am wrong.
If the associated class is the many (child) side of a one-to-many relationship, the many class will be stored in a collection of the many classes, where the type of the collection is EntitySet , and T is the type of the many entity class. This collection will be a member variable of the one class.
If the associated class is the one (parent) side of a one-to-many relationship, a reference to the one class will be stored in a variable of type EntityRef , where T is the type of the one class. This reference to the one class will be a member variable of the many class.
therefore, EntityRef & EntitySet act one-to-Many relationship,EntitySet act the many in parent and EntityRef act as One in Child.
recently caught myself in the same doubts, so according to this answer you are right

Linq2Sql: linking tables without relationships

I'm new to Linq2Sql and to learn it I'm trying to setup reports using Linq2Sql against the FogBugz tables. But, since the FogBugz tables don't have any true Foreign Key relationships, LinqToSql isn't finding the relationships and I can't figure out how to create them through the Designer. So, is there a way to do this manually? If I do it manually and then later decide to add another table, will everything I did be overwritten?
You can add relationships (aka Associations) in the SQL2LINQ designer by right clicking the white space in the designer, choose Add -> Association.
Choose your parent and child tables (classes), then link the properties on the tables accordingly using the grid that appears below the dropdowns for parent and child classes