多对多配置
先安装 Install-Package MySql.Data.EntityFrameworkCore
创建3个表
创建类
public class Role { public long Id { get; set; } public string Name { get; set; } } public class User { public long Id { get; set; } public string Name { get; set; } } public class UserRoleRelation { public long Id { get; set; } public long UserId { get; set; } public long RoleId { get; set; } public User User { get; set; } public Role Role { get; set; } }
编写DbContext,ef core的DbContext等EF的核心类在using Microsoft.EntityFrameworkCore;
public class MyDbContext : DbContext { public DbSetUsers { get; set; } public DbSet Roles { get; set; } public DbSet UserRoleRelations { get; set; } public DbSet WeixinUserinfo { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); optionsBuilder.UseMySQL("Server=127.0.0.1;database=test;uid=root;pwd=123456"); } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); var typeUser = modelBuilder.Entity (); typeUser.ToTable("T_Users"); var typeRole = modelBuilder.Entity (); typeRole.ToTable("T_Roles"); var typeUserRoleRelation = modelBuilder.Entity (); typeUserRoleRelation.ToTable("T_UserRoleRelations"); /** * 多对多:不用中间实体的多对多还不支持,要自己拆成用中间实体的两对一对多。 * EF中(一对多)写法:builder.HasRequired(e => e.Role).WithMany(); */ typeUserRoleRelation.HasOne(e=>e.Role).WithMany().HasForeignKey(e=>e.RoleId).IsRequired(); typeUserRoleRelation.HasOne(e=>e.User).WithMany().HasForeignKey(e=>e.UserId).IsRequired(); } }
通过UseMySQL
这样的扩展方法来配置连接字符串,这是.Net core的风格!可以把连接字符串写到配置文件中,然后再读取。
运行:
using (MyDbContext ctx = new MyDbContext()) { var user = ctx.Users.First(); long userId = user.Id; var relactions = ctx.UserRoleRelations.Include(e => e.Role) .Where(r => r.UserId == userId); foreach (var relation in relactions) { Console.WriteLine(relation.Role.Name); } }
链接:EF Fluent API
EntityTypeConfiguration
ef core 1.1
没有内置EntityTypeConfiguration,需要手动自己注册一个
- IEntityTypeConfiguration.cs
using Microsoft.EntityFrameworkCore;namespace Entity.Config{ public interface IEntityTypeConfiguration { void Map(ModelBuilder builder); } public interface IEntityTypeConfiguration: IEntityTypeConfiguration where T : class { void Map(ModelBuilder builder); }}
- EntityTypeConfiguration.cs
using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Metadata.Builders;namespace Entity.Config{ public abstract class EntityTypeConfiguration: IEntityTypeConfiguration where T : class { public abstract void Map(EntityTypeBuilder builder); public void Map(ModelBuilder builder) { Map(builder.Entity ()); } }}
- ModelBuilderExtenions.cs
using Microsoft.EntityFrameworkCore;namespace Entity.Config{ public static class ModelBuilderExtenions { private static IEnumerableGetMappingTypes(this Assembly assembly, Type mappingInterface) { return assembly.GetTypes().Where(x => !x.GetTypeInfo().IsAbstract && x.GetInterfaces().Any(y => y.GetTypeInfo().IsGenericType && y.GetGenericTypeDefinition() == mappingInterface)); } public static void AddEntityConfigurationsFromAssembly(this ModelBuilder modelBuilder, Assembly assembly) { var mappingTypes = assembly.GetMappingTypes(typeof(IEntityTypeConfiguration<>)); foreach (var config in mappingTypes.Select(Activator.CreateInstance).Cast ()) { config.Map(modelBuilder); } } }}
修改MyDbContext.cs
protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); //modelBuilder.AddEntityConfigurationsFromAssembly(Assembly.GetEntryAssembly());//参数表示config类所在的程序集 modelBuilder.AddEntityConfigurationsFromAssembly(Assembly.Load("Entity"));//这里加载的是 Entity程序集 ... }
下面示例创建一个UserRoleRelationConfig.cs,其他自己建
using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Metadata.Builders;namespace Entity.Config{ public class UserRoleRelationConfig:EntityTypeConfiguration{ public override void Map(EntityTypeBuilder builder) { builder.ToTable("T_UserRoleRelations"); /** * 多对多:不用中间实体的多对多还不支持,要自己拆成用中间实体的两对一对多。 * EF中(一对多)写法:builder.HasRequired(e => e.Role).WithMany(); */ builder.HasOne(e => e.Role).WithMany().HasForeignKey(e => e.RoleId).IsRequired(); builder.HasOne(e => e.User).WithMany().HasForeignKey(e => e.UserId).IsRequired(); } }}
ef core 2.0
EF Core 2.0 中内置了 IEntityTypeConfiguration
public class UserRoleRelationConfig:IEntityTypeConfiguration{ public void Configure(EntityTypeBuilder builder) { builder.ToTable("T_UserRoleRelations"); /** * 多对多:不用中间实体的多对多还不支持,要自己拆成用中间实体的两对一对多。 * EF中(一对多)写法:builder.HasRequired(e => e.Role).WithMany(); */ builder.HasOne(e => e.Role).WithMany().HasForeignKey(e => e.RoleId).IsRequired(); builder.HasOne(e => e.User).WithMany().HasForeignKey(e => e.UserId).IsRequired(); } }