EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
wombat
Posts: 4
Joined: Mon 15 Dec 2014 14:26

EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by wombat » Mon 15 Dec 2014 15:26

We want to have multiple models in a project. Each model should reside in it's on subfolder. We use the Studio Plugin.
If we select Metadata Artifact Processing = "Embed In Output Assembly" then we find the following files in the \obj\Debug folder:
Folder1.Model1.csdl
Folder1.Model1.msl
Folder1.Model1.ssdl


So the correct connection string would be something like this:

Code: Select all

(..)metadata=res://*/Folder1.Model1.csdl|res://*/Folder1.Model1.ssdl|res://*/Folder1.Model1.msl;(..)
which is generated correctly in the app.config, too.

With the dbcontext Template and "Save connection string in App.Config" = false the connection string in the context file constructor now is

Code: Select all

(..)metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;(..)
which seems to be wrong (and produces a runtime error because of the missing "Folder1" part). Can you confirm this behavior?

The second question would be: How do we get the "Folder1" part (seems to be the complete folder path to the model in the project) in the dbcontext template? We have to customize the context file generation, because we want to have one single connection string in the App.Config. So we must add the metadata to this string for each context. Our code already works if all models are in the project root.

For better performance the res://*/ should be replaced with the res://MyDefaultNamespace/, is that right?

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by Shalex » Wed 17 Dec 2014 17:00

CopyToOutputDirectory means placing mapping files into the output folder.
EmbedInOutputAssembly means embedding mapping files (.csdl, .ssdl, and .msl) into an output assembly.
wombat wrote:If we select Metadata Artifact Processing = "Embed In Output Assembly" then we find the following files in the \obj\Debug folder:
Folder1.Model1.csdl
Folder1.Model1.msl
Folder1.Model1.ssdl
Looks like the *.csdl, *.msl, *.ssdl files were generated when your model had the MetadataArtifactProcessing="CopyToOutputDirectory" setting.

Assuming that I created the *.edml model in the Folder1 folder in my project, the designed behaviour would be (depending on the MetadataArtifactProcessing value):

1. MetadataArtifactProcessing="CopyToOutputDirectory"
a) connection string

Code: Select all

(..)metadata=.\Folder1.Model1.csdl|.\Folder1.Model1.ssdl|.\Folder1.Model1.msl;(..)
b) Folder1.Model1.csdl, Folder1.Model1.msl, Folder1.Model1.ssdl are created in the \bin\Debug folder

2. MetadataArtifactProcessing="EmbedInOutputAssembly"
a) connection string

Code: Select all

(..)metadata=res://*/Folder1.Model1.csdl|res://*/Folder1.Model1.ssdl|res://*/Folder1.Model1.msl;(..)
b) there are no Folder1.Model1.csdl, Folder1.Model1.msl, Folder1.Model1.ssdl files in the \bin\Debug folder because they are embedded into application assembly

If you are experiencing a different behaviour, please send us a small test project for reproducing the issue.

wombat
Posts: 4
Joined: Mon 15 Dec 2014 14:26

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by wombat » Thu 15 Jan 2015 16:54

Sorry for the delay.

Maybe I gave too much information of our situation and simply mixed up CopyToOutputDirectory & EmbedInOutputAssembly on typing, which has led to confusion. But this is not so important. I would like to cut it down to the second question:

Where do we get the "Folder1" substring from within the DbContext template if Entity Developer generates a connection string which contains

Code: Select all

(..)metadata=.\Folder1.Model1.csdl|.\Folder1.Model1.ssdl|.\Folder1.Model1.msl;(..)
We want to have one "base" connection string without metadata.
We want to customize the code generation to use this base connection string and add the corresponding metadata for each context. We have enough information about the metadata (model name and the string constants) to concat the string except for the path qualifier prefix ("Folder1" below the project root in this case) before the model name:

Code: Select all

Metadata = string.Format(@"res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", modelName)
We fill modelName from the "baseFileName" in the DbContextTemplate.
Where do we get the "Folder1" from?

This would help us a lot. Thank you.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by Shalex » Mon 19 Jan 2015 13:47

wombat wrote:Where do we get the "Folder1" from?
"Folder1" is a directory where the model is placed in.
Try using the model.FullFileName property which includes the full path to the model file. Does this help in your approach?

wombat
Posts: 4
Joined: Mon 15 Dec 2014 14:26

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by wombat » Wed 21 Jan 2015 13:28

Sorry, it does not. :cry:

We don't need the full path to the model file, we need the relative path below the project root. We could try to substract the full path to the project from model.FullFileName, but the well known "this.Host.ResolvePath()" does not seem to work: Host is null in the DBContext Template (of course with hostspecific="true"). So we don't get the project path.

How does Entity Developer itself create the realtive path in the metadata in the connection string in app.config?

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by Shalex » Fri 23 Jan 2015 18:22

wombat wrote:We could try to substract the full path to the project from model.FullFileName
Please try this way.
wombat wrote:How does Entity Developer itself create the realtive path in the metadata in the connection string in app.config?
Entity Developer uses Visual Studio's internal variables.

wombat
Posts: 4
Joined: Mon 15 Dec 2014 14:26

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by wombat » Mon 26 Jan 2015 14:24

:shock:
Shalex, don't get me wrong. I appreciate any help I can get.
But as I wrote this in my last post: Where do we get the path to the current project from? I mean...this is a TEMPLATE. We want to use it in different projects. So I don't want to hardcode the path. And the only way I know of is to use
this.Host.ResolvePath
.
https://msdn.microsoft.com/de-de/libra ... 10%29.aspx

This works fine for common T4 files in Visual Studio. Using an Entity Developer T4 file (the DbContext template in this case) "this.host" is null/empty/nothing:
We could try to substract the full path to the project from model.FullFileName, but the well known "this.Host.ResolvePath()" does not seem to work: Host is null in the DBContext Template (of course with hostspecific="true"). :arrow: :arrow: :arrow: So we don't get the project path.
So there is no use in writing "Please try this way." without telling me an alternative or what I'm doing wrong.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by Shalex » Tue 27 Jan 2015 14:37

wombat wrote:This works fine for common T4 files in Visual Studio. Using an Entity Developer T4 file (the DbContext template in this case) "this.host" is null/empty/nothing.
Devart Entity Model (*.edml) uses a different object model comparing to Microsoft's ADO.NET Entity Data Model (*.edmx).
We will investigate the possibility to support the this.Host.ResolvePath() method in *.edml and notify you about the result.

Shalex
Site Admin
Posts: 8248
Joined: Thu 14 Aug 2008 12:44

Re: EF: Model in subfolder and Metadata with "Embed In Output Assembly"

Post by Shalex » Thu 29 Jan 2015 15:35

Please upgrade to the new (5.7.539) build of Entity Developer. Now you can obtain a relative path with the model.GetHostRelativePath() method.

Post Reply