我可以将接口用作实体框架的属性吗? [英] Can I use an Interface as a property with Entity Framework?

查看:60
本文介绍了我可以将接口用作实体框架的属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类,它是数据库表的模型:

I have the following class which is a model for a database table:

public class User : IUser
{
    internal int Id { get; set; }

    public string Name { get; set; }
    public string Email { get; set; }

    public ITest TestData { get; set; }
}

当我运行 Update-Database 命令时,它会引发错误:

When I run Update-Database command, it throws an error:

属性 User.TestData 是接口类型( ITest ).如果它是导航属性,则通过将其强制转换为映射的实体类型来手动配置该属性的关系

The property User.TestData is of an interface type (ITest). If it is a navigation property manually configure the relationship for this property by casting it to a mapped entity type

如果我不希望实际的属性成为类,那么如何以及在何处手动配置此属性的关系?

How and where can I manually configure the relationship for this property if I do not want the actual property to be a class?

推荐答案

EF不支持接口,但是您可以通过这种方式进行处理.看看此解决方案如何在CodeFirst中使用接口属性

EF does not support interfaces, but you can deal with it this way. Take a look on this solution How to use interface properties with CodeFirst

这篇关于我可以将接口用作实体框架的属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆