Sie sind auf Seite 1von 9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

Set Identity and Computed Properties in Entity Framework Without Triggers


Posted by dotConnect Team on August 9th, 2011

This article deals with the following: StoreGeneratedPattern Identity Computed DefaultValue InsertNullBehaviour Example Conclusion

StoreGeneratedPattern
Entity Framework contains an excellent functionality of server-generated columns of the entity that is intended for defining the primary key values on the database side, concurrency check and so on. This is done by setting StoreGeneratedPattern of columns of the EF-model storage part (SSDL) to either Identity or Computed.

Identity
In a number of databases (Microsoft SQL Server, MySQL, PostgreSQL, SQLite), StoreGeneratedPattern.Identity of the columns primary key is defined by the EF-designer when a model is created against the database and if these columns are made auto-increment in the database. Oracle does not have any standard way of making a column auto-increment, thus, in the database, the users have to create a sequence and the BEFORE INSERT trigger that defines the value of an auto-increment column based on the sequence; when the model is created, the users have to set the value of StoreGeneratedPattern manually in the model editor.

Computed
In most cases, supporting this functionality involves writing UPDATE-triggers in the database, which takes time and requires knowledge of a specific programming language other than SQL, for example, PL/SQL in
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 1/9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

Oracle.

DefaultValue
To facilitate and speed up the process of development as well as to increase its flexibility, we have provided the users of our EF-providers for Oracle, MySQL, PostgreSQL and SQLite with a capability that, in simple cases, obviates the need to write INSERT and UPDATE triggers and to set a default value for columns in the database. This can also be useful when you need, if possible, to use existing objects rather than modify the database. Now, you can assign values to columns by specifying database functions in the columns DefaultValue attribute in the storage part of the EF model (SSDL). If you use Entity Data Model Designer, you will need to edit the models XML using an XML editor. Bear in mind that the DefaultValue standard attribute is checked by EF-validation and that is why it cannot be used to call a DB-function. For that reason, we have introduced the new custom devart:DefaultValue SSDL attribute (if you need to edit manually the EDMX file, do not forget to add the attribute xmlns:devart=http://devart.com/schemas/edml/StorageSchemaExtensions/1.0 to the tag Schema in SSDL). The devart:DefaultValue attribute does not trigger the type consistency check, so you can use a wider range of default values, like CURRENT_TIMESTAMP or MY_SEQUENCE.NEXTVAL. If you use Devart Entity Developer, you will be able to set the value for DefaultValue in the designer, and it will store this value correctly either in the DefaultValue attribute or in devart:DefaultValue , as required.

InsertNullBehaviour
To use this functionality, you need to configure the EF-provider. For more information on the specifics of configuration, see New Features of Entity Framework Support in dotConnect Providers; for the purpose of the present article we shall review a small example that shows how to customize the DefaultValue processing behavior. In this example, we deal with the dotConnect for Oracle provider, but the procedure is identical for MySQL, PostgreSQL, and SQLite. v a rc o n f i g=O r a c l e E n t i t y P r o v i d e r C o n f i g . I n s t a n c e ;

c o n f i g . D m l O p t i o n s . I n s e r t N u l l B e h a v i o u r=I n s e r t N u l l B e h a v i o u r . I n s e r t D e f a / /o r c o n f i g . D m l O p t i o n s . I n s e r t N u l l B e h a v i o u r=I n s e r t N u l l B e h a v i o u r . I n s e r t D e f a New features are configured in the <ProviderName>EntityProviderConfig class. This class is available in the Devart.Data.<ProviderName>.Entity assembly. Dont forget to add this Devart.Data. <ProviderName>.Entity.dll assembly as Reference for the corresponding Entity Framework version (support for the new functionality is available for EF v1 and v4.x). For more information on capabilities of InsertNullBehaviour, see here.

Example
As an example, we shall use a table in Oracle, in which we want to define the behavior of the store generated columns in the model rather than create triggers. For the purpose of this example, we also suppose that we already have a sequence (MY_SEQUENCE) and a database function (MY_FUNCTION) that we can now use. Lets demonstrate all the capabilities within a single example.
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 2/9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

Below is the script for creating the table: C R E A T ET A B L E" P r o d u c t s "( I DN U M B E R ( 9 )P R I M A R YK E Y , " P r o d u c t N a m e "V A R C H A R 2 ( 1 6 0 )N O TN U L L , " P r i c e "N U M B E R ( 1 0 ,2 ) , " F u n c t i o n G e n e r a t e d V a l u e "N U M B E RN O TN U L L , " M o d i f i e d B y "V A R C H A R 2 ( 3 0 )N O TN U L L , " L a s t M o d i f i e d "T I M E S T A M PN O TN U L L ) XML-representation of the entity in the storage model (SSDL):

< E n t i t y T y p eN a m e = " P r o d u c t s " > < K e y > < P r o p e r t y R e fN a m e = " I D "/ > < / K e y > < P r o p e r t yN a m e = " I D "T y p e = " i n t "N u l l a b l e = " f a l s e "/ > < P r o p e r t yN a m e = " P r o d u c t N a m e "T y p e = " V A R C H A R 2 "N u l l a b l e = " f a l s e "M a x L e n < P r o p e r t yN a m e = " P r i c e "T y p e = " d o u b l e "/ > < P r o p e r t yN a m e = " F u n c t i o n G e n e r a t e d V a l u e "T y p e = " d e c i m a l "N u l l a b l e = " f a l < P r o p e r t yN a m e = " M o d i f i e d B y "T y p e = " V A R C H A R 2 "N u l l a b l e = " f a l s e "M a x L e n g < P r o p e r t yN a m e = " L a s t M o d i f i e d "T y p e = " T I M E S T A M P "N u l l a b l e = " f a l s e "/ > < / E n t i t y T y p e > Now we shall modify mapping by defining the following rules: The ID column, which is the primary key, will be defined through the value of the sequence MY_SEQUENCE on INSERT. The FunctionGeneratedValue column will be defined through the call of the user-defined function MY_FUNCTION() on INSERT. The ModifiedBy will be defined through the system database function USER on INSERT and UPDATE. The LastModified column will be defined through the system database function CURRENT_TIMESTAMP on INSERT and UPDATE. This column will be used for concurrency check, so we need to set ConcurrencyMode=Fixed for this column in the conceptual model. XML SSDL after modification is shown below:

< E n t i t y T y p eN a m e = " P r o d u c t s " > < K e y > < P r o p e r t y R e fN a m e = " I D "/ > < / K e y > < P r o p e r t yN a m e = " I D "T y p e = " i n t "N u l l a b l e = " f a l s e "S t o r e G e n e r a t e d P a t t e r < P r o p e r t yN a m e = " P r o d u c t N a m e "T y p e = " V A R C H A R 2 "N u l l a b l e = " f a l s e "M a x L e n < P r o p e r t yN a m e = " P r i c e "T y p e = " d o u b l e "/ > < P r o p e r t yN a m e = " F u n c t i o n G e n e r a t e d V a l u e "T y p e = " d e c i m a l "N u l l a b l e = " f a l < P r o p e r t yN a m e = " M o d i f i e d B y "T y p e = " V A R C H A R 2 "N u l l a b l e = " f a l s e "M a x L e n g < P r o p e r t yN a m e = " L a s t M o d i f i e d "T y p e = " T I M E S T A M P "N u l l a b l e = " f a l s e "S t o r < / E n t i t y T y p e >
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 3/9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

Below shown is an example of C# code:


2

Tweet

/ /H e r ew et u r no nt h em o n i t o r i n go fi n t e r a c t i o nw i t ht h ed a t a b a s et h r v a rm o n=n e wD e v a r t . D a t a . O r a c l e . O r a c l e M o n i t o r{I s A c t i v e=t r u e} ;

/ /2H e r ew ec o n f i g u r et h eE F p r o v i d e r ,s ot h a tt h ed e f a u l tv a l u e sd e f i n v a rc o n f i g=O r a c l e E n t i t y P r o v i d e r C o n f i g . I n s t a n c e ; c o n f i g . D m l O p t i o n s . I n s e r t N u l l B e h a v i o u r=I n s e r t N u l l B e h a v i o u r . I n s e r t D e f a

/ /H e r ew ec r e a t et h ec o n t e x ta n dp e r f o r mi n s e r ta n du p d a t eo ft h ee n t u s i n g( v a rc t x=n e wE n t i t i e s ( ) ){ P r o d u c t sp r o d u c t=n e wP r o d u c t s ( ){ P r o d u c t N a m e=" G a d g e t " , P r i c e=2 2 0 . 2 5 } ; c t x . P r o d u c t s . A d d O b j e c t ( p r o d u c t ) ; c t x . S a v e C h a n g e s ( ) ;/ /I N S E R T p r o d u c t . P r i c e=1 9 9 . 9 9 ; c t x . S a v e C h a n g e s ( ) ;/ /U P D A T E

To monitor queries sent to the server, we recommend that you use Devart dbMonitor, a free tool, that is compatible with Devart EF-providers. However, the use of this monitor is recommended only on the stage of development and debugging of the application, since its use causes a drop in performance. Shown below is SQL generated on INSERT:

D E C L A R E u p d a t e d R o w i dR O W I D ; B E G I N I N S E R TI N T O" P r o d u c t s " ( I D ," F u n c t i o n G e n e r a t e d V a l u e " ," M o d i f i e d B y " , V A L U E S( M Y _ S E Q U E N C E . N E X T V A L ,M Y _ F U N C T I O N ( ) ,U S E R ,C U R R E N T _ T I M E S T A M P R E T U R N I N GR O W I DI N T Ou p d a t e d R o w i d ; O P E N: o u t P a r a m e t e rF O RS E L E C TI D ," F u n c t i o n G e n e r a t e d V a l u e " ," M o d i f i e d B E N D ; Shown below is SQL generated on UPDATE:

D E C L A R E u p d a t e d R o w i dR O W I D ; B E G I N U P D A T E" P r o d u c t s " S E T" M o d i f i e d B y "=U S E R ," L a s t M o d i f i e d "=C U R R E N T _ T I M E S T A M P ," P r i c e W H E R EI D=: p 1A N D" L a s t M o d i f i e d "=: p 2 R E T U R N I N GR O W I DI N T Ou p d a t e d R o w i d ; O P E N: o u t P a r a m e t e rF O RS E L E C T" M o d i f i e d B y " ," L a s t M o d i f i e d "F R O M" P r o d u
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 4/9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

E N D ;

Conclusion
As we have seen, this functionality is extremely simple to use. If you, as yet, do not use Entity Developer, whose EF-version is shipped with our EF-providers, consider doing so, since it allows modifying the storage model much more easily and more conveniently than a standard EDM designer does and hence is a more suitable tool for this purpose. Nor should you forget to monitor SQL being sent on the development stage, so that you can be sure that everything works as intended. See also Oracle 12c Support and More New Features in New Versions of Devart Products Model-Defined Functions in Entity Developer Enhanced Entity Framework Spatials support for Oracle, MySQL and PostgreSQL Entity Framework 6 Support for Oracle, MySQL, PostgreSQL, SQLite, DB2 and Salesforce New Features in Entity Developer 5.0

2 Responses to Set Identity and Computed Properties in Entity Framework Without Triggers
1. Max1ce Says:
November 15th, 2011 at 5:30 pm

How would you use the DefaultValue in a code-first approach without using the designer? 2. Shalex Says:
November 17th, 2011 at 1:25 pm

Microsoft does not allow setting default value in the current version of EF Code-First fluent mapping. There are several suggestions about the default value support at official Entity Framework UserVoice: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-featuresuggestions/suggestions/1050341-allow-setting-default-values-from-model?ref=title http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-featuresuggestions/suggestions/1501715-more-options-in-code-first-db-generation?ref=title Search for:
Search

Teams ALM Team DAC Team dbForge Team dotConnect Team Subscribe by e-mail Enter your email address:
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 5/9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

Subscribe

Tags
ADO.NET Android development

C++Builder Code-First Migrations code comparison Code First code

review command line Continuous Integration database designer database development database

diagram database project data compare data import Delphi Delphi XE2 Delphi XE5 EF

Entity Framework Full-Text Search how to Intellisense large databases LINQ LINQ to SQL MySQL MySQL GUI client NHibernate Oracle Performance PostgreSQL query builder
Tips and Tricks
development iOS Many-to-Many Mapping MSBuild

Rad Studio RAD Studio XE2 RAD Studio XE5 Review Assistant schema compare
SQLite SQL

Server synchronize database Whats New in Review Assistant 2.0

Devart Blog / Teams / dotConnect Team / Set Identity and Computed Properties in Entity Framework Without Triggers Devart Blog

Products Downloads Purchase Support Blog Company Products Database Tools SQL Server Tools MySQL Tools Oracle Tools PostgreSQL Tools .NET Database Connectivity ADO.NET Data Providers ORM Solutions Developer Tools Coding Assistance Delphi Database Connectivity Delphi Data Access Components
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 6/9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

dbExpress Drivers Support Submit Request View Forums Documentation Center Ordering FAQs High Five Program Events Company About Us News Customers Partners Resellers Contacts Contact Form Social Facebook Twitter Google+ Linkedin Copyright 1998 - 2014 Devart. All rights reserved. Products Forums Support Company Site Map SQL Server Tools dbForge Studio for SQL Server Updated! SQL Complete Updated! Compare Bundle for SQL Server Schema Compare for SQL Server Data Compare for SQL Server Query Builder for SQL Server SQL Azure Backup SQL Decryptor dbForge Fusion for SQL Server New! MySQL Tools dbForge Studio for MySQL Updated! Compare Bundle for MySQL Schema Compare for MySQL Updated! Data Compare for MySQL Updated! Query Builder for MySQL Updated!
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 7/9

YouTube

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

dbForge Fusion for MySQL Oracle Tools dbForge Studio for Oracle Updated! Compare Bundle for Oracle Schema Compare for Oracle Updated! Data Compare for Oracle Updated! dbForge Fusion for Oracle Updated! PostgreSQL Tools Data Compare for PostgreSQL Developer Tools Review Assistant Updated! Code Compare Updated! LINQ Insight Updated! T4 Editor Updated! SecureBridge dbMonitor ADO.NET Data Providers dotConnect for Oracle dotConnect for MySQL dotConnect for PostgreSQL dotConnect for SQLite dotConnect for Salesforce Updated! dotConnect for DB2 New! dotConnect for SQL Server dotConnect Universal ORM Solutions LinqConnect Entity Developer Updated! Delphi Data Access Components EntityDAC Beta! UniDAC ODAC SDAC MyDAC IBDAC PgDAC LiteDAC VirtualTable dbExpress Drivers
http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html 8/9

28/3/2014

Set Identity and Computed Properties in Entity Framework Without Triggers

Oracle driver SQL Server driver MySQL driver Interbase/Firebird driver PostgreSQL driver SQLite driver Forums Request Support Documentation Center Ordering FAQs About Us News Customers Partners Resellers Contacts Contact Form

http://blog.devart.com/set-identity-and-computed-properties-in-entity-framework-without-triggers.html

9/9

Das könnte Ihnen auch gefallen