简体   繁体   English

数据库计算的属性,它是Entity Framework 6中的外键

[英]Database computed property that is a foreign key in Entity Framework 6

How can I reference a property to be a ForeignKey plugged to a DB computed field (from a view) ? 我如何引用一个属性作为插入数据库计算字段(从视图)的ForeignKey?

I have the following : 我有以下内容:

public class PersonSite
{
    public int Id {get;set;}

    //...

    public int PersonId {get;set;}
    [ForeignKey("PersonId")]
    public virtual Person Person {get;set;}

    //...

}

public class Person
{
      public Id {get;set;}

      //...

      [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
      public int? MainPersonSiteId { get; protected internal set; }

      [ForeignKey("MainPersonSiteId")]
      [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
      public PersonSite MainPersonSite { get; protected internal set; }

      //...
}

Whenever I try to update a Person , I get the following excpetion: 每当我尝试更新Person ,都会得到以下提示:

Message: 信息:

A dependent property in a ReferentialConstraint is mapped to a store-generated column. ReferentialConstraint中的从属属性映射到商店生成的列。 Column: 'MainPersonSiteId'. 列:“ MainPersonSiteId”。 Stack trace: 堆栈跟踪:

at System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildSetClauses(DbExpressionBinding target, PropagatorResult row, PropagatorResult originalRow, TableChangeProcessor processor, Boolean insertMode, Dictionary`2& outputIdentifiers, DbExpression& returning, Boolean& rowMustBeTouched) at System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildUpdateCommand(PropagatorResult oldRow, PropagatorResult newRow, TableChangeProcessor processor) at System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode, UpdateCompiler compiler) 在System.Data中的System.Data.Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildSetClauses(DbExpressionBinding目标,PropagatorResult行,PropagatorResult originalRow,TableChangeProcessor处理器,布尔insertMode,Dictionary`2&outputIdentifiers,DbExpression&returning,Boolean&rowMustBeTouched)中。 Entity.Core.Mapping.Update.Internal.UpdateCompiler.BuildUpdateCommand(PropagatorResult oldRow,PropagatorResult newRow,TableChangeProcessor处理器)位于System.Data.Entity.Core.Mapping.Update.Internal.TableChangeProcessor.CompileCommands(ChangeNode changeNode,UpdateCompiler编译器)

In the database, which is Sql Server 12, Person comes from a view, where MainPersonSiteId is projected from a function. 在数据库中,即Sql Server 12,Person来自一个视图,该视图从一个函数投影MainPersonSiteId。 This is a computed field, which does not need to be updated by the ORM. 这是一个计算字段,无需由ORM更新。

How to define it with EntityFramework ? 如何用EntityFramework定义它?

EDIT : 编辑:

I just managed to have update working by setting DatabaseGeneratedOption.Identity instead of DatabaseGeneratedOption.Computed , nevertheless I just found the inserts were still broken. 我只是通过设置DatabaseGeneratedOption.Identity而不是DatabaseGeneratedOption.Computed设法使更新工作,但是我只是发现插入仍然损坏。 So I tested the inserts with DatabaseGeneratedOption.Computed back and it ... worked :/ 所以我用DatabaseGeneratedOption.Computed back进行了测试,它工作了:/

The situation is : 情况是:
- I can insert only with DatabaseGeneratedOption.Computed -我只能使用DatabaseGeneratedOption.Computed插入
- I can update only with DatabaseGeneratedOption.Identity -我只能使用DatabaseGeneratedOption.Identity更新

It's strange as MSDN says that DatabaseGeneratedOption.Computed let the DB generate a value for both inserts and updates while DatabaseGeneratedOption.Identity does only for inserts 正如MSDN所说的DatabaseGeneratedOption.Computed让数据库为插入和更新生成一个值,而DatabaseGeneratedOption.Identity只对插入生成一个值,这很奇怪。

Columns in the database defined as IDENTITY are different from COMPUTED ones. 数据库中定义为IDENTITY的列与COMPUTED的列不同。

When you insert a new row, then the database engine creates a new value (number) for an IDENTITY-Column. 当您插入新行时,数据库引擎将为IDENTITY-Column创建一个新值(数字)。 For example you can use this as a primary/unique key column. 例如,您可以将其用作主键/唯一键列。 You can not update an IDENTITY column. 您无法更新IDENTITY列。 You can't define a formula for doing calculations. 您无法定义用于计算的公式。

When you want to calculate somethin' at the table level, you can use computed columns. 当您想在表级别上计算时,可以使用计算列。 Here you have to define a formula. 在这里,您必须定义一个公式。 When you create the computed column with the PERSISTED option, then SQL-Server stores the computed values in the table. 当使用PERSISTED选项创建计算列时,SQL-Server会将计算值存储在表中。 When you update columns which are part of a computed column, only then the computed column gets updated. 当您更新属于计算列的列时,仅计算列会被更新。 PERSISTED computed columns can be part of index-key-columns and foreign keys. 持久的计算列可以是索引键列和外键的一部分。 When you don't use the PERSISTED option, the database does not store the computed values. 当您不使用PERSISTED选项时,数据库将不存储计算的值。 So it has to do the calculation every time a query needs the computed column. 因此,每当查询需要计算列时就必须进行计算。

When you update a row, make sure the computed column is not part of list of columns to be updated (in the SET-clause of an UPDATE-statement). 更新行时,请确保计算出的列不属于要更新的​​列列表的一部分(在UPDATE语句的SET子句中)。

You can define FOREIGN KEYs only between tables and not views. 您只能在表之间定义外键,而不能在视图之间定义外键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM