I am not planning to use EF Code First in an MVC Website. I am looking to Utilize it in a App that has a WPF Client.
Projects are setup as
ContactManager.Core // Contains all
Entities(dll)
ContactManager.Data // Contains
the DataContext and other data
related Services(dll)
ContactManager.Services // Business
components (dll)
ContactManager.Client // WPF
Application
I am unable to generate a SQLExpress or SQLCE 4.0 DB. I am more interested in compact version db. I am not getting any error except my unit tests fail because it tries to connect a db that doesnt exist.
I found out the answer 2 Options:
Option 1:
In your DbContext you specify the connection strings in the base constructor:
public class RecetteContext : DbContext
{
public RecetteContext()
:base("<YourConnectionString HERE>")
{
}
public DbSet<Categorie> Categories { get; set; }
public DbSet<Recette> Recettes { get; set; }
}
}
Option 2:
The one I used, you give you connection string a name in the DbContext base constructor:
public RecetteContext()
: base("RecettesDatabase")
{ }
And in your App.Config file you add a ConnectionString with the same name:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="RecettesDatabase"
connectionString="Data Source=RecettesDB.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
</configuration>
Hope it solved your issue!
Related
I've got a code base that uses EF Core and Dapper to perform actions on a database.
I want to set up a new copy of the site to develop some features and I want this site to connect to a new isolated copy of the database (dbConnectionDEBUG).
At the moment, I use the following setup:
startup.cs
...
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("dbConnectionMain")));
services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
...
I have a ConnectionStrings class which is being populated correctly via the DI in startup:
public class ConnectionStrings
{
public string dbConnectionMain { get; set; }
public string dbConnectionDEBUG { get; set; }
public ConnectionStrings()
{
this.dbConnectionMain = "";
this.dbConnectionDEBUG = "";
}
}
Then, throughout my controllers/services I have access to ConnectionStrings and 99% of the time I'm doing the following to make DB calls:
using (var conn = new SqlConnection(_connectionStrings.dbConnectionMain))
{
conn.Open();
...
This would amount to a lot of code changes if I were to want to switch over to the 'DEBUG' db.
How do I easily switch between the connection strings in my code depending on what version of the system I'm working on.
If I could somehow do this dynamically that'd be great. The obvious determining factor would be the URL the site is operating on.
Alternatively, (as a single change) do I just manually change the connection string at the source (e.g keystore/appsettings). I'm not keen on this as it leaves room for human error.
Update (2)
Based on what #Nkosi mentioned I am pursuing this path:
Have one connection string 'Id' (i.e. dbConnection) used throughout
Differentiate the connection string value within this based on the environment the app is running/deployed in
I have another question:
If I have the following...
"MYAPPNAME": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:12345/;https://myapptestdomain.com/"
}
and:
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
IHostingEnvironment env = context.HostingEnvironment;
config.AddJsonFile($"appsettings.{env.EnvironmentName.ToLower()}.json", optional: true);
})
.UseStartup<Startup>();
...will this automatically pick up my site is in the Development mode based on the applicationUrl values OR will I have to manually add ASPNETCORE_ENVIRONMENT with a value Development on the server I deploy the app to?
Additional: My app is running in an Azure App Service.
Update (3) - Mission Complete
Just to finalise this question (in case anyone needs to know this), I have the following setup based on recommendations made by #Nkosi.
Connection String - I have one connection string Id/name dbConnection which is used in all appSettings (see below)
App Settings
I have a default appSettings.json with dbConnection that looks at the live database
I have an additional appSettings.Playground.json file with dbConnection that looks at my testing database
Azure - App Service - On my playground development slot I have added an App Setting for ASPNETCORE_ENVIRONMENT with the value 'Playground'
In my Program.cs file I have:
config.AddJsonFile($"appsettings.json", optional: true,reloadOnChange: true);
and
config.AddJsonFile($"appsettings.{env.EnvironmentName.ToLower()}.json", optional: true,reloadOnChange: true);
Just to note, I do also initialise a Vault on Azure which stores all my Keys and Secrets for the Azure based apps. Locally User Secrets is used.
ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT at app startup and stores the value in IHostingEnvironment.EnvironmentName.
Since the environment is being loaded, then it should be available from the hosting environment via the builder context
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) => {
string environment = context.HostingEnvironment.EnvironmentName; //get current environment
//load config based on environment.
config.AddJsonFile($"appsettings.{environment}.json", optional: true);
//...
})
//...
Reference Use multiple environments in ASP.NET Core
For simple apps and to keep the connection strings away from my repository I use preprocessor statements plus PATH/System Variables and for release I provide a connection string within the settings.json.
#define USE_FEATURE_X
using System;
namespace MyNamespace {
internal static class StaticConnectionStringFactory {
public static string GetConnectionString() {
#if DEBUG && !USE_FEATURE_X
var connectionString = Environment.GetEnvironmentVariable("CNNSTR_SQL_XYZ", EnvironmentVariableTarget.User);
#elif DEBUG && USE_FEATURE_X
var connectionString = Environment.GetEnvironmentVariable("CNNSTR_SQL_ABC", EnvironmentVariableTarget.User);
#else
var connectionString = Environment.GetEnvironmentVariable("SqlConnectionString", EnvironmentVariableTarget.Process);
#endif
return connectionString;
}
}
}
I think if you add 2 connection for debug and main then you will have face some difficulty because more member working in you team. may be some own wrongly use release mode for code development.
you can try this webconfig method:
public class ConnectionStrings
{
public string dbConnection { get; set; }
public ConnectionStrings()
{
bool Ismain = bool.Parse(System.Configuration.ConfigurationManager.AppSettings["HasLive"]);
if (Ismain)
{
dbConnection = "";// get Main connectionstring
}
else
{
dbConnection = "";// get Debug connectionstring
}
}
}
web.config:
<connectionStrings>
<add name="dbConnectionMain" connectionString="" providerName="System.Data.SqlClient" />
<add name="dbConnectionDEBUG" connectionString="" roviderName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="HasLive" value="false"/>
</appSettings>
</connectionStrings>
I used this pluralsight video on MVC code first migrations to keep my default MVC IdentityDb context and create another context for custom tables. Since then I get an error trying to connect connecting to the database online:
CREATE DATABASE permission denied in database 'master'.
.........
It works locally. My connection string are correct and my context classes point to the right connection string name:
public class IdentityDb : IdentityDbContext<ApplicationUser>
{
public IdentityDb()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static IdentityDb Create()
{
return new IdentityDb();
}
}
public class CustomDb : DbContext
{
public CustomDb() : base("DefaultConnection") { }
public DbSet<Inquiry> Inquiry { get; set; }
public DbSet<Product> Product { get; set; }
}
Connection string:
<add name="DefaultConnection" connectionString="server=***.db.1and1.com; initial catalog=***;uid=***;pwd=***" providerName="System.Data.SqlClient" />
I've read that the connection string name should be the same as the context class name but since I have two contexts I need a common name (DefaultConnection) which I've specified in the contexts.
It works connecting to my local database but not when its online so I did wonder if this would relate to the migration history table being up to date online and EF 6 trying to update the database but the entries in the migrations table match.
Any help appreciated.
* UPDATE *
I tried resetting the EF migrations with this guide thinking if the migrations where out of sync with the online DB it could result in EF trying to re-create the database causing this issue. However the problem still persists!
I have now added these lines to my context constructors respectively:
Database.SetInitializer<IdentityDb>(null);
Database.SetInitializer<CustomDb>(null);
This has stopped the error but kind of defeated the purpose of EF because I now have to remove it when creating migrations and manually script the changes to the online DB, then put it back in for the site to work online.
I've created very simple MVC5 project with two Web roles . webRole1 with local DB which was created by code first (and generate the view by scaffold) this is working fine ,I was able to run it get view data and create data in the local DB.
Now I created very simple API in the WebRole1 to read the data from the DB which is working and I get the data via the API
I created WebRole2 and The problem is that I dont able to read the data in from this API (which is in web role1 ) in webRole2,How should I do that?I getting null for the key that I provided which is exist in DB
I can share the project in one drive if its OK.
This is the model of WebRole1
namespace WebRole1.Models
{
public class User
{
[Key]
[Required]
[DatabaseGenerated(DatabaseGeneratedOption.None)]
public string UserId { get; set; }
public string FirstName { get; set; }
}
public class ConfigModelDbContext : DbContext
{
public ConfigModelDbContext()
: base("DefaultConnection")
{
}
public DbSet<User> User { get; set; }
}
}
This is the connection string
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\User.mdf;Initial Catalog=Users;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
This is the simple API which read the data from the local DB (the data found if I call this API from webRole1
namespace WebRole1.Models
{
public class UserApi
{
private ConfigModelDbContext db = new ConfigModelDbContext();
public User getDbData()
{
User user = db.User.Find("user1");
return user;
}
}
}
Answered here by Russriguez:
Configure the second WebRole to point at the .mdf in the first WebRole (MVC project), e.g.
<connectionStrings>
<add name="Connection1" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=C:\Path\To\The\Database\Person.mdf;Initial Catalog=Persons;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
replace "C:\Path\To\The\Database\Person.mdf" with the actual full path
to the database file that is in WebRole1's App_Data directory.
Only problem is that this is an absolute path, so all developers need their repository in the same place. Not ideal, but I couldn't get relative paths to work. I tried:
..\..\..\..\..\..\[project-with-db]\App_Data\[db-name].mdf
assuming that the base directory of the second web role would be:
[src-root]\[azure-project]\csx\Debug\roles\[web-role-project-2]\approot
I have an ASP.NET MVC project in VS 2012. I want to use Entity Framework 6 code-first. When I create model and db-context file and when I run my project it does not create any database in my SQK Server. I want to know what is wrong?
I want to know how can I create database by code first in my SQL Server not in SQL Server Express or Compact. How can I create it? I try scaffolding too but it dos not work true and does not create any database. Any tips or trick would be welcome
This is my web.config setting :
<connectionStrings>
<add name="dbRaja"
connectionString="Data Source=hp-PC;Initial Catalog=Raja;User ID=sa;Password=123;MultipleActiveResultSets=True;Trusted_Connection=False;Persist Security Info=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
and its my datacontext :
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace Raja1.Models
{
public class dbRaja : DbContext
{
// You can add custom code to this file. Changes will not be overwritten.
//
// If you want Entity Framework to drop and regenerate your database
// automatically whenever you change your model schema, add the following
// code to the Application_Start method in your Global.asax file.
// Note: this will destroy and re-create your database with every model change.
//
// System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<Raja1.Models.Raja1Context>());
public DbSet<Raja1.Models.Spareparts> Spareparts { get; set; }
}
}
You need to tell your DbContext to use the intended connection string. It will default to one named the same as your application (i.e. "Raja1"), whereas your connection string is named "dbRaja".
public class MyContext : DbContext
{
public MyContext : base("name=dbRaja")
{
}
}
Put the following code in your app start:
using (var context = new YOUR_DB_CONTEXT()) {
if (!context.Database.Exists()) {
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
I have a connection string:
<add name="Gini" providerName="System.Data.SqlClient" connectionString="user id=user;Password=pa55;Data Source=server;Database=gini" />
I want EF to be able to control the creation of the database and updates through migrations so I'm letting it have complete control over the DB.
My contact class looks like the following:
public class GiniContext : DbContext
{
public DbSet<UserSession> UserSessions { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserSessionConfiguration());
}
public GiniContext() : base("Gini")
{
Database.Create();
}
}
I would expect this to create a database called "gini" on the server called "server" using the username and password as above but it's creating it on the (LocalDB)\v11.0 instance.
What am I doing wrong?
If you have two projects like a Class Library for Objects and a Web Application referencing it. You ll need to add the connection from app.config to the web.config in your web application.