vb.net ReferentialConstraint 中的依赖属性映射到存储生成的列。列:'费用ID'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13982957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:37:54  来源:igfitidea点击:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'FeeID'

asp.net-mvcvb.netentity-frameworkentity-framework-5

提问by Sam Axe

I'm getting the error:

我收到错误:

{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'FeeID'."}

{"ReferentialConstraint 中的依赖属性映射到存储生成的列。列:'FeeID'。"}

The setup: I've created the database by hand (SQL Server 2012 and SSMS)

设置:我手动创建了数据库(SQL Server 2012 和 SSMS)

I do NOT have an edmx file

我没有 edmx 文件

I have two classes, FeeMetaDataand Fee, which map to two tables in the database (PFD.FeeMetaDataand PFD.Fees)

我有两个类,FeeMetaDataFee,它们映射到数据库中的两个表(PFD.FeeMetaDataPFD.Fees

Database Structure

数据库结构

FeeMetadata
------------ 
FeeID  BIGINT  IDENTITY(1,1) PRIMARY KEY 
Something VARCHAR(25) NOT NULL

Fees
------------ 
FeeID  BIGINT  PRIMARY KEY NOT NULL
FeeBatchID  BIGINT NOT NULL
PersonID BIGINT 
Amount DECIMAL(18,2) NOT NULL
DueDate DATE NOT NULL

There is a 1-to-1 relationship between FeeMetadata.FeeID and Fees.FeeID

FeeMetadata.FeeID 和 Fees.FeeID 之间存在一对一的关系

Class Structure

班级结构

Imports System.ComponentModel.DataAnnotations
Imports System.ComponentModel.DataAnnotations.Schema
Namespace PFD
    <Table("FeeMetadata", Schema:="PFD")>
    Public Class FeeMetadata

        Public Sub New()
            MyBase.New()
        End Sub

        Public Sub New(ByVal tFee As SOACourt_v1)
            Me.New()
            Me.PfdFee = New PFD.Fee(tFee)
        End Sub

        <Key>
        <DatabaseGenerated(DatabaseGeneratedOption.Identity)>
        Public Property FeeID As Int64

        Public Property Something As String

        <ForeignKey("FeeID")>
        Public Property PfdFee As PFD.Fee
    End Class
End Namespace





Namespace PFD
<Table("Fees", Schema:="PFD")>
Public Class Fee
    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal tFee As SOACourt_v1)
        Me.New()
        Me.Amount = tFee.Amount
        Me.DueDate = tFee.DueDate
    End Sub


    <DatabaseGenerated(DatabaseGeneratedOption.None)>
    Public Property FeeID As Int64

    Public Property FeeBatchID As Int64 = 0

    Public Property PersonID As Int64? = 0

    Public Property Amount As Decimal

    Public Property DueDate As Date = Date.Today
End Class
End Namespace

Usage

用法

Using tContext As FeesContext = New FeesContext
    For Each tFee As SOACourt_v1 In tFees
        tContext.FeeMetadata.Add(New PFD.FeeMetadata(tFee))
    Next
    tContext.SaveChanges()     '  <---- Error occurs here
End Using

Any Ideas on what is causing the error:

关于导致错误的原因的任何想法:

{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'FeeID'."}

{"ReferentialConstraint 中的依赖属性映射到存储生成的列。列:'FeeID'。"}

回答by Michael Edenfield

Although I haven't used EF code-first yet, this looks like the same error you get if you set up your entity relationships incorrectly in a model diagram. In particular, it looks to me like you have your foreign key set up backwards.

虽然我还没有使用 EF 代码优先,但这看起来与您在模型图中错误地设置实体关系时得到的错误相同。特别是,在我看来,您的外键设置向后。

The error is because you told Entity Framework to use the FeeIdfield as the foreign key between FeeMetaDataand Fee, but that field is auto-generated in the FeeMetaDataclass. That's almost certainly not what you intended to do.

错误是因为您告诉 Entity Framework 将该FeeId字段用作FeeMetaData和之间的外键Fee,但该字段是在FeeMetaData类中自动生成的。这几乎肯定不是你打算做的。

If Feeis the primary table and FeeMetaDatahas the foreign key, then you should put the identity field in Fee. If the tables are the other way around, then your classes are backwards and you should instead define Feeto have a FeeMetaDataproperty with FeeIdas the foreign key.

如果Fee是主表并且FeeMetaData有外键,那么你应该把身份字段放在Fee. 如果表是相反的,那么你的类是向后的,你应该定义Fee一个FeeMetaData属性FeeId作为外键。