Multi-table common fields in EF, and setting EntityBase to inherit custom classes from all entity classes

Source: Internet
Author: User

When using the EF framework to access a database, if some tables have common fields, such as in the approval process, for the creator of the various requisition materials, the time of creation, the modified person, the modified time, these may be the fields required by multiple tables, if the assignment in each entity is clearly similar and repetitive, The following is an example of a uniform assignment when data is submitted (simplified, if it is not used only for recording time), recorded as follows:

1. Create a common interface iaudited, which contains the common fields, and the requisition single entity class inherits this interface.

2, define an abstract class dbentity (with the EntityBase attribute of the dbml file, so that the database entity classes inherit from this class), define the onsaving, check the entities that can be converted to iaudited, uniform assignment of common fields.

3, DB access to the database class, data transfer to dbentity before submitting data, and call onsaving to make the public field data assigned to the database.

The specific code takes a MVC3 project as an example:

1, create a new MVC3 project, the directory structure is as follows:

2. Add db.dbml, select Menu View-server Explorer, add database connection, select table in database drag and drop to open db.dbml

3, Iaudited class

 using   System;  using   System.Collections.Generic;  using   System.Linq;  using   system.web;  namespace   mvcapplication1.data{ public  interface   iaudited {DateTime Createon { get ; set          get ; set     
View Code

4, Student class

using System; using System.Collections.Generic; using System.Linq; using system.web; namespace mvcapplication1.data{    publicpartialclass  student:iaudited    {           }}
View Code

5, Dbentity class

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Data.Linq;namespacemvcapplication1.data{ Public Abstract classdbentity { Public Virtual voidonsaving (changeaction changeaction) {varAuditentity = This  asiaudited; if(Auditentity! =NULL)            {                if((changeaction = = changeaction.update) | | (Changeaction = =Changeaction.insert)) {Auditentity.lastupdateon=DateTime.Now; if(Changeaction = =changeaction.insert) {Auditentity.createon=Auditentity.lastupdateon; }                }            }        }         Public Virtual voidonsaved () {} }}
View Code

6, set the database entity class all inherit dbentity, first close db.dbml (remember must close), select Db.dbml file Right--open as--select XML format-OK

Add entitybase= "Dbentity" to the first line

When you open Db.designer.cs, you can see that all the entity classes inherit the Dbentity

8, open db.dbml in the blank right click, select Properties, modify the name of the DB, and then in the Open db.dbml blank right click-view Code, in the Db.cs write operation database code.

9. Call

Private void Add ()        {            using (var db = db.open ())            {                new  Student ();                 "001";                 " Class One " ;                Db. Students.insertonsubmit (c);                Db. SubmitChanges ();            }        }
View Code

Multiple-table public fields in EF, and setting EntityBase to inherit custom classes from all entity classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.