简体   繁体   English

没有方法将 map 实体添加到实体框架核心中

[英]No method to map entity to table in entity framework core

I am using entity framework core in a normal .net project, version 4.7.我在一个普通的 .net 项目 4.7 版中使用实体框架核心。 I know I can do this.我知道我能做到。 The problem is that I can't seem to map an entity to a table because the "ToTable" method doesn't exist.问题是我似乎无法将实体 map 添加到表中,因为“ToTable”方法不存在。 I can't edit the poco or entity classes because they are predefined and generated.我无法编辑 poco 或实体类,因为它们是预定义和生成的。 So I can't use the attribute.所以我不能使用该属性。 I looked on the inte.net and everyone seems to use this method to map an entity to a table.我在inte.net上看了看,好像大家都用这个方法把map一个实体转表。

Here is my code:这是我的代码:

public class FactsDbContext : DbContext
{
    public DbSet<TblIncident> TblIncidents { get; set; }

    public DbSet<TblAction> TblActions { get; set; }

    public DbSet<TblAddressTypeAlias> TblAddressTypeAliases { get; set; }

    public DbSet<TblCountry> TblCountries { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        //these methods don't exist in my case
        modelBuilder.Entity<TblIncident>(entity => entity.ToTable("Incident"));
        modelBuilder.Entity<TblIncident>().ToTable("Incident");
    }
}

I also tried to use IEntityTypeConfiguration with a EntityTypeBuilder but it still don't have access to the map to table method:我还尝试将 IEntityTypeConfiguration 与 EntityTypeBuilder 一起使用,但它仍然无法访问 map 表方法:

public class IncidentConfig : IEntityTypeConfiguration<TblIncident>
{
    public void Configure(EntityTypeBuilder<TblIncident> builder)
    {
        builder.ToTable("Incident");
    }
}

I looked into the Entity Framework Core repository on GitHub and searched for the method "Totable" inside the repository. 我查看了GitHub上的Entity Framework Core存储库,并在存储库中搜索了“Totable”方法。 It turns out it is defined as an extension method but it is in separate nuget package and library called Microsoft.EntityFrameworkCore.SqlServer 事实证明它被定义为一个扩展方法,但它位于单独的nuget包和库中,名为Microsoft.EntityFrameworkCore.SqlServer

After I downloaded the package I got the Totable method that I need. 下载软件包后,我得到了我需要的Totable方法。 Still it doesn't make sense to add that method in a separate package for sql server when you already have the "Table" attribute that you can add on entities directly in the entity framework core package. 当您已经具有可以直接在实体框架核心包中添加实体的“表”属性时,将该方法添加到sql server的单独包中仍然没有意义。

You can use the below approach. 您可以使用以下方法。 You have to use Table data annotation. 您必须使用Table数据注释。

DBContext: 的DbContext:

public virtual DbSet<Article> Article { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<Article>(b =>
    {
        b.Property(e => e.Property1).HasDefaultValue(true);
        ... //Other properties
    }

Model class: 型号类:

[Table("Article")]
public class Article
{

You can also use to ToTable in DBContext, but you have to make sure that you have included using Microsoft.EntityFrameworkCore; 您也可以在ToTable中使用ToTable,但您必须确保已using Microsoft.EntityFrameworkCore; .

Line modelBuilder.Entity<TblIncident>().ToTable("Incident"); Line modelBuilder.Entity<TblIncident>().ToTable("Incident"); looks correct according to the documentation. 根据文档看起来是正确的。 https://docs.microsoft.com/en-us/ef/core/modeling/relational/tables#fluent-api https://docs.microsoft.com/en-us/ef/core/modeling/relational/tables#fluent-api

It's very old thread but I got the same issue and I solved it by placing base.OnModelCreating(builder) as a first line of OnModelCreating method.这是非常老的线程,但我遇到了同样的问题,我通过将base.OnModelCreating(builder)作为OnModelCreating方法的第一行来解决它。

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    // Rest of the code
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM