EF Code First数据库连接配置
Posted Avatarx
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EF Code First数据库连接配置相关的知识,希望对你有一定的参考价值。
前面几节,使用的都是通过EF Code First创建的新数据库,接下来,将开始使用已存在的数据库。
1、使用配置文件设置数据库连接
App.config
数据库连接字符串的name与Data中NorthwindContext.cs类名相同
<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> </configSections> <connectionStrings> <add name="NorthwindContext" connectionString="Data Source=(local); Database=Northwind; User ID=sa; Password=1;" providerName="System.Data.SqlClient"/> </connectionStrings></configuration> Data中NorthwindContext.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 using Northwind.Domain.Mapping;10 11 namespace Northwind.Data12 {13 public class NorthwindContext : DbContext14 {15 public DbSet<Category> Categories { get; set; }16 public DbSet<Product> Products { get; set; }17 public DbSet<Supplier> Suppliers { get; set; }18 19 protected override void OnModelCreating(DbModelBuilder modelBuilder)20 {21 modelBuilder.Configurations.Add(new CategoryMap());22 modelBuilder.Configurations.Add(new ProductMap());23 modelBuilder.Configurations.Add(new SupplierMap());24 }25 }26 } 执行成功后,创建的数据库如下图:
2、使用构造函数指定数据库
NorthwindContext.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 using Northwind.Domain.Mapping;10 11 namespace Northwind.Data12 {13 public class NorthwindContext : DbContext14 {15 public NorthwindContext()16 { }17 18 public NorthwindContext(string databaseName)19 : base(databaseName)20 {21 22 }23 24 public DbSet<Category> Categories { get; set; }25 public DbSet<Product> Products { get; set; }26 public DbSet<Supplier> Suppliers { get; set; }27 public DbSet<User> Users { get; set; }28 public DbSet<Role> Roles { get; set; }29 30 protected override void OnModelCreating(DbModelBuilder modelBuilder)31 {32 modelBuilder.Configurations.Add(new CategoryMap());33 modelBuilder.Configurations.Add(new ProductMap());34 modelBuilder.Configurations.Add(new SupplierMap());35 modelBuilder.Configurations.Add(new UserMap());36 modelBuilder.Configurations.Add(new RoleMap());37 }38 }39 } Program.cs
using (NorthwindContext db = new NorthwindContext("Northwind")){} 3、使用构造函数指定数据库连接字符串
App.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <configSections> 4 <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> 5 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 6 </configSections> 7 <connectionStrings> 8 <add name="NorthwindConnectionString" connectionString="Data Source=(local); Database=Northwind; User ID=sa; Password=1;" providerName="System.Data.SqlClient"/> 9 </connectionStrings>10 </configuration> NorthwindContext.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 using System.Data.Entity; 7 8 using Northwind.Domain.Entities; 9 using Northwind.Domain.Mapping;10 11 namespace Northwind.Data12 {13 public class NorthwindContext : DbContext14 {15 public NorthwindContext()16 : base("name=NorthwindConnectionString")17 { }18 19 public DbSet<Category> Categories { get; set; }20 public DbSet<Product> Products { get; set; }21 public DbSet<Supplier> Suppliers { get; set; }22 public DbSet<User> Users { get; set; }23 public DbSet<Role> Roles { get; set; }24 25 protected override void OnModelCreating(DbModelBuilder modelBuilder)26 {27 modelBuilder.Configurations.Add(new CategoryMap());28 modelBuilder.Configurations.Add(new ProductMap());29 modelBuilder.Configurations.Add(new SupplierMap());30 modelBuilder.Configurations.Add(new UserMap());31 modelBuilder.Configurations.Add(new RoleMap());32 }33 }34 }
以上是关于EF Code First数据库连接配置的主要内容,如果未能解决你的问题,请参考以下文章
EF Code First自定义数据库(服务器及数据库名)连接配置
EF 连接 mysq l数据库 code first模式 的实践