Configuring a N:N relationship with a lookup of source entity type on target entity - dynamics-crm-2013

I'm trying to configure an N:N relationship from EntityA to EntityB. On EntityB, I have a lookup field of type EntityA.
Now, when I go back to an entity of type EntityA (say, named My Entity A) and add an existing EntityB in the subgrid, the look up field on EntityB is updated with My Entity A. Which is clearly not what I want.
I simply want to be able to tie any number of EntityB's to EntityA and I want EntityB to be able to point to any other EntityA on its look up field.
Is this possible ?

If you have a reference to entity A on Entity B then that is a N:1 relationship, not an N:N relationship. Check that you don't have more than on relationship between the entities, and it is defined as a N:N.

Related

Hibernate Many-to-Many Mapping with seperate dto class

can any one tell how to get hibernate Many-To-Many mapping with different dto files means suppose there are student and teacher relationship in that i want -
1) Student Dto class
2) Teacher Dto class
3) Student_Teacher Dto class and in this class all mapping are there
is this possible and how to do it ??
You need to annotate collection attribute with #ManyToMany annotation on each of the entities. In your case you will want to annotate Student and Teacher entity. Student_Teacher table will be created automatically if you are using schema export or you can use #JoinTable to specify join table details. This join table will have all the mappings.
For more details I have tried to explain #ManyToMany mapping here in simple way.

Specifying the Table on a HasMany() relationship mapping in FluentNHibernate

I have a mapping in FluentNHibernate for a HasMany relationship and I'd like to specify a Table on it to override the default Table that nHibernate will look in to find those objects that I have many of. Does that make sense?
So lets say I have a table for Invoices and a table for InvoiceItems and lets say I have table called InvoiceItemsTwo.
I have a class for Invoice and a Class for InvoiceItems as well, and their mappings are pretty straight forward. I'd like to specify in my mapping for Invoice, that it should look for it's items in InvoiceItemsTwo instead of the default InvoiceItems.
So my mapping of that relationship looks like this
HasMany(c => c.InvoiceItems).Cascade.SaveUpdate().Table("InvoiceItemsTwo");
But this doesn't work. I keep getting an error from my website at runtime that says Invalid object name 'InvoiceItems'.
Why is it ignoring the fact that I am explicitly specifying the Table in my mapping on the relationship?
I tried dumping the mapping at run time and it's being setup something like this
<bag cascade="save-update" table="InvoiceItemsTwo">
Any ideas?
The table attribute applies only to many-to-many relationships, not one-to-many.
you can't specify a different table in your mapping class. Fluent NHibernate uses the class mapped on the property list (InvoiceItems).
If yoy want to use another class to map your details table you must create a InvoceItemsTwo class and map it in your master table class.
You could map the list as composite-element instead of a one-to-many relation and then map it to another table. But it is not a good idea. Consider that NH needs to know where to store an object which is in memory. So it may happen that the object is stored in the wrong table.
Either store all the InvoiceItems in separate tables using composite-element instead of one-to-many and components instead of many-to-one (however this is called in Fluent).
Or store all the InvoiceItems in the same table and use regular references.

Fluent NHibernate, Many-to-Many, setting a property on the child from the many-to-many table

I have a Subscriber object that contains a list of Provider objects. Providers can belong to many Subscribers, hence the many-to-many relationship. This is fine, except that the Provider needs to define a Status property but this cannot be stored in the Provider table, as the same provider could have a different Status for different Subscribers, so I am storing the Status in the many-to-many table. At the moment I have a basic many-to-many mapping:
HasManyToMany(s => s.Providers)
.Table("SubscriberProviders")
.ParentKeyColumn("SubscriberID")
.ChildKeyColumn("ProviderID");
How can I set the Status property, of the Provider, within the many-to-many mapping?
Many thanks
A many-to-many mapping can't have properties of its own, so you have to map the join table into an artificial ProviderSubscriber entity, which will be one-to-many from the Provider.
For a full example of a workaround, see Many-to-many relationships with properties
You'll have to map the cross-reference table (which NH currently generates for you), and change the mapping between Providers and Subscribers to instead be a HasMany() on either side referencing the cross-reference table.

How to cascade save and delete of a non-collection entity?

I have an entity A who has entity B.
Class Entity A
{
public EntityB;
}
Class Entity B
{
public Entity A;
}
Entity B has one to one relationship with A. I am trying to use cascade save,delete when entity A is saved so that I don't have to manually save entity B. It should be done automatically.
My mapping for entity B looks like:
<many-to-one name="EntityA" cascade="save-update"
column="EntityASomeProperty" class="EntityA" />
I not able to save entity B automatically when A is saved.
It looks like you have a cascade defined from B to A, so that when you save EntityB, EntityA should be saved.
If you want EntityB saved when you save EntityA, you'll need to have that configuration reversed. Note that you should pick a direction in which you want to manage this relationship and always work from that direction. You can map both directions, but make one inverse so that hibernate knows which direction you intend to manage it from.
I'd also suggest you use a OneToOne mapping, if that is what it really is.

How would you model a "default child" flag with an ORM?

I'm using an ORM (SQLAlchemy, but my question is quite implementation-agnostic) to model a many-to-many relationship between a parent class and its children.. I was wondering, what would be a simple way to express the concept "one of the children is the default/main one"?
For example, I'd need to persist the following:
This Person instance has Address X and Y, the main one is Y.
I saw this implemented using a "middle" class like "PersonAddressRelation" that would contain "Person", "Address" and the "main" flag, but I think it looks a bit cumbersome.. Is there a better way?
The simplest way would be to have a join table, PersonAddressRelation, and also a DefaultAddress column on the Person table that keys to the Address table.
A couple of remarks.
M:N relationships don't specify 'parent' and 'child', as there's no parent nor a child: there are simply two entities having an m:n relationship via a 3rd entity (the intermediate entity).
'Address' is in general not a valid entity type, as semantically it has no identity, similar to a 'name' has no identity (first name, last name). You'll see this when you look at re-using an entity instance of type Address: you won't do that in general. (though you will re-use a Customer entity instance for example, when the customer has multiple orders)
You want to specify an attribute on the M:N relationship (default), as it belongs there. This means that the relationship itself forms an entity (which is the intermediate entity, often it has just two FK fields forming the PK). This is called an 'objectified relationship', as the relationship itself is seen as an entity. Other examples of this are Employee m:n Department and you want to specify the StartDate an employee started for a department the employee works for.
So in general: create the intermediate entity, as it in general should be there, and add the attribute there. In this particular case with Address, be really sure you are re-using Address instances among related entities (Person). If not, merge Address with Person OR if a person can have multiple addresses, create a simple 1:n relationship between Person - Address, to normalize it out, though don't be afraid to merge address data into the entity it is related to, as often address data is really not re-used (so your m:n relationship is really not there: there's no Address instance which is related to multiple person instances.