How can you use MSCE with WPF 4 and Entity Framework Codefirst? - wpf

I am attempting to create a WPF application, using MSCE and Entity Framwork. (I am new to wpf)
In asp.net mvc 3 I have done similar things simply by giving the data context, the database, and the connection string the same name. I try that in wpf and it does not work (instead it creates a SqlExpress database.
How does one use a MSCE 4.0 database with WPF and codefirst? Is there some sort of secret add-in?
Here is some sample code
in the app.config
<connectionStrings>
<add name="ReportingContext" connectionString="provider=System.Data.SqlServerCe.4.0;provider connection string=Data Source=|DataDirectory|\ReportingContext.sdf" providerName="System.Data.EntityClient" />
</connectionStrings>
from the models folder
public class ReportingContext : DbContext
{
...
}

It was a problem with the nuget packages somehow. I deleted all of them, reinstalled and the problem vanished.

Related

Read connection string from app.config in my DataAccess library project for dbContext

I am working on a VS2012 solution that has got an ASP.NET MVC 4.0 project and multiple class libraries like my Managers, providers and DataAccess projects. My dbContext class is defined inside the DataAccess project.
My aim is to restrict the connection string info in the DataAccess project's App.Config file. I am trying to avoid specifying the connectionString anywhere else in the project as it is my DataAccess classes that would interact with the DB.
Now if I specify my connection string the my dbContext class by hard coding it, my project works fine and is able to read data from DB.
public MyDbContext()
: base(#"Data Source=MYLAPTOP\SQL2012MAIN;Initial Catalog=MyDB;User ID=sa;Password=*****")
{
}
But if I specify the connection string in app.config file like this:
<connectionStrings>
<add name="MyDBConnection" connectionString="Data Source=MYLAPTOP\SQL2012MAIN;Initial Catalog=MyDB;User ID=sa;Password=*****;Connect Timeout=200; pooling='true" providerName="System.Data.SqlClient" />
</connectionStrings>
and use it in my dbContext classs as follows:
public MyDbContext()
: base("MyDBConnection")
{
}
It doesn't work. I tried using the same connection string in my MVC project's web.config file also, but then again I am getting the same error (attached image):
Can anybody please guide me...
Thanks Hari
I think your fault lies at the pooling, so try
Pooling=True also make sure to remove the single inverted comma that you have, '
There is one more point you might want to research on, I think that Pooling is by default enabled, so setting Pooling=True, while this is explicit, if I remember correctly, it has no effect, whereas Pooling=False does have a effect.
To make it work you can try calling the base constructor like this:
public MyDbContext()
: base("MyDBConnection", backend, metadataSource)
{
}
In this case, backend and metadataSource would be the fields in your MyDbContext class, which hold the configuration of the backend and the configuration of the mapping.
In the Creating OpenAccessContext article, you will find more details about the design of the context.

connection string in ADO.NET entity data model

I have a C# console application (say Project.Console) in which I am using a dll of another project (say Project.Data, which is a class library using ADO.NET Entity Data Model to retrieve data from the DB).
I have added the connection string in Project.Data's App.Config file. When I'm trying to access DB in Project.Console by the classes in dll an error (ArgumentException : 'The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.') is showing.
When I tested by adding the connection string in Project.Console's App.Config file also, its working fine.
Why its so? Is it necessary to pass the connection string in Project.Console's App.Config file since no direct DB interaction is there?
From my learning, its only need to pass the connection string in Project.Data's App.Config file.. Am I right..?
the connection string is properly set in the DLL's app.config file.
// Assumsing
connectionString is your connectionString
ModelEntities context=new ModelEntity(connectionString, Assembly.GetExecutingAssembly());
or like this:
1. The model is inside a folder called Models
2. The name of the library is ClassLibrary1
3. The name of the EF model is ClassLibrary1Model
4. connectionString is your connectionString
If you put the model lets in a folder called Model for example:
ModelEntities context=new ModelEntity(GetConnection("ClassLibrary1", "Models", "ClassLibrary1Model", connectionString));

PivotViewer unable to display collection - only shows link to cxml file

I have been trying to develop a pivot viewer dynamic collection (from sample source code). My environment is IIS 6, Windows server 2003 and VS 2010. The collection works fine from VS2010, but when i publish the service through VS2010, the collection does not load. The main page is shown with only the link to cxml file. I have added the three mime entries and clientaccesspolicy.xml is already copied by VS. Is there something missing in my configuration?
Use Fiddler to check the traffic with the app, where does it get stuck, does it get the .cxml file, can it find the dzi and dsc files?
Did you add dzi and dzc mime types as allowed?
You can see this Stackoverflow question to learn how to do this in IIS 6:
How do I get the Silverlight Pivot Server application to run on IIS 6?
Aslo in case you are using the JIT sample code then in the PivotServer project there is web.config file where there are httpHandler configuration values that are meant to be used when running in VS only.
You should remove those when you're deploying:
The Visual Studio Development Server
uses these httpHandler configuration
values. To run the server under IIS,
comment these handlers so that IIS
will use the duplicate set of handlers
in the system.webServer section below.
<httpHandlers>
<add path="*.cxml" verb="GET" type="PivotServer.CxmlHandler"/>
<add path="*.dzc" verb="GET" type="PivotServer.DzcHandler"/>
<add path="*.dzi" verb="GET" type="PivotServer.DziHandler"/>
<add path="*/dzi/*_files/*/*_*.jpg" verb="GET" type="PivotServer.DeepZoomImageHandler"/>
<add path="*_files/*/*_*.jpg" verb="GET" type="PivotServer.ImageTileHandler"/>
</httpHandlers>
Add a handler to the Loading Fail event and look at the exception?
PivotViewer.CollectionLoadingFailed += new EventHandler< CollectionErrorEventArgs >( PivotViewer_CollectionLoadingFailed );

DomainService only works with EDMX file?

I want to add a new domain service class within vs2010. I choose my entity framework database context in the dialog "Add New Domain Service Class", but it doesen't show me the available entities. If I click "ok" VS2010 gives me the following error message:
---------------------------
Microsoft Visual Studio
---------------------------
Value cannot be null.
Parameter name: ssdlPath
---------------------------
OK
---------------------------
The reason for this is, that I don't have an EDMX file (I tried it with an EDMX file and it worked). Instead I have a seperate csdl, msl and ssdl file. This is because I want to support multiple databases.
How can I use the class LinqToEntitiesDomainService with my data context? The entity objects and the data context are generated with the POCO template. If I add the Domain Serive class manually (not using the domain service wizzard) I get a compile time error which says basically the same:
MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Ria.Client.targets(303,5): error : Value cannot be null.
MSBuild\Microsoft\Silverlight\v4.0\Microsoft.Ria.Client.targets(303,5): error : Parameter name: ssdlPath
Can anyone help?
Select YorDoaminContainer.csdl,YorDoaminContainer.ssdl file and the YorDoaminContainer.msl file and then
In Properties, set Build Action to Embedded Resource.
And then make sure you have correct connection string for this three resources.
for example:
<connectionStrings>
<add name="DomainModel"
connectionString="metadata=res://*/NameSpace.DomainController.csdl|res://*/NameSpace.DomainController.ssdl|res://*/NameSpace.DomainController.msl;
provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
Initial Catalog=DB;Integrated Security=True;Connection Timeout=60;
providerName="System.Data.EntityClient" />
</connectionStrings>

Deploying desktop WPF/SQLite application

I'm trying to deploy a desktop application written on WPF 4.0 that uses SQLite through Entity Framework. So i've changed .config file (absolute path to db -> relative) and copied database file itself to app's directory. It seems to be working fine, but on other PC it's throwing an error during runtime. Supplying System.Data.SQLite.dll and System.Data.SQLite.Linq.dll and/or installing ADO.NET 2.0 Provider for SQLite via exe (http://sourceforge.net/projects/sqlite-dotnet2/files/) didn't help. No additional controls/providers etc. used during development. Am i missing something?
p.s. FW 4.0 is present on client machine because VS2010 was installed there and C:\Windows\Microsoft.NET\Framework\v4.0.30319 exists and is ~400mb.
I had the same problem with SQLite and ADO.NET in WPF Application. I solved it in this way:
I added System.Data.SQLite.dll, System.Data.SQLite.Linq.dll and Sqlite.Designer.dll as reffences in my project.
I did right - click on them -> properties -> Copy Local -> true
I added in App.config this code:
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add name="SQLite Data Provider" invariant="System.Data.SQLite" description=".Net Framework Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
</DbProviderFactories>
</system.data>
This was all I did, and it's working.
Good Luck !

Resources