EF CodeFirst

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"));
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值