在App.Config中,可以:
(1)自定义类
public xxxxDbContext()
{
public XXXXDbContext():base("name=xxxxContainer")//这个是指向其连接字符串的东西
{
}
protected overrride void OnModelCreating(DbModelBuilder modelBuilder)
{
}
pulbic DbSet<Model1> Model1{get;set;}
public DbSet<Model2> Model2 {get;set;}
}
(2)Model1和Model2自己写
public class Model2
{
[Key]
public int Id{get;set;}
public virtual Model1 Model1{get;set;}
}
(3)添加EF相关引用,注意在新版的VS中,引用的程序集放到项目下的packages文件夹中
(4)
<configuration>
<connectionStrings>
<add name="xxxxContainer" connectionString = " server=localhost;userid =root;password=123;persist security info =true;database=dbName;charset=utf8;"
providerName="Mysql.Data.MysqlClient">//如果是SqlServer,那么就是System.Data.SqlClient
</...>
</...>
(5)写代码
*动态创建数据库
dbContext.Database.CreateIfNotExists();
//