简体   繁体   中英

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). When it loads from the database, I want it to populate this column with an instance of 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. More info:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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