Get the attribute representing the FK value when on the correspondend navigator

Posts   
 
    
chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 18-May-2011 17:26:40   

Hi all,

I am using LLBLGen Pro 3.1 latest in conjunction with MS Visual Studio 2010 SP 1, MS SQL Server 2008 R2, MS EF 4.0 and MS .Net Framework 4.0 on MS Windows 7 SP 1. Now I am looking for a possibility to get the fk attribute while i am on the navigator. What I want to do is to set an (.Net) attribute on the one side of a navigation if the cardinality of the navigation is [0|1..N]. What I actually do is:

if((entity.Fields.Where(f => f.Name == relationshipInfo.RelatedEntity.Name).FirstOrDefault()).IsOptional) { do something ... } else { do something else ... } [Header of navigation property follows here].

This works as long as the name of the fk attribute has the same name as the entity to which it points. I would be happy to get some hints in how to solve this more generic.

Thanx in advance,

regards

Chris

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-May-2011 06:46:14   

Hi Chris,

chrissie wrote:

Now I am looking for a possibility to get the fk attribute while i am on the navigator. What I want to do is to set an (.Net) attribute on the one side of a navigation if the cardinality of the navigation is [0|1..N].

What is the side of the relation you want to set the attribute into its navigator? (1-side or M-side) What is the exact condition to emit this?

chrissie wrote:

if((entity.Fields.Where(f => f.Name == relationshipInfo.RelatedEntity.Name).FirstOrDefault()).IsOptional)
{
do something ...
}
else
{
do something else ...
}
[Header of navigation property follows here].

So you do this already? I assume you modified the templates, right?

chrissie wrote:

This works as long as the name of the fk attribute has the same name as the entity to which it points. I would be happy to get some hints in how to solve this more generic.

What do mean by "more generic"?

Have you seen the "Code gen meta-data defaults" subtab at Project Properties? There you can set default attributes for model objects. For instance you can set a default attribute for NavigatorSingleValue (1-side) or NavigatorCollection (m-side). All objects of that type will implement that attribute, but you can remove it or add more per object basis. You just have to edit the entity, go to Code-gen info and select the navigator object you want. See this for more info.

If you need more specific control over the condition to generate such attribute, you can code a plugin that make the work for you (select the navigator candidates and then assign an attribute to them).

Finally, you can modify the templates, but this should be the last option, because you are modifying the core templates, and you may need to adjust them when you update the LLBLGen build.

David Elizondo | LLBLGen Support Team
chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 19-May-2011 10:29:18   

Hi David,

thanx for answering. And yes, your assumptions are mostly right. We changed the templates to our needs. And yes, merging them with new or corrected templates provided by solution design is no fun. But it works. I am on the many-side but look to the one-side. And I have to distinguish between 1 and 0|1 case. In case of 1 i will set another attribute as I do in 0|1 case (always and only on the 1-side).

So I am on top of the 1-side-navigator. How can I distinguish between 1-case and the 0|1-case even when the name of the foreign key attribute changes (this is what I mean by "more generic" because i actually look for an attribute which has the same name as the entity to which it points. But this is only an assumption, the name of the forei). And I am not in the loop

FOREACH(IFIELDELEMENTCORE FIELD IN ENTITYFIELDS)

I am in the loop

FOREACH(VAR RELATIONSHIPINFO IN ALLRELATIONSHIPINFOSTOTRAVERSE)

Hope I could explain my needs better, thanx in advance

regards

Chris

MTrinder
User
Posts: 1461
Joined: 08-Oct-2008
# Posted on: 19-May-2011 22:32:36   

Hi Chris

I'm still not clear about WHY you need to set this attribute - what is it used for ? Knowing this may make it easier for us to suggest a solution to your issue ?

Thanks

Matt

chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 20-May-2011 09:50:01   

Hi Matt,

I want to generate Attributes on a navigation property if it represents the one-side. Therefore I have to distinguish between 1 and 0|1 because if I have a 1 one-side the value of this property must not be null otherwise it can be null. To make the decision I use actually the I wrote down in my first message. The depends on the name of the foreign key attribute. If the name of the foreign key attribute is the same as the entity it points on, this code works well. But we want to rename our foreign keys. So I need another way to determine if the current navigation property is a 1 or a 0|1 one.

regards

Chris

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 20-May-2011 11:26:06   

I assume the code you posted above is in the template? The difference between 1 and 0|1 is determined on the fact whether 1 or more of the FK fields is optional. If that's the case, the relationship side is a 0|1, otherwise it's always mandatory.

var isOptional = normalRelationship.GetFkFieldsFromFkSide().Any(f=>f.IsOptional);

tells you whether 1 or more fk fields in the normal relationship is optional. The normal relationship also has properties called 'StartEntityIsOptional' and 'EndEntityIsOptional' which you can also use. You then first have to determine which side is the 'fk side', so it comes down to: var isOptional = normalRelationship.StartEntityIsPkSide ? normalRelationship.EndEntityIsOptional : normalRelationship.StartEntityIsOptional;

or something like that. Keep an eye on the relationshipinfo object returned by the generatorutils class, it has a lot of info too you might want to use.

In v3.5 a rule engine is in place which allows you to specify a rule based on properties of an element for an attribute, so you can define it on the project level for navigator single item and simply specify as rule that the side has to be optional.

To merge templates easier, do the following: - keep the templates you merged the changes into your own around - do a diff with newer released templates with this set. - port all differences over to your own templates, then keep the newer templates around for next update.

This way work is minimal, as changes are often small.

Frans Bouma | Lead developer LLBLGen Pro
chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 20-May-2011 12:16:07   

Hi Otis,

these are exact the information I expected to get.

Thanx and regards

Chris