entity framework - Getting a 'System.InvalidOperationException' when changing the connection string for connecting to a database C# -
i getting unhandled exception of type 'system.invalidoperationexception' occurred in entityframework.dll when run application.
my application basic c# application using entity framework code first approach , trying connect database.
the following code used add data database (which worked before added connection string app.config file.
static void main(string[] args) { using (var context = new behaviourcontext()) { behaviour behaviour = new behaviour() { name = "test behaviour", activation_threshold = 90, currently_executing = false, preconditions_met = false, priority = 0.9f }; context.behaviours.add(behaviour); context.savechanges(); } }
this context class:
public class behaviourcontext: dbcontext { public behaviourcontext(): base("name=behaviourconnectionstring") { } public dbset<behaviour> behaviours { get; set; } }
this working fine until added connection string:
<connectionstrings> <add name="behaviourconnectionstring" connectionstring="data source=.;initial catalog=behaviourdb-byconnectionstring;integrated security=true" providername="system.data.sqlclient"/>
and full error when run application:
an unhandled exception of type 'system.invalidoperationexception' occurred in entityframework.dll
additional information: context cannot used while model being created. exception may thrown if context used inside onmodelcreating method or if same context instance accessed multiple threads concurrently. note instance members of dbcontext , related classes not guaranteed thread safe.
any help, hints or tip appreciated. :)
solved
there error in connection sting, had forgotten add '\sqlexpress'
<connectionstrings> <add name="behaviourconnectionstring" connectionstring="data source=.\sqlexpress;initial catalog=behaviourdb-byconnectionstring;integrated security=true" providername="system.data.sqlclient"/>
.
Comments
Post a Comment