Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net EF4 Builder.Configurations-无法推断实体类型的键_.net_Entity Framework_Ef4 Code Only - Fatal编程技术网

.net EF4 Builder.Configurations-无法推断实体类型的键

.net EF4 Builder.Configurations-无法推断实体类型的键,.net,entity-framework,ef4-code-only,.net,Entity Framework,Ef4 Code Only,我想知道是否有人能帮助我 我使用的是VS2010、.Net 4、EF4,我试图生成一个objectcontext,它完全是在代码中配置的,而不是模型优先或数据库优先 简言之,我确实: 创建一个ContextBuilder 为指定PK、FKs、Nullable等的每个实体配置ContextBuilder。 我要求ContextBuilder使用提供的连接字符串为我创建一个上下文。 上述最后一步失败,出现以下异常: Unable to infer a key for entity type 'MyN

我想知道是否有人能帮助我

我使用的是VS2010、.Net 4、EF4,我试图生成一个objectcontext,它完全是在代码中配置的,而不是模型优先或数据库优先

简言之,我确实:

创建一个ContextBuilder 为指定PK、FKs、Nullable等的每个实体配置ContextBuilder。 我要求ContextBuilder使用提供的连接字符串为我创建一个上下文。 上述最后一步失败,出现以下异常:

Unable to infer a key for entity type 'MyNamespace.MyEntityName'.
我认为这意味着它不能为我的实体推断主键/标识列

我的代码如下。引用的ContextExtension是一个包装类,它只是从ObjectContext继承

我的实体定义如下:

Public Class Sector
    Implements IEntity
    Public Overridable Property id As Long Implements IEntity.id
    Public Overridable Property Universe As Universe
    Public Overridable Property SolarSystems As ICollection(Of SolarSystem)
End Class
我的实体配置如下所示:

Public Class SectorConfig
    Inherits EntityConfiguration(Of Sector)
    Public Sub New()
        [Property](Function(x) x.id).IsIdentity()
        Relationship(Function(x) x.Universe).FromProperty(Function(y) y.Sectors).IsRequired()
    End Sub
End Class
如果我从配置中排除扇区,那么下一个实体也会发生同样的情况,以此类推-因此,要么所有实体/配置都是错误的,要么不是实体/配置问题

如果我在Create调用之前检查生成器,我可以看到3个配置,它们与我的实体和指定的id字段相匹配,并具有以下内容:我在[]s中的注释

Builder.Configurations.Items(0)[Sector].Items(0)[id].Value.StoreGeneratedPattern = Identity {1}
这似乎意味着配置应用正确

有人能解释一下我为什么会遇到异常,以及如何着手解决这个问题吗


非常感谢

我使用以下语法解决了这个问题:

Public Class SectorConfig
    Inherits EntityConfiguration(Of Sector)
    Public Sub New()
        HasKey(Function(x) X.id)
        [Property](Function(x) x.id).IsIdentity()
        Relationship(Function(x) x.Universe).FromProperty(Function(y) y.Sectors).IsRequired()
    End Sub
End Class
注意附加的HasKey调用

Public Class SectorConfig
    Inherits EntityConfiguration(Of Sector)
    Public Sub New()
        HasKey(Function(x) X.id)
        [Property](Function(x) x.id).IsIdentity()
        Relationship(Function(x) x.Universe).FromProperty(Function(y) y.Sectors).IsRequired()
    End Sub
End Class