The nice feature in MVC 4. It straight forward. You can change the connectionString to any SQL Server. At the standard feature, if there is no database on local "(localDb)\v11.0)" or on the server assigned to database, it will automatically created. If database
exist, it will use it. Look at the following code: Change the connectionString name and Database name, note: on non local db do not use db auto-name like on local db, you need to have a standard SQL db name.
If you have message "CREATE DATABASE permission denied in database 'master'", this is about authorization on SQL Server, or when you want to create a database using script, put "USE [master]". Look at the following sample:
USE [master]
GO
/****** Object: Database [humaNiTarian] Script Date: 05-May-2012 14:06:58 ******/
CREATE DATABASE [humaNiTarian]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'AdventureWorks2008R2_Data', FILENAME = N'D:\dB\CoRe\humaNiTarian_Data.mdf' , SIZE = 220928KB , MAXSIZE = UNLIMITED, FILEGROWTH = 16384KB )
LOG ON
( NAME = N'AdventureWorks2008R2_Log', FILENAME = N'D:\dB\CoRe\humaNiTarian_Data_log.ldf' , SIZE = 18240KB , MAXSIZE = UNLIMITED, FILEGROWTH = 10%)
GO
I've tried to use several times entity framework 4.3.1 it was unsuccesful, also I've tried to use EF 5 pre released version also unsuccessful. WHen I went back to EF 4.0, It's working. This is happend when I use name space System.ComponentModel.DataAnnotations.dll.
This error has been known by Microsoft
I am using data-migration to generate the database based on the domain model, it's a new feature in entity framework, i think for that we need to use the latest version of entity framework.
The error i get is when i run the application on the server. do you know where in the code we can view the checkup for cration a new database ?
I have also create database using Visual Studio 11 Beta Version, I created on VS 2010, but I didn't change the EF. Since I found the problem with data annotation, I temporary stoped using EF 4.3 and EF 5.0 until it solved the data annotation problem. In
this case I separate project of creating my database, and other CRUD projects.
Let me test my creation of database project using EF4.3 and EF 5.0, and maybe I can generate this case, and let you know.
My database is created automatically when there is no database on the SQL Server
I use: public classSchoolInitializer
: DropCreateDatabaseIfModelChanges<SchoolContext>
At the Global.asax.cs
on Application_Start(), I have: Database.SetInitializer<SchoolContext>(newSchoolInitializer());
So far everythings is working fine
When I try to have a constructor on my context, I have an error:
Exception Details:System.ComponentModel.Win32Exception: The network path was not found
public SchoolContext()
: base("NewContosoUniversity")
{
}
slasi.net
Member
135 Points
93 Posts
How to turn off the database creation with code first application
May 05, 2012 06:37 AM|LINK
Hi to all,
I am trying to deploy my first mvc 4 beta application, with entity framework 4.3.1 code first.
I would like to ask how do i turn off the database creation on production server, I prefer to do that in the web.config.
At the moment i have created the database using sql script, and i still get an error "CREATE DATABASE permission denied in database 'master'".
Any advice would be appreciated,
Thank you
Ori
jsiahaan
Contributor
2320 Points
595 Posts
Re: How to turn off the database creation with code first application
May 05, 2012 07:08 AM|LINK
Hi,
The nice feature in MVC 4. It straight forward. You can change the connectionString to any SQL Server. At the standard feature, if there is no database on local "(localDb)\v11.0)" or on the server assigned to database, it will automatically created. If database exist, it will use it. Look at the following code: Change the connectionString name and Database name, note: on non local db do not use db auto-name like on local db, you need to have a standard SQL db name.
<connectionStrings> <!--<add name="MovieDbContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication5-201254114647;Integrated Security=true" providerName="System.Data.SqlClient" />--> <add name="MovieDbContext" connectionString="Data Source=webserver\sqlexpress;Initial Catalog=Movies;Integrated Security=true" providerName="System.Data.SqlClient" /> </connectionStrings>If you have message "CREATE DATABASE permission denied in database 'master'", this is about authorization on SQL Server, or when you want to create a database using script, put "USE [master]". Look at the following sample:
Have fun.
Indonesian Humanitarian Foundation
jsiahaan
Contributor
2320 Points
595 Posts
Re: How to turn off the database creation with code first application
May 05, 2012 07:20 AM|LINK
Hi again,
I've tried to use several times entity framework 4.3.1 it was unsuccesful, also I've tried to use EF 5 pre released version also unsuccessful. WHen I went back to EF 4.0, It's working. This is happend when I use name space System.ComponentModel.DataAnnotations.dll. This error has been known by Microsoft
This can be considered to take care
Indonesian Humanitarian Foundation
slasi.net
Member
135 Points
93 Posts
Re: How to turn off the database creation with code first application
May 05, 2012 12:09 PM|LINK
Hi,
Thank you for your posts,
I have used Visual studio to create Script.sql to create the database (schema+data). this one worked fine.
I have updated the connectionstring as normal:
<add name="DefaultConnection" connectionString="Server=.\SQLEXPRESS;User ID=userID;Password=password;Initial Catalog=databaseName;" providerName="System.Data.SqlClient" />
the second one:
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS;User ID=userID;Password=password;Initial Catalog=databaseName; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
I am using data-migration to generate the database based on the domain model, it's a new feature in entity framework, i think for that we need to use the latest version of entity framework.
The error i get is when i run the application on the server. do you know where in the code we can view the checkup for cration a new database ?
Is there any other solution available ?
Thank you
Ori
jsiahaan
Contributor
2320 Points
595 Posts
Re: How to turn off the database creation with code first application
May 05, 2012 12:55 PM|LINK
Hi,
I have also create database using Visual Studio 11 Beta Version, I created on VS 2010, but I didn't change the EF. Since I found the problem with data annotation, I temporary stoped using EF 4.3 and EF 5.0 until it solved the data annotation problem. In this case I separate project of creating my database, and other CRUD projects.
Let me test my creation of database project using EF4.3 and EF 5.0, and maybe I can generate this case, and let you know.
Have fun
Indonesian Humanitarian Foundation
slasi.net
Member
135 Points
93 Posts
Re: How to turn off the database creation with code first application
May 05, 2012 07:20 PM|LINK
Hi,
I have manage to fix it, I have done 2 things:
1. I have copied the database files from my developer computer into the production server.
2. because I have also used different database name in the production server then in my developer environment.
I saw that the application tryes to connect to the old database name, even though I have updated the web.config-> connection string.
in order to solve it I have added constructor into the DatabasenameDBContext()
then it looks:
public class OldDatabaseNameDBContext : DbContext { public OldDatabaseNameDBContext () :base("NewDatabaseName") { }I expected from the EntityFramework to work properly when i update the connection string in the web.config.
it looks very wired that i need to code the database name.
Do you have any advice how to make it better ?
Thank you
Ori
jsiahaan
Contributor
2320 Points
595 Posts
Re: How to turn off the database creation with code first application
May 06, 2012 12:38 AM|LINK
Hi Ori,
My test outcome - using EF 4.3.1.0:
public SchoolContext() : base("NewContosoUniversity") { }Is this the same error with yours?
Here my code:
ConnectionString Web.Config:
<connectionStrings> <add name="SchoolContext" connectionString="Data Source=.\SQLExpress;Initial Catalog=NewContosoUniversity;Integrated Security=true" providerName="System.Data.SqlClient" /> </connectionStrings>Global.asax.cs
using ContosoUniversity.Models; using ContosoUniversity.DAL; . . . . . protected void Application_Start() { AreaRegistration.RegisterAllAreas(); // Use LocalDB for Entity Framework by default Database.DefaultConnectionFactory = new SqlConnectionFactory("Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True"); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); BundleTable.Bundles.RegisterTemplateBundles(); Database.SetInitializer<SchoolContext>(new SchoolInitializer()); }Context:
public class SchoolContext : DbContext { public DbSet<Student> Students { get; set; } public DbSet<Enrollment> Enrollments { get; set; } public DbSet<Course> Courses { get; set; } //public SchoolContext() // : base("NewContosoUniversity") //{ //} protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); } }Initializer
Try to remove the constructor on your context
Have fun
Indonesian Humanitarian Foundation