posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

指定数据连接,指定表名,移除表名复数化(表名后面不加s),设置字段约束,主外键关系。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using MvcApplication1.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
 
namespace MvcApplication1.Repositories
{
    public class EFContext : DbContext
    {<br>     //指定数据库连接字符串 name是 DefaultConnection
        public EFContext() : base("DefaultConnection") { }
 
        public DbSet<AdminInfo> AdminInfos { get; set; }
        public DbSet<ArticlesInfo> ArticlesInfos { get; set; }
        public DbSet<BannerInfo> BannerInfos { get; set; }
        public DbSet<CandidatesInfo> CandidatesInfos { get; set; }
        public DbSet<FriendLinksInfo> FriendLinksInfos { get; set; }
        public DbSet<PartnersInfo> PartnersInfos { get; set; }
        public DbSet<RecruitmentInfo> RecruitmentInfos { get; set; }
        public DbSet<SingleArticle> SingleArticles { get; set; }
 
        /// <summary>
        /// 构造表
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();//移除表名复数的契约
 
            #region 管理员信息
            modelBuilder.Entity<AdminInfo>().HasKey(k => k.ID); //设置主键
            modelBuilder.Entity<AdminInfo>().Property(q => q.UserName).IsRequired().HasMaxLength(50);//设置不能为空
            modelBuilder.Entity<AdminInfo>().Property(q => q.UserPwd).IsRequired().HasMaxLength(50);//设置不能为空
            #endregion
 
            #region 招聘信息
            modelBuilder.Entity<RecruitmentInfo>().HasKey(k => k.ID);
            modelBuilder.Entity<RecruitmentInfo>().Property(q => q.TypeID).IsRequired();
            modelBuilder.Entity<RecruitmentInfo>().Property(q => q.Title).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<RecruitmentInfo>().Property(q => q.RequireNum).IsRequired().HasMaxLength(50);
            modelBuilder.Entity<RecruitmentInfo>().Property(q => q.PostCharacter).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<RecruitmentInfo>().Property(q => q.Responsibilities).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<RecruitmentInfo>().Property(q => q.Qualification).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<RecruitmentInfo>().Property(q => q.CreateDate).IsRequired();
 
            //主表包含多个:CandidatesInfoList;子表含有一个:RecruitmentInfoModel;子表中对应主表的外键:RecruitmentID。
            modelBuilder.Entity<RecruitmentInfo>().HasMany(s => s.CandidatesInfoList).WithRequired(c => c.RecruitmentInfoModel).HasForeignKey(f => f.RecruitmentID);
            #endregion
 
            #region 应聘者信息
            modelBuilder.Entity<CandidatesInfo>().HasKey(k => k.ID);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.RecruitmentID).IsRequired();
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Name).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.MaritalStatus).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.NativePlace).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.DomicileLocation).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.BirthDay).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Age).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.IDCard).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Education).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Hobby).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Address).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Areas).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Email).IsRequired().HasMaxLength(500);
            modelBuilder.Entity<CandidatesInfo>().Property(q => q.Mobile).IsRequired().HasMaxLength(500);
            #endregion
 
            #region 文章信息
            modelBuilder.Entity<ArticlesInfo>().HasKey(k => k.ID);//编号
            modelBuilder.Entity<ArticlesInfo>().Property(q => q.TypeID).IsRequired();//类型 1:集团新闻  2:行业动态
            modelBuilder.Entity<ArticlesInfo>().Property(q => q.Title).IsRequired();//标题
            modelBuilder.Entity<ArticlesInfo>().Property(q => q.IsRecommend).IsRequired();//推荐 0:不推荐 1:推荐
            modelBuilder.Entity<ArticlesInfo>().Property(q => q.Img).IsRequired();//配图
            modelBuilder.Entity<ArticlesInfo>().Property(q => q.Contents).IsRequired();//内容
            modelBuilder.Entity<ArticlesInfo>().Property(q => q.CreateDate).IsRequired();//创建日期
            #endregion
 
            #region Banner
            modelBuilder.Entity<BannerInfo>().HasKey(k => k.ID);//编号
            modelBuilder.Entity<BannerInfo>().Property(q => q.Name).IsRequired();//名称
            modelBuilder.Entity<BannerInfo>().Property(q => q.Img).IsRequired();//图片
            modelBuilder.Entity<BannerInfo>().Property(q => q.LinkUrl).IsRequired();//链接
            #endregion
 
            #region 友情链接表
            modelBuilder.Entity<FriendLinksInfo>().HasKey(q => q.ID);//编号
            modelBuilder.Entity<FriendLinksInfo>().Property(q => q.Name).IsRequired();//名称
            modelBuilder.Entity<FriendLinksInfo>().Property(q => q.LinkUrl).IsRequired();//链接
            #endregion
 
            #region 合作伙伴表
            modelBuilder.Entity<PartnersInfo>().HasKey(q => q.ID);//编号
            modelBuilder.Entity<PartnersInfo>().Property(q => q.Name).IsRequired();//名称
            modelBuilder.Entity<PartnersInfo>().Property(q => q.Img).IsRequired();//图标
            modelBuilder.Entity<PartnersInfo>().Property(q => q.LinkUrl).IsRequired();//链接
            #endregion
 
            #region 单篇图文信息
            modelBuilder.Entity<SingleArticle>().HasKey(q => q.ID);//编号
            modelBuilder.Entity<SingleArticle>().Property(q => q.Title).IsRequired();//标题
            modelBuilder.Entity<SingleArticle>().Property(q => q.Contents).IsRequired();//内容
            modelBuilder.Entity<SingleArticle>().Property(q => q.CreateDate).IsRequired();//创建日期
            #endregion
 
 
            #region 添加默认数据
 
            #endregion
        }
    }
}

  

posted on   邢帅杰  阅读(487)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示