好了,好了,话很少说,EF的思想啊介绍啊,请看上一篇:juejin.im/post/5d99d0…html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Class
{
public int Id { get; set; }
public string ClassName { get; set; }
public virtual ICollection<Student> Student { get; set; }//表明的意思是一个班级有不少个学生
}
}
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
public class Student
{
public int Id { get; set; }
public string StuName { get; set; }
public virtual Class Class { get; set; } //表示学生是班级的
}
}
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace ConsoleApp1
{
public class DbTextContext:DbContext
{
public DbTextContext(): base("conStr")
{
}
public DbSet<Class> Classe { get; set; }
public DbSet<Student> Student { get; set; }
}
}
复制代码
<connectionStrings>
<add name="conStr" connectionString="data source=.;initial catalog=数据库名写你要建立的;persist security info=True;user id=xx;password=xxxxxx;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
using (DbTextContext db = new DbTextContext())
{
//建立数据库,没有就建立
db.Database.CreateIfNotExists();
db.SaveChanges();
}
}
}
}
复制代码
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); //将映射成的表移除复数约定,不加s
}
复制代码
一些关于上下文的配置:www.cnblogs.com/libingql/p/…数据库