简体   繁体   English

如何在实体框架核心中将属性映射到类实例?

[英]How can I map a property to a class instance in entity framework core?

I've got a class 我上课了

public class MyEntity
{
    public IDateTimeProvider DateTimeProvider { get; }

    public MyEntity(IDateTimeProvider dateTimeProvider)
    {
        DateTimeProvider = dateTimeProvider;
    }

    protected MyEntity()
    {
    }
}

When I map this to the DbContext , I want it to ignore this column (as it's not a primitive type). 当我将此映射到DbContext ,我希望它忽略此列(因为它不是原始类型)。 When it loads from the database, I want it to populate this column with an instance of DateTimeProvider . 当它从数据库加载时,我希望它用DateTimeProvider的实例填充此列。

Does anyone know how to do this? 有谁知道如何做到这一点?

I want to say something like: 我想说些类似的话:

modelBuilder
    .Entity<MyEntity>()
    .Property(x => DateTimeProvider)
    .Ignore()
    .OnLoadInject(new DateTimeProvider());

If you do not want to create a column in the database, you can use the [NotMapped] annotation on the property. 如果不想在数据库中创建列,则可以在属性上使用[NotMapped]批注。 More info: 更多信息:

http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx http://www.entityframeworktutorial.net/code-first/notmapped-dataannotations-attribute-in-code-first.aspx

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

相关问题 如何将一个属性映射到与实体框架的多个关系? - How can I map one property to multiple relations with entity framework? 将鉴别器列映射到类 Property Entity Framework Core 2.2.0 - Map Discriminator Column to class Property Entity Framework Core 2.2.0 如何在实体框架中映射类型为“字典”的属性? - How do I map property with of type 'Dictionary' with Entity Framework? 实体框架,我可以将类映射到键/值表吗? - Entity framework, can I map a class to a key/value table? 如何在不使用Entity Framework且仅使用ADO.NET的情况下将我的实体映射到.Net Core项目中的表 - How can I map my entity to a table in .Net Core project without using Entity Framework and only using ADO.NET 实体框架核心LINQ类属性/方法不能使用Included属性 - Entity Framework Core LINQ Class Property/Methods can't use Included Property 如何使用Entity Framework映射具有实体集合的派生类 - How can I map a derived class having a collection of entities with Entity Framework 如何使用自定义 class 作为具有 Entity Framework Core 转换的实体的属性? - How do you use a custom class as a property of an entity with a conversion with Entity Framework Core? 如何更新 Entity Framework Core 中的单个属性 - How to update a single property in Entity Framework Core Map 使用 Entity Framework Core 的图像类型属性 - Map Image type property using Entity Framework Core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM