EF Code First custom database (server and database name) connection configuration

Source: Internet
Author: User
Tags connectionstrings

Create a new database with EF Code First, which is the default: namespace. Models.moviedbcontext such as database files, if you want to use a custom database name, you can use the following method:

1.1, first must establish a good model file

Models in Movie.cs

 

1 usingSystem;2 usingSystem.ComponentModel.DataAnnotations;3 usingSystem.Data.Entity;4 5 namespaceMVCLX. Models6 {7      Public classMovie8     {9          Public intid{Get;Set;}Ten[Stringlength ( -, minimumlength=3)] One[Display (Name="Movie Title")] A          Public stringtitle{Get;Set;} -[Display (Name="category")] -[Stringlength ( -)] the [Required] -          Public stringgenre{Get;Set;} -[Display (Name="Release date")] - [DataType (datatype.date)] +[DisplayFormat (dataformatstring="{0:YYYY-MM-DD}", applyformatineditmode=true)] -          PublicDateTime ReleaseDate {Get;Set;} +[Display (Name="Price")] A [DataType (datatype.currency)] at[Range (1, +)] -          Public decimalPrice {Get;Set; } -[Stringlength (5)] -          Public stringRating {Get;Set; } -     } -      Public classMoviedbcontext:dbcontext in     { -          PublicMoviedbcontext () to         { +Database.setinitializer<moviedbcontext> (NULL); -         } the          PublicMoviedbcontext (stringdatabasename) *:Base(databasename) $         {Panax Notoginseng         } -         //The above two constructors are used to create a self-named database at the same time, which must be displayed in the controller file, calling the         //such as: private Moviedbcontext db = new Moviedbcontext ("Movietest"); +         //This can be done in the Web. config configuration by A         /* the <entityFramework> + <!--<defaultconnectionfactory type= "System.Data.Entity.Infrastructure.LocalDbConnectionFactory, Enti Tyframework "> - <parameters> $ <parameter value= "v11.0"/> $ </parameters> - </defaultConnectionFactory>--> - <defaultconnectionfactory type= "System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework "> the <parameters> - <parameter value= "Data source=192.168.3.19;initial catalog=movietest; User Id=sa; password=; Multipleactiveresultsets=true "/>Wuyi </parameters> the </defaultConnectionFactory> - <providers> Wu <provider invariantname= "System.Data.SqlClient" type= "System.Data.Entity.SqlServer.SqlProviderService S, entityframework.sqlserver "/> - </providers> About </entityFramework> $          */ -         //EntityFramework The connection properties specified in Defaultconnectionfactory, navigate to the specified server, generate a new database "Movietest" -         //The following two lines can call the configured database connection in Web. config, but the database movietest or moviedb must already be pre-existing -         /* A <connectionStrings> + <add name= "movieconnstring" connectionstring= "Data source=192.168.3.19;initial catalog=moviedb; User Id=sa; password=; the multipleactiveresultsets=true "providername=" System.Data.SqlClient "/> - <add name= "Moviedbcontext" connectionstring= "Data source=192.168.3.19;initial catalog=movietest; User Id=sa; password=; $ multipleactiveresultsets=true "providername=" System.Data.SqlClient "/> the </connectionStrings> the          */ the         //Public Moviedbcontext () the         //: Base ("Name=moviedbcontext") -         //{ in         //} the         //Public Moviedbcontext () the         //: Base ("name=movieconnstring") About         //{ the         //database.setinitializer<moviedbcontext> (null); the         //} the          PublicDbset<movie> Movies {Get;Set; } +     } -}

1.2. Configuration of EntityFramework related properties in the Web. config file

<entityFramework> <!--<defaultconnectionfactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0"/> </parameters> </defaultConnectionFactory>--> <defaultconnectionfactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> <parameters> <parameter value="Data source=192.168.3.19;initial catalog=movietest; User Id=sa; password=; Multipleactiveresultsets=true"/> </parameters> </defaultConnectionFactory> <providers> <provider invariantname="System.Data.SqlClient"Type="System.Data.Entity.SqlServer.SqlProviderServices, Entityframework.sqlserver"/> </providers> </entityFramework>


1.3, modify the model file Movie.cs in the Moviedbcontext class constructor, and by the Controller class MovieController.cs display the parameters ("custom database name") call, you can generate a custom database name:

  

 Public classMoviedbcontext:dbcontext { PublicMoviedbcontext () {Database.setinitializer<MovieDbContext> (NULL); }         PublicMoviedbcontext (stringdatabasename):Base(databasename) {}//The above two constructors are used to create a self-named database at the same time, you must display the parameter call in the controller file to generate a custom database name//"Movietest",such as:
public class Moviecontroller:controller
{
Private Moviedbcontext db = new Moviedbcontext ("Movietest");
...
}

EF Code First custom database (server and database name) connection configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.