Cheers all,
I'm using an Entity Framework model. I need to pass my connection string like parameter, so, in another file, I've written (from this question):
namespace MyNamespace.Model
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MyEntities: DbContext
{
public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
}
}
When I startup the app, I call this constructor in this way, so I can refer to this from anyway in the app:
public static MyEntities dbContext =
new MyEntities(mdlImpostazioni.SetConnectionString());
where mdlImpostazioni.SetConnectionString()
returns a string (the data are correct):
server=192.168.1.100\SVILUPPO;database=MyDB;uid=myName;pwd=111111;
When I execute this code, it seems to be all ok, but when I try to make a query like:
var query = (from r in MainWindow.dbContext.TabTipoSistema select r);
it throws an exception from here:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
saying:
Additional information: The code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First, make sure that the connection string of Entity Framework is specified in the configuration file of the application running. To use these classes, which were generated from Database First or Model First, with Code First add any additional configuration using attributes or the API DbModelBuilder and remove the code that throws this exception.
the stacktrace is:
in MyNamespace.Model.MyEntities.OnModelCreating(DbModelBuilder modelBuilder) in c:...\Model\Model.Context.cs:row 25 in System.Data.Entity.DbContext.CallOnModelCreating(DbModelBuilder modelBuilder) in System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder() in System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) in System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)*
InnerException is null.
Where is the error?