C# 实体框架6中的Fluent Api与实体框架核心不兼容
我使用EntityFramework6实现了FluentAPI。在使用EnityFrameworkCore实现相同功能时出现问题 下面是使用EntityFramework 6使用Fluent API的代码C# 实体框架6中的Fluent Api与实体框架核心不兼容,c#,entity-framework,asp.net-core,entity-framework-core,ef-fluent-api,C#,Entity Framework,Asp.net Core,Entity Framework Core,Ef Fluent Api,我使用EntityFramework6实现了FluentAPI。在使用EnityFrameworkCore实现相同功能时出现问题 下面是使用EntityFramework 6使用Fluent API的代码 public class CustomerConfiguration : EntityTypeConfiguration<Customers> { public CustomerConfiguration() { T
public class CustomerConfiguration : EntityTypeConfiguration<Customers>
{
public CustomerConfiguration()
{
ToTable("Customers");
Property(c => c.FirstName).IsRequired().HasMaxLength(50);
Property(c => c.LastName).IsRequired().HasMaxLength(50);
Property(c => c.Gender).IsRequired().HasMaxLength(10);
Property(c => c.Email).IsRequired().HasMaxLength(25);
Property(c => c.Address).IsRequired().HasMaxLength(50);
Property(c => c.City).IsRequired().HasMaxLength(25);
Property(c => c.State).IsOptional().HasMaxLength(15);
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new CustomerConfiguration());
modelBuilder.Configurations.Add(new OrderConfiguration());
modelBuilder.Configurations.Add(new ProductConfiguration());
modelBuilder.Entity<Orders>()
.HasRequired(c => c.Customers)
.WithMany(o => o.Orders)
.HasForeignKey(f => f.CustomerId);
modelBuilder.Entity<Orders>()
.HasMany<Products>(s => s.Products)
.WithMany(c => c.Orders)
.Map(cs =>
{
cs.MapLeftKey("OrderRefId");
cs.MapRightKey("ProductRefId");
cs.ToTable("OrderDetails");
});
}
公共类客户配置:EntityTypeConfiguration
{
公共客户配置()
{
ToTable(“客户”);
属性(c=>c.FirstName).IsRequired().HasMaxLength(50);
属性(c=>c.LastName).IsRequired().HasMaxLength(50);
属性(c=>c.Gender).IsRequired().HasMaxLength(10);
属性(c=>c.Email).IsRequired().HasMaxLength(25);
属性(c=>c.Address).IsRequired().HasMaxLength(50);
属性(c=>c.City).IsRequired().HasMaxLength(25);
属性(c=>c.State).IsOptional().HasMaxLength(15);
}
}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Configurations.Add(新CustomerConfiguration());
添加(neworderconfiguration());
modelBuilder.Configurations.Add(新产品配置());
modelBuilder.Entity()
.HasRequired(c=>c.Customers)
.有许多(o=>o.订单)
.HasForeignKey(f=>f.CustomerId);
modelBuilder.Entity()
.HasMany(s=>s.Products)
.有许多(c=>c.订单)
.Map(cs=>
{
cs.MapLeftKey(“OrderRefId”);
cs.MapRightKey(“ProductRefId”);
cs.ToTable(“订单详情”);
});
}
我在EntityFrameworkCore中遇到的问题是
有人能告诉我如何在EntityFrameWork Core中使用Fluent API实现这一点吗?EF Core使用完全不同的API。所以你必须先学习它 例如:这是它设置
ToTable()
的方式
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
modelBuilder.Entity()
.ToTable(“博客”);
}
要学习,您必须阅读以下链接:
在类中封装实体类型的配置:
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Microsoft.EntityFrameworkCore
{
public abstract class EntityTypeConfiguration<TEntity>
where TEntity : class
{
public abstract void Map(EntityTypeBuilder<TEntity> builder);
}
public static class ModelBuilderExtensions
{
public static void AddConfiguration<TEntity>(this ModelBuilder modelBuilder, EntityTypeConfiguration<TEntity> configuration)
where TEntity : class
{
configuration.Map(modelBuilder.Entity<TEntity>());
}
}
}
使用Microsoft.EntityFrameworkCore.Metadata.Builders;
命名空间Microsoft.EntityFrameworkCore
{
公共抽象类EntityTypeConfiguration
地点:班级
{
公共抽象空图(EntityTypeBuilder);
}
公共静态类ModelBuilderExtensions
{
公共静态void AddConfiguration(此ModelBuilder ModelBuilder,EntityTypeConfiguration配置)
地点:班级
{
Map(modelBuilder.Entity());
}
}
}
您可以在此处查看更多详细信息(请参阅由@rowanmiller编辑的第一篇文章):直接链接到文档。
阅读其余部分。EF core是一种新产品,不是EF6的升级。请查看EF core文档:因此我无法创建单独的配置文件,它们会在onmodelCreating中添加配置。您可以参考此链接。请让我知道这是否有效?据我所知,您不能。EntityType的问题是
配置
仍处于打开状态:感谢link.EF团队提供了解决方案。请参阅我的帖子@BotondBalázlooks great!我不明白为什么他们没有将其包含在框架中。
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Microsoft.EntityFrameworkCore
{
public abstract class EntityTypeConfiguration<TEntity>
where TEntity : class
{
public abstract void Map(EntityTypeBuilder<TEntity> builder);
}
public static class ModelBuilderExtensions
{
public static void AddConfiguration<TEntity>(this ModelBuilder modelBuilder, EntityTypeConfiguration<TEntity> configuration)
where TEntity : class
{
configuration.Map(modelBuilder.Entity<TEntity>());
}
}
}