在ReferentialConstraint从属属性映射到一个存储生成列。专栏:“FeeID” [英] A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'FeeID'
问题描述
我收到以下错误:
{在ReferentialConstraint从属属性映射到一个
存储生成列。专栏:FeeID}
块引用>的设置:
我的手(SQL Server 2012的SSMS和)创建的数据库我没有一个EDMX文件
我有两个班,
FeeMetaData code>和
费用
,它映射到两个表中的数据库(PFD.FeeMetaData code>和
PFD.Fees
)数据库结构
FeeMetadata
------------
FeeID BIGINT IDENTITY(1,1)PRIMARY KEY
一些VARCHAR(25)NOT NULL费用
------------
FeeID BIGINT PRIMARY KEY NOT NULL
FeeBatchID BIGINT NOT NULL
是PersonID BIGINT
量DECIMAL(18,2)NOT NULL
DUEDATE DATE NOT NULL有是FeeMetadata.FeeID和Fees.FeeID之间的1对1的关系。
类结构
导入System.ComponentModel.DataAnnotations
进口System.ComponentModel.DataAnnotations.Schema
命名空间PFD
<表(FeeMetadata模式:=PFD)>
公共类FeeMetadata 的Public Sub New()
MyBase.New()
结束小组 的Public Sub New(BYVAL tFee作为SOACourt_v1)
Me.New()
Me.PfdFee =新PFD.Fee(tFee)
结束小组 <关键>
< DatabaseGenerated(DatabaseGeneratedOption.Identity)GT;
公共属性FeeID作为Int64的 公共财产的东西作为字符串 < ForeignKey的(FeeID)>
公共属性PfdFee作为PFD.Fee
末级
最终命名空间命名空间PFD
<表(费用,架构:=PFD)>
公共课时费
的Public Sub New()
MyBase.New()
结束小组 的Public Sub New(BYVAL tFee作为SOACourt_v1)
Me.New()
Me.Amount = tFee.Amount
Me.DueDate = tFee.DueDate
结束小组
< DatabaseGenerated(DatabaseGeneratedOption.None)GT;
公共属性FeeID作为Int64的 公共属性FeeBatchID作为的Int64 = 0 公共财产是PersonID作为Int64的? = 0 公共财产金额为十进制 公共属性交货期为日期= Date.Today
末级
最终命名空间用法
使用tContext作为FeesContext =新FeesContext
对于每个tFee作为SOACourt_v1在tFees
tContext.FeeMetadata.Add(新PFD.FeeMetadata(tFee))
下一个
tContext.SaveChanges()'< ----这里发生错误
使用完这是什么原因造成的错误任何想法:
{在ReferentialConstraint从属属性映射到一个
存储生成列。专栏:FeeID}
块引用>解决方案虽然我没有使用EF code-先然而,这看起来像你得到了同样的错误,如果你在一个不正确设置你的实体关系模型图。特别是,它看起来像你对我有你的外键设置反了。
这个错误是因为你对实体框架使用
FeeId
字段和FeeMetaData code>之间的外键
费用
,但是那场是自动生成的FeeMetaData code>类。这几乎可以肯定不是你打算做。
如果
费用
是主表和FeeMetaData code>的外键,那么你应该把身份领域
费用
。如果表是倒过来,那么你的类是倒退,你应该不是定义费用
有一个FeeMetaData code>与
FeeId
属性作为外键。I'm getting the error:
{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'FeeID'."}
The setup: I've created the database by hand (SQL Server 2012 and SSMS)
I do NOT have an edmx file
I have two classes,
FeeMetaData
andFee
, which map to two tables in the database (PFD.FeeMetaData
andPFD.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
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'."}
解决方案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.
The error is because you told Entity Framework to use the
FeeId
field as the foreign key betweenFeeMetaData
andFee
, but that field is auto-generated in theFeeMetaData
class. That's almost certainly not what you intended to do.If
Fee
is the primary table andFeeMetaData
has the foreign key, then you should put the identity field inFee
. If the tables are the other way around, then your classes are backwards and you should instead defineFee
to have aFeeMetaData
property withFeeId
as the foreign key.这篇关于在ReferentialConstraint从属属性映射到一个存储生成列。专栏:“FeeID”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!