[英]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.