简体   繁体   English

如何在实体框架核心中使用泛型类型?

[英]How can I use a generic type with entity framework core?

If I have a domain model that looks something like this:如果我有一个看起来像这样的域模型:

public class Foo<T> {
    public Guid Id { get; set; }
    public string Statement { get; set; }
    public T Value { get; set; }
}

I want to use it for built in Data Types (string, int, etc...) as well as date.我想将它用于内置数据类型(字符串、整数等...)以及日期。 I want to use it like:我想像这样使用它:

var foo = new Foo<string>();
foo.Value = "Hey";

how can I persist this to a database using EF Core?如何使用 EF Core 将其保存到数据库中?

I imagine the database table would look like我想数据库表看起来像

| Id | Statement | ValueAsString | ValueAsDecimal | ValueAsDate | ValueAsInt | 
| 1  | NULL      | "Hey"         |                |             |            |
| 2  | NULL      |               | 1.1            |             |            |

you should still have a class.你应该还有一堂课。 Your class Foo should be abstract.你的类Foo应该是抽象的。 So you would get":所以你会得到“:

public abstract class Foo<T> {
    public Guid Id { get; set; }
    public string Statement { get; set; }
    public T Value { get; set; }
}

then your implementation class would be:那么你的实现类将是:

public class Orders: Foo<Order> {
}

now you have your Orders class with your generic type which can be stored.现在您拥有了可以存储的具有泛型类型的Orders类。

If you want to persist different values types to the database in a single table similar to the one in your question, you can do it like this:如果您想将不同的值类型持久保存到数据库中类似于您问题中的表的单个表中,您可以这样做:

public interface IHasValue<T> {
    T Value { get; set; }
}

public abstract class Foo {
    public Guid Id { get; set; }
    public string Statement { get; set; }
}

public class Foostring : Foo, IHasValue<string> {
    string Value { get; set; }
}

public class FooInt : Foo, IHasValue<int> {
    int Value { get; set; }
}

In your DbContext class add properties:在您的DbContext类中添加属性:

public DbSet<FooString> FooStrings { get; set: }
public DbSet<FooInt> FooInts { get; set; }

You can set the column names for the table in the OnModelCreating method of your DbContext :您可以在表中设置的列名OnModelCreating你的方法DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    // include the base class so a single table is created for the hierarchy
    // rather than a table for each child class
    modelBuilder.Entity<Foo>().ToTable("Foos");

    // Specify the column names or you will get weird names
    modelBuilder.Entity<FooString>().Property(entity => entity.Value)
        .HasColumnName("ValueAsString");
    modelBuilder.Entity<FooInt>().Property(entity => entity.Value)
        .HasColumnName("ValueAsInt");
}

This code will generate a table Foos with columns Id , Statement , Discriminator , ValueAsString and ValueAsInt .此代码将生成一个表Foos其中包含IdStatementDiscriminatorValueAsStringValueAsInt More info on the Discrimiator column can be found here可以在此处找到有关Discrimiator列的更多信息

结果表的图像

You still need to create a class for each Type/column you want to use for T , I don't think you can get around that.您仍然需要为要用于T每个类型/列创建一个类,我认为您无法解决这个问题。

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

相关问题 如何使用实体框架通过 ASP.NET Core Web API 处理通用查询? - How can I process a generic query through ASP.NET Core Web API using Entity Framework? 如何将现有实体附加到通用的实体框架上下文中? - How can I attach an existing entity to the entity framework context in generic? 如何使用 Entity Framework Core 3.0 创建所需的拥有类型 - How can I create a Required Owned Type with Entity Framework Core 3.0 在Entity Framework 6.1(非Core)中,如何使用IndexAttribute定义聚簇索引? - In Entity Framework 6.1 (not Core), how can I use the IndexAttribute to define a clustered index? 如何使用Entity Framework Core功能更改数据库? - How can I use the Entity Framework Core capabilities to change my database? 如何在 Azure Functions 中使用 Entity Framework Core - How do I use Entity Framework Core in Azure Functions 实体框架核心通用查询 - Entity framework core generic query 如何使用实体框架作为 DataGridView 的通用数据源? - How to use Entity Framework as generic DataSource for DataGridView? 如何在实体框架核心中将属性映射到类实例? - How can I map a property to a class instance in entity framework core? 如何在实体框架核心中制作动态查询过滤器? - How can I make a dynamic query filter in entity framework core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM