1.创建一个MVC Web项目
2.安装NeGet包EntityFramework
3.web.config加配置文件
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=127.0.0.1;Initial Catalog=tenant1db;User ID=sa;PassWord=123456" providerName="System.Data.SqlClient" />
</connectionStrings>
4.创建类
public class Person
{
public long Id { get; set; }
public int? Age { get; set; }
public string Name { get; set; }
}
5.创建config类
public class PersonConfig:EntityTypeConfiguration<Person>
{
public PersonConfig()
{
ToTable("T_Persons");
this.Property(c => c.Name).HasMaxLength(30);
}
}
public class TestContext:DbContext
{
public TestContext() : base("name=ConnectionString")
{
}
public DbSet<Person> Persons { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
Assembly assembly = Assembly.GetExecutingAssembly();
modelBuilder.Configurations.AddFromAssembly(assembly);
}
}
7.异常查看
8.
public class PersonConfig:EntityTypeConfiguration<Person>
{
public PersonConfig()
{
ToTable("T_Persons");
//设置最大长度
this.Property(c => c.Name).HasMaxLength(30);
//属性不能为空
this.Property(c => c.Name).IsRequired();
//属性可以为空
this.Property(c => c.Name).IsOptional();
//设置主键 默认Id
this.HasKey(e => e.Id);
//某个字段不参加数据库映射
this.Ignore(c => c.Name);
//对应数据库类型是varchar,而不是nvarchar()
this.Property(c => c.Name).IsUnicode(false);
//设置数据库属性的名字是其他名字
this.Property(e => e.Age).HasColumnName("NianLing");
//指定字段是自整长类型
this.Property(p => p.Id).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
}
}
9.表一对多的关系
public class Student
{
public long Id { get; set; }
public string Name { get; set; }
public long ClassId { get; set; }
public long? XZClassId { get; set; }
public virtual Class Class { get; set; }
public virtual Class XZClass { get; set; }
public int Age { get; set; }
}
public class Class
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Student> Students { get; set; } = new List<Student>();
public virtual ICollection<Student> XZStudents { get; set; } = new List<Student>();
}
public class StudentConfig:EntityTypeConfiguration<Student>
{
public StudentConfig()
{
ToTable("T_Students");
this.HasRequired(c => c.Class).WithMany(e=>e.Students).HasForeignKey(e=>e.ClassId);
this.HasOptional(c => c.XZClass).WithMany(e=>e.XZStudents ).HasForeignKey(e => e.XZClassId);
}
}
public class Classconfig:EntityTypeConfiguration<Class>
{
public Classconfig()
{
ToTable("T_Classes");
HasMany(e => e.Students).WithRequired(e=>e.Class).HasForeignKey(e => e.ClassId);
HasMany(e => e.XZStudents ).WithOptional(e=>e.XZClass).HasForeignKey(e => e.XZClassId);
}
}
20.多对多的关系
public class Teacher
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Student> Students { get; set; } = new List<Student>();
}
public class Student
{
public long Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Teacher> Teachers { get; set; } = new List<Teacher>();
}
public class StudentConfig:EntityTypeConfiguration<Student>
{
public StudentConfig()
{
ToTable("T_Students");
}
}
public class Teacherconfig: EntityTypeConfiguration<Teacher>
{
public Teacherconfig()
{
this.HasMany(c => c.Students).WithMany(e=>e.Teachers ).Map(m => m.ToTable("T_TeacherStudentsRelations").MapLeftKey("TeacherId").MapRightKey("StudentId"));
}
}