DDL SqlSpecific

Posts   
 
    
Rushmore
User
Posts: 125
Joined: 27-Jan-2005
# Posted on: 22-Dec-2010 11:43:37   

Hello,

I´m trying to modify the DDL generation template but with no luck.

The current DDL SqlSpecific template include columns which are unmapped.

Is there something like "EntityFieldContainedInCurrentEntity" available?

Kind regards, Carlo

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 22-Dec-2010 11:54:52   

Would you please describe in more details, what are you trying to do? How did you approach it? and what was the outcome?

Rushmore
User
Posts: 125
Joined: 27-Jan-2005
# Posted on: 22-Dec-2010 13:22:38   

Walaa wrote:

Would you please describe in more details, what are you trying to do? How did you approach it? and what was the outcome?

I try to modify this pice of template:

<% // the name of the catalog is in the variable 'catalogName', the name of the schema is in the variable 'schemaName'. The DBTable object // is in the variable 'table'.%> CREATE TABLE [<%=schemaName%>].[<%=table.Name%>] ( <%=string.Join(string.Format(", {0}\t", Environment.NewLine), table.Fields.Select(f=>CreateFieldDefinitionString(f)).ToArray())%> ) ON [PRIMARY] GO

I want that the CreateFieldDefinitionString is called only for fields they are mapped in the designer).

How to accomplish this?

I think the CreateFieldDefinitionString is the right place for the modification. There is no intellisense for edit.

Regards Carlo

Walaa avatar
Walaa
Support Team
Posts: 14986
Joined: 21-Aug-2005
# Posted on: 22-Dec-2010 15:46:27   

You should use the project properties for orphaned elements which can auto-delete fields which are unmapped.

If the fields are created by the designer, they're not emitted in the ddl sql. (NonExcludableOrphanedElementDetectedAction)

Rushmore
User
Posts: 125
Joined: 27-Jan-2005
# Posted on: 22-Dec-2010 16:09:59   

Walaa wrote:

You should use the project properties for orphaned elements which can auto-delete fields which are unmapped.

If the fields are created by the designer, they're not emitted in the ddl sql. (NonExcludableOrphanedElementDetectedAction)

The fields aren´t generated from the designer. What I try is to make is a little subset of an existing database scheme. I´ve reverse-engineered some tables from the existing database to the entity definition(s). Then I´ve removed unnecessary fields via edit entity in the designer.

Now I want to generate the create sql script from that project without the unmapped fields. That´s all ...

Kind regards Carlo

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 23-Dec-2010 06:27:51   

Are you using this on v2.6 or v3.0 (AFAIK, they are not ported to v3 yet).

David Elizondo | LLBLGen Support Team
Rushmore
User
Posts: 125
Joined: 27-Jan-2005
# Posted on: 23-Dec-2010 09:10:48   

daelmo wrote:

Are you using this on v2.6 or v3.0 (AFAIK, they are not ported to v3 yet).

I am talking about v3.0 and the built in "Generate Database Scheme Create/Update script (DDL SQL)".

What I want/try is, that these scripts are generated from the project definition and not from the meta data.

The "_currentProject.Entities" collection doesn´t exist anymore.

Any suggestions?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39760
Joined: 17-Aug-2003
# Posted on: 23-Dec-2010 11:02:09   

The template you mentioned is an include template. It's included in the createscript.lpt template (line 127). This means that the project object is available to you, as _currentProject.

In scope you have the project (as _currentProject) and the table (as table). What you need to obtain is all mappings which target the table. There's no public method to directly obtain this information, as everything is focused the other way around or internal.

You can obtain this information however, by obtaining the DatabaseMappingData object for the DriverId in the executingGenerator. Do this by using var databaseMappingData = _currentProject.MappingData.MappingDataPerDriverID.FindByKey( _executingGenerator.DriverID).FirstOrDefault();

If it returns a value, there are mappings for the driverid. You can assume this is the case. Then, in this object, simply do a linear search using a linq query: var mappings = databaseMappingData.EntityMappings.Cast<GroupableModelElementMapping>().Where(m=>m.MappedTarget == table).Union(databaseMappingData.TypedViewMappings.Cast<GroupableModelElementMapping>().Where(m=>m.MappedTarget==table);

now you have the mappings for this table. You can then traverse the FieldMappings in this mapping to obtain all mapped fields, and thus skip ones which aren't mapped.

It is of course logical you should do this a bit more clever than I did above, i.e. cache the mappings perhaps in a dictionary so you don't have to traverse the mappings every time you handle a table, but perhaps it's quick enough for your project.

Frans Bouma | Lead developer LLBLGen Pro
Rushmore
User
Posts: 125
Joined: 27-Jan-2005
# Posted on: 23-Dec-2010 11:15:01   

Thank you,

I will implement it asap.

Kind regards, Carlo