DomainService only works with EDMX file? - silverlight

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>

Related

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));

How to force the Entity Framework 5 DbContext to re-read connection strings from app.config?

I am trying to allow users to type in the data source into a dialog which I am then writing to the app.config for the application. The scenario I am having a problem with works like this:
1) User types the data source into my dialog and the dialog adds or updates the connection string. In this scenario the user typed the wrong data source the first time.
2) The user then opens the dialog that has the EF code (which inits the connection string stuff) and EF throws an exception that it cannot connect.
3) The user goes back into the original dialog and puts the correct data source name in and the app.config is again updated.
4) The user the opens again the dialog that triggers the EF code and EF still has the old data source in there although it is correct now in the app.config.
I have tried
ConfigurationManager.RefreshSection("connectionStrings").
I have tried running
MyEFContainer.Database.Initialize() as well.
Neither seems to work. What do I need to do to have EF refresh the connection string data without forcing the user to close and reopen the app? If they do that then it works.
I would always work with a connection string in memory. Keep it e.g. in a context factory. Initially you read it from the config file, but it may get replaced by a new one. You store the new one in the app.config (when it is valid) for the next run of the application.
Are you instantiating a new instance of the DbContext class after the connection string is corrected? Reusing the DbContext instance that initially failed would be the problem.
For the moment,I think to force EF to reload app.config .After we change the app.config the
ConfigurationManager.RefreshSection("xxx")
is not worked for EF,so you must to restart your application.
The best way to solve the problem:
with NO Connection strings in app.config.
Uses automatic migrations and 2 databases using the same context. The real runtime supplied Connection. Approach.

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

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.

Using Lightswitch with my own Domain object : missing reference

I'm working on an asp.net application. I have a Domain assembly (containing mostly command and queries code), a Domain.Contract assembly ( containing mostly the domain objets), a Repository (called by the Domain) and the Web site.
For some specific backoffice needs, I want to create a Lightswitch application. I want to use my current domain as a datasource. I added a WCF Ria Service class Library, and in the RiaService.Web project, added a class based on DomainService, with one query method. Nothing more (no entities, since they are in my Domain.Contract assembly)
Using help from here (thanks Michael W), I was able to add the datasource, and to get my domain object recognized as an entity in light switch.
But, I now have a compile error : 'The type or namespace name 'MyDomain' could not be found in the global namespace (are you missing an assembly reference?)' in the project ServerGenerated.
The specific error is on this line :
global::Domain.Contract.MyObject result = new global::Domain.Contract.MyObject();
which is on of my domain objects (created in my Domain.Contract assembly).
So, it seems that the only thing I need to do is to add a reference do Domain.contract to the ServerGenerated project.
But it does not appear in Visual Studio. Adding my reference to the Server project does not fix the error.
Thanks.
In your solution explorer windows, select the ServerGenerated assembly and click on the "show all files" button. This will show all hidden folders - including the generated client code for WCF Ria.
If there's no generated code this means there's either something wrong with the service implementation (typically you'd get a compilation error) or there's something else wrong with the service - like a missing [EnableClientAccess] attribute (I know for a fact that a service without this attribute does not generate client code).
Other things to check for is to include the server-side ria assembly in your host project + have a correct configuration for WCF ria in the .config file (ask google).
In your solution explorer windows, select the project and click on the "show all files" button and also select the "file view" (not the logical view).
Then add the reference to the projet "ServerGenerated" (not the server project)

How do I encrypt the connection string with EF 4.1 Code First?

I'm using the Code First RC to build a class library and I would like to be able to encrypt the connection string that I'm using. The consumers of the class library could be ASP.NET or Windows Forms apps, so I need an encryption method that works with both.
It appears that I can pass in a connection string to DbContext by name, but not by value, as shown here, so I don't think I can manually decrypt within my program before passing the string to DbContext. Could anyone point me in the right direction?
You can easily encrypt any .NET configuration section - not just in ASP.NET as many devs seem to think, but absolutely also in other apps.
Check out Jon Galloway's blog post on the topic - excellent read!
With this approach, you could encrypt the <connectionStrings> section - and to make it easier still, you could externalize that section into a separate file, too.
So in your app.config for your Winforms app, you'd have:
<connectionStrings configSource="ConnectionStrings.config" />
and the same would be in your web.config for your web application, and the file referenced would contain just the <connectionStrings> and that could be encrypted. Load the appropriate connection string from your config, and pass it into your DbContext constructor, and you should be fine.
You can pass a full connection string into DbContext:
http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx
Under "Other DbContext Constructor Options":
...
You can pass a full connection string to DbContext instead of just
the database or connection string
name. By default this connection
string is used with the
System.Data.SqlClient provider;
this can be changed by setting a
different implementation of
IConnectionFactory onto
context.Database.DefaultConnectionFactory.
You can use an existing DbConnection object by passing it
to a DbContext constructor. If the
connection object is an instance of
EntityConnection, then the model
specified in the connection will be
used in Database/Model First mode.
If the object is an instance of
some other type—for example,
SqlConnection—then the context will
use it for Code First mode.
...
If this is true, then you can use AES or some other encryption to encrypt the string in the .config file, then decrypt at runtime and feed it into the DbContext constructor.

Resources