Entity framework metadata exception - No csdl, ssdl, msl in dll resources - entity-framework

I have DAL (model first entity framework 4.1) and Service which is using it in separate projects. Everything was working fine, but after some minor changes (for example I generated model from database) it stoppedd working. I am now getting metadata exception.
After many hours of research I downloaded ILSpy and checked that inside DAL.dll there are no resources.
My connection string looks like:
metadata=res://*/DataModel.TerminalRegistryModel.csdl|
res://*/DataModel.TerminalRegistryModel.ssdl|
res://*/DataModel.TerminalRegistryModel.msl;
ANd in EDMX file metadata artifact processing is set to Embed in Output Assembly.
What can cause my problem?

The standard metadata string looks like this:
metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl
And this works fine in most cases. However, in some Entity Framework get confused and does not know which dll to look in. Therefore, change the metadata string to:
metadata=res://nameOfDll/Model.csdl|res://nameOfDll/Model.ssdl|res://nameOfDll/Model.msl

Related

Entity Framework Connection strings issue

im developing a class library (dll) with visual studio 2012, this library contains a model first created database, with the connection string setted on its app.config file.
Now, im creating a console app (actually a test, but for this its pretty much the same) with an added reference to the dll project.
At first i thought the console app didnt need to know anything about the database, since it was using the dll, however i got a connection string missing error. Fine i added it and it worked, but this isn't what i want, i need the dll to encapsulate all the database related stuff.
Also, what bothers me the most, is that the connection string is also needed on BOTH the dll and the app, even the .mdf file(im using local sql server) generated in the dll/bin/debug folder is needed and copied over to the app/bin/debug folder so i need the file to be on both sides...
How can i make the dll to work by itself, using its own config file and not the app config file? And no, using some kind of service is not an option, it needs to be a dll!
in your database class you can add your connection string like this as base parameter
public class mydb:DbContext
{
public mydb() :base("Server=....")
{
}
}
or you can add your Connection string to app.config and just put its name in base
Example:
public mydb() :base("ConnectionstringName")

How do I stop Entity Framework from generating BOTH ObjectContext and dbContext

Background
Visual Studio 2012
NEW Model.EDMX file created in VS2012
Copied some of the EDMX xml from a previously created EDMX into the new one
Problem / Question
Now. The EDMX (TT transform, custom tool, whatever, etc.) is generating BOTH DbContext classes (under the Model.tt/Model.Context.tt files) and ObjectContext classes (via the Model.designer.cs file). See image below:
Everything builds fine and works with the DbContext (but obviously only if I delete the Designer.cs file just before building) but the Designer.cs - and its ObjectContext-based code - keeps reappearing. How do I stop this behavior?!
I didn't find out how to stop the Designer.cs file from generating the ObjectContext, but I did figure out how to make it so that it doesn't matter. Just set the Build Action to "None" instead of "Compile".
The .tt files will generate the code regardless of the code generation strategy in the .edmx. They listen to the .edmx file changes. At least this is how they are working for me.
So by turning the code generation strategy to None in the .edmx you make the .designer.cs file empty of any useful content.
Then open up the project file, find the nodes representing the .edmx, by default it is contained in EntityDeploy node ie the Build Action value, and remove its Generator subkey.
On normal EF Code First projects, Code Generation Strategy is "None" and ObjectContext is not generated.
It appears in your case that you Code Generation Strategy set to "Default".
To stop generating ObjectContext in xxxx.Designer.cs, go to your edmx file, and change your Code Generation Strategy from "Default" to "None".
If you inspect xxxx.Designer.cs
// Default code generation is disabled for model 'C:\Users\xyxy\xyxyxy.Web\Models\xyxy.edmx'.
// To enable default code generation, change the value of the 'Code Generation Strategy' designer
// property to an alternate value. This property is available in the Properties Window when the model is
// open in the designer.
I think the issue is having an EntityModelCodeGenerator value in the Custom Tool field of the .edmx file properties. Just delete that value.
I think this is it, because I notice that in the project that I started off as an EF6 project, there is no value and it has no .designer.cs file, but in the two that I upgraded from EF4, they both have the value and the designer file.
(This equates to a <Generator> tag in the underlying .csproj file.)

Howto customize pluralisation for Entity Framework 5

As my database was designed using german table- and column names, the default pluralisation feature of entity framework doesn't work for me.
I have found a couple of resources where this is discussed, but none of them seem to work.
What i have found:
There is the PluralisationService where i can add the mappings:
PluralizationService pluralizer =
PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us"));
ICustomPluralizationMapping mapping = ps as ICustomPluralizationMapping;
mapping.AddWord("Tabelle", "Tabellen");
But what's next?
I have tried to:
EntityModelSchemaGenerator generator = new EntityModelSchemaGenerator(container);
generator.PluralizationService = pluralizer;
generator.GenerateMetadata();
and put both of them in my POCO T4 Template. But it throwed the following exception:
The EntityContainer 'ContainerName' is not a store EntityContainer. Parameter name: storeEntityContainer
at System.Data.Entity.Design.EntityModelSchemaGenerator.Initialize(...)
at Microsoft.VisualStudio.TextTemplating...GeneratedTextTransformation.TransformText()
To completely customize the table names in EF Code First, you can use the Table attribute to explicitly specify the name of the table associated with a class:
[Table("InternalBlogs")]
public class Blog
{
//...
}
I'm also looking for the same thing. Maybe this can help. I'm just not willing to pay for such a basic feature.
EDIT:
The code you posted is to be used with EdmGen2 wich will give you CSDL, SSDL or MSL files pluralized according to your class.
A very old question, but if someone is still looking for a possible workflow/solution:
I had a similar problem where I wanted to customize the schema import (CSDL) from the database. The solution / workflow was as follows:
Deployed the database schema (I used Visual Studio Database Project VS 201x) to a
local database
Imported the database model using EDMGEN to create the CSDL, SSDL and MSDL files
http://msdn.microsoft.com/en-us/library/vstudio/bb387165(v=vs.110).aspx
Modified EDMGEN2 with my changes on how to handle pluralization and naming with custom rules and created EDMX file
Ran the T4 templates (with additional customization as needed) to create
output.

EntitySet could not be found

I'm working on an MVC site using EF4 as my data source. I've had no problems with other projects with EF4 until today. I'm getting an error stating EntitySet name Entities.Sites could not be found. I have another EDMX file for another dataset that is set up nearly the same (with different entity names) and it works fine when I try to run a LINQ query against it.
My EDMX has 2 tables (site and page) with a 1 to many association from the site to page table. Everything compiles fine, but in runtime, it's almost like the datamodel fails on every property list population. I do have Lazy Loading enabled (It's a .NET 4 project).
I stepped through the code on the designer class and it fails when it gets to a call like this
if((_Pages == null))
{
_Pages = base.CreateObjectSet<Page>("Pages");
}
I have not altered the code in the designer.cs. I'm at a major wall with this.
Any ideas what I could be doing wrong?
Try renaming your entity Page to something else, like EntityPage (for example) to see if this resolved it. As stated by #Yakimych this could simply be a case of Namespace clashing

Where should I put my connectionString in ASP.Net 3.5?

I have two projects:
ASP.Net 3.5 website (frontend, UI)
VB Class Library (dataaccess logic)
Where should I save my connectionString, so that I can use if from the class library? And how does this affect where it is placed when I deploy/publish?
Note:
I don't want to pass it to every function in my VB Class
Depending on how you constructed your DAL -- LINQ, TableAdapters, etc. -- it may automatically look for it in the web.config file. If you created the DAL via a designer, likely it stores the default connection string in the app.config file for you class library. I copy the connection strings section from the app.config file to my web.config and change the connection string to the correct database (I have separate web.config's for DEV/QA/PROD). This makes it trivial since the designer generated code already has the code implemented to retrieve it from the configuration file.
If you are hand-coding your DAL and need to pass in the connection string, I suggest setting up a strongly-typed configuration class that interfaces to the web.config and does lazy loading of your configuration values. Use a factory to create your DAL and inject the configuration class into your factory via constructor so that it knows how to create your DAL with the connectionsString retrieved from the configuration file.
My question came from having spent half a day of trying to make this work, but I kept getting the wrong connection when deploying (where I use another database).
My problem was, that I was using
My.Settings.DefaultConnectionString
...To retrieve the connectionString in my VB Class Library.
After following tvanfossons anwer, I dug around some more and found out, that I can simply use (after referencing system.configuration) :
System.Configuration.ConfigurationManager.ConnectionStrings.Item("DefaultConnectionString").ConnectionString
It looks in the web-config for webapplications and app.config for windows/class library apps.
I am glad it now works, but even more glad I know why. ;)
I had the same issue you were having and I ended up using the System.Configuration.ConfigurationManager class to obtain the connection string stored in my web.config file from my class library like Kjensen's answer suggested. This worked wonders, if I had more experience I would vote that answer up.
I needed the connection string to build my Linq2Sql data context, which this method provided me with.
I now build my data context like below (remembering to add a reference to System.Configuration) -
public MyDataContext() : base(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"]
.ConnectionString, mappingSource)
And as long as the web.config file contains "MyConnectionString" the configuration manager takes care of the rest.
We keep ours in the machine.config of each server and have a custom DAL to handle all DB interaction for our web apps.
Put it in the web.config in the connection strings section.
In the VB project use HttpContext.Current.GetSection to retrieve the section.
A fellow developers idea once was that we should store all the connection strings in a database table.
Don't try doing that. You won't get very far. :)