标签:tin display ase 文件 user color default multiple class
通过EF Code First创建新的数据库,默认的都是: 命名空间.Models.MovieDbContext之类的数据库文件,如果想用自定义的数据库名称,则可以用下面的方法:
1.1、首先必须建立好Model文件
Models中Movie.cs
1 using System; 2 using System.ComponentModel.DataAnnotations; 3 using System.Data.Entity; 4 5 namespace MVCLX.Models 6 { 7 public class Movie 8 { 9 public int Id{get;set;} 10 [StringLength(60,MinimumLength=3)] 11 [Display(Name="电影标题")] 12 public string Title{get;set;} 13 [Display(Name="分类")] 14 [StringLength(30)] 15 [Required] 16 public string Genre{get;set;} 17 [Display(Name="发布日期")] 18 [DataType(DataType.Date)] 19 [DisplayFormat(DataFormatString="{0:yyyy-MM-dd}",ApplyFormatInEditMode=true)] 20 public DateTime ReleaseDate {get;set;} 21 [Display(Name="价格")] 22 [DataType(DataType.Currency)] 23 [Range(1,1000)] 24 public decimal Price { get; set; } 25 [StringLength(5)] 26 public string Rating { get; set; } 27 } 28 public class MovieDbContext : DbContext 29 { 30 public MovieDbContext() 31 { 32 Database.SetInitializer<MovieDbContext>(null); 33 } 34 public MovieDbContext(string databasename) 35 : base(databasename) 36 { 37 } 38 //上面两个构造函数同时使用用于创建自已命名的数据库,必须在控制器文件中,显示调用 39 //如: private MovieDbContext db = new MovieDbContext("MovieTest"); 40 //这样才能在web.config配置中由 41 /* 42 <entityFramework> 43 <!--<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 44 <parameters> 45 <parameter value="v11.0" /> 46 </parameters> 47 </defaultConnectionFactory>--> 48 <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"> 49 <parameters> 50 <parameter value="Data Source=192.168.3.19;Initial Catalog=MovieTest;User ID=sa;Password=;MultipleActiveResultSets=True" /> 51 </parameters> 52 </defaultConnectionFactory> 53 <providers> 54 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> 55 </providers> 56 </entityFramework> 57 */ 58 //entityFramework中的defaultConnectionFactory所规定的连接属性,定位到指定服务器,生成新的数据库"MovieTest" 59 //下面这两行分别可以调用web.config中配置的数据库连接,但数据库MovieTest或MovieDB都必须已经是预先存在的 60 /* 61 <connectionStrings> 62 <add name="MovieConnString" connectionString="Data Source=192.168.3.19;Initial Catalog=MovieDB;User ID=sa;Password=; 63 MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/> 64 <add name="MovieDbContext" connectionString="Data Source=192.168.3.19;Initial Catalog=MovieTest;User ID=sa;Password=; 65 MultipleActiveResultSets=True" providerName="System.Data.SqlClient"/> 66 </connectionStrings> 67 */ 68 //public MovieDbContext() 69 // : base("name=MovieDbContext") 70 //{ 71 //} 72 //public MovieDbContext() 73 // : base("name=MovieConnString") 74 //{ 75 // Database.SetInitializer<MovieDbContext>(null); 76 //} 77 public DbSet<Movie> Movies { get; set; } 78 } 79 }
1.2、web.config文件中配置好EntityFrameWork的相关属性
<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、修改模型文件Movie.cs中的MovieDbContext类里的构造函数,并由控制器类MovieController.cs显示传参("自定义数据库名称")调用,即可生成自定义数据库名称:
public class MovieDbContext : DbContext { public MovieDbContext() { Database.SetInitializer<MovieDbContext>(null); } public MovieDbContext(string databasename) : base(databasename) { } //上面两个构造函数同时使用用于创建自已命名的数据库,必须在控制器文件中,显示传参调用才能生成自定义数据库名 //"MovieTest",如:
public class MovieController : Controller
{
private MovieDbContext db = new MovieDbContext("MovieTest");
...
}
EF Code First自定义数据库(服务器及数据库名)连接配置
标签:tin display ase 文件 user color default multiple class
原文地址:http://www.cnblogs.com/yxlq/p/6692960.html