c# - Entity Framework 6 中的 Fluent Api 不兼容 Entity Framework Core

标签 c# entity-framework asp.net-core entity-framework-core ef-fluent-api

我使用 Entity Framework 6 实现了 Fluent API。使用 EnityFrameworkCore 实现相同的 API 时遇到问题。

下面是使用 EntityFramework 6 使用 Fluent API 的代码

 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");
                });


        }

我在 EntityFrameworkCore 中遇到的问题是

  1. 它无法识别 CustomerConfiguration() 中的 ToTable 和 Property 关键字
  2. 它无法识别 OnModelCreating 方法中的 Configurations、HasRequired、MapLeftKey、MapRightKey、ToTable 关键字

有人可以告诉我如何使用 EntityFrameWork Core 中的 Fluent API 来实现此目的

最佳答案

EF core使用完全不同的API,所以你必须先学习它。

举个例子:这是设置 ToTable() 的方式。

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .ToTable("blogs");
    }

要了解,您必须阅读这些链接:

Table Mapping

Relationships

Creating a Model

将实体类型的配置封装在类中:

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>());
        }
    }
}

您可以在此处查看更多详细信息(请参阅由 @rowanmiller 编辑的第一篇文章:Github

关于c# - Entity Framework 6 中的 Fluent Api 不兼容 Entity Framework Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708056/

相关文章:

c# - 如何在 ASP.NET Core 1 和 VSCode 编辑器中使用 WCF 服务?

C# 项目在很短的时间内为空?

c# - 如何将 EF4 代码优先 ICollection 转换为 EntityCollection?

c# - asp.net core 2 从本地磁盘驱动器加载和显示图像

c# - EF 6 代码第一个存储过程 - 只读

c# - 如何在 EF Core 中连接多个表中仅包含某些列的数据集?

c# - 如何在 .Net Core 测试中模拟 UserManager?

c# - 我无法使用 QBitNinjaClient 获取交易结果

c# - UseWindowsAzureActiveDirectoryBearerAuthentication 如何验证 token ?

entity-framework - 在下一个 Entity Framework 中选择 N+1