新建一个类库项目Service,分别创建两个文件夹,方便管理!!
利用NuGet管理包在Service项目下载安装EF框架
按照需求新建实体类,每个实体类都继承一个基类(每个类共有的属性,不用每个新建类都重复写,这里用到的是id 创建时间 软删除)代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZSZ.Service.Entities
{
public abstract class BaseEntity
{
public long Id { get; set; }
public DateTime CreateDateTime { get; set; } = DateTime.Now;
public bool IsDeleted { get; set; } = false;
}
}
然后逐一新建实体类,继承基类;
实体类之间关系大多数分为一对多和多对多
一对多方式(以一个城市对多个用户为例):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZSZ.Service.Entities
{
public class UserEntity:BaseEntity
{
public string PhoneNum { get; set; }
public string PasswordHash { get; set; }
public string PasswordSalt { get; set; }
public int LoginErrorTimes { get; set; }
public DateTime? LastLoginErrorDateTime { get; set; }
public long? CityId { get; set; } //外键
public virtual CityEntity City { get; set; } //导航属性的写法
}
}
同时新建一个实体配置类放在ModeConfig文件下(通常在多端配置外键和对应关系):
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.Service.Entities;
namespace ZSZ.Service.ModelConfig
{
class UserConfig:EntityTypeConfiguration<UserEntity>
{
public UserConfig()
{
ToTable("T_Users");
HasRequired(u => u.City).WithMany().HasForeignKey(u => u.CityId).WillCascadeOnDelete(false); //WillCascadeOnDelete(false) 不级联删除的意思 WithMany指的是
Property(p => p.PasswordHash).IsRequired().HasMaxLength(100);
Property(p => p.PasswordSalt).IsRequired().HasMaxLength(20);
Property(p => p.PhoneNum).IsRequired().HasMaxLength(20).IsUnicode(false);
}
}liang
}
多对多方式(权限和角色为例子):
权限实体类新建:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZSZ.Service.Entities
{
public class PermissionEntity :BaseEntity
{
public string Description { get; set; }
public string Name { get; set; }
public virtual ICollection<RoleEntity> Roles { get; set; } = new List<RoleEntity>(); //多对多写法 两个类都要写对应的ICollection
}
}
实体配置(在其中一边配置关系就行!!!!两边同时都行。。没必要!):
using System;
using System.Collections.Generic;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZSZ.Service.Entities;
namespace ZSZ.DAL.ModelConfig
{
class RoleConfig: EntityTypeConfiguration<RoleEntity>
{
public RoleConfig()
{
ToTable("T_Roles");
HasMany(r => r.Permissions).WithMany(p => p.Roles).Map(m=>m.ToTable("T_RolePermissions")
.MapLeftKey("RoleId").MapRightKey("PermissionId"));
Property(p => p.Name).IsRequired().HasMaxLength(50);
}
}
}
接下来要在UI项目里面配置连接字符串,同时UI项目也要按照EF框架,方法同上!!
<connectionStrings>
<add name="connstr" connectionString="Data Source=.; Initial Catalog=zhangshangzu; User ID=sa;Password=123456" providerName="System.Data.SqlClient"/>
</connectionStrings>
在Service下新建一个上下文管理
代码:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;
using ZSZ.Service.Entities;
namespace ZSZ.Service
{
public class ZSZDbContext:DbContext //生成数据库之后去掉public最好,因为只想在Service用
{
public ZSZDbContext() : base("connstr")
{
//Database.SetInitializer<ZSZDbContext>(null); //生成以后再加这个就不用反复创建了
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly());
}
public DbSet<AdminUserEntity> AdminUsers { get; set; }
public DbSet<UserEntity> Users { get; set; }
public DbSet<CityEntity> Cities { get; set; }
public DbSet<CommunityEntity> Communities { get; set; }
public DbSet<PermissionEntity> Permissions { get; set; }
public DbSet<RegionEntity> Regions { get; set; }
public DbSet<RoleEntity> Roles { get; set; }
public DbSet<SettingEntity> Settings { get; set; }
public DbSet<AttachmentEntity> Attachments { get; set; }
public DbSet<HouseEntity> Houses { get; set; }
public DbSet<HouseAppointmentEntity> HouseAppointments { get; set; }
public DbSet<IdNameEntity> IdNames { get; set; }
public DbSet<HousePicEntity> HousePics { get; set; }
public DbSet<AdminLogEntity> AdminUserLogs { get; set; }
}
}
在UI项目里面右键添加引用:
这里用控制台直接运行:
ZSZDbContext ctx = new ZSZDbContext();
ctx.Database.Delete(); //先删除同名的 确保思维!!!
ctx.Database.Create();
大功告成!!!