Entity Framework Third article attribute declaration

Source: Internet
Author: User

The Entity Framework has strict requirements for attribute declarations in entities.

1. The entity must have a primary key attribute, but what if the entity does not have a primary key attribute?

   Public int Executesqlcommand (stringparams  sqlparameter[] paras)        {            if (this . istransaction)                {ifnull)                {                    DbContext.Database.BeginTransaction ();                }            }             return dbContext.Database.ExecuteSqlCommand (SQL, paras);        }

Only the implementation of comand can be used to compromise the solution, why introduce a transaction mechanism? Because we are in the process of executing a transaction, it is possible to dbset<t> the additions and deletions and command operations mixed, but in order to meet the requirements of the transaction, so the introduction of Dbcontexttransaction

2. Entity must have table name attribute, primary key attribute

[Table ("shop")]      Public class Shop    {        [Key]        [Databasegeneratedattribute (Databasegeneratedoption.none)]        public  intgetset;}          Public string Get set; }
Databasegeneratedattribute (Databasegeneratedoption.none) represents a non-self-growing primary key column

3. When the entity does not need to be mapped, declare the attribute
  [notmapped]        publicstringgetset;        [notmapped]          Public Get set; }

Note notmapped will make additions and deletions to change the statement to ignore the change field, but also the following method invalidation, that is, declaring the notmapped attribute of the field, the value of the entity is not reflected

 Public Ienumerable<t> getlist<t> (string  strSQL)        {            return Dbcontext.database.sqlquery<t>(strSQL). ToList ();        }

4. The entity type must be consistent with the data table field type

For example, the tinyint type corresponds to byte, etc.




Entity Framework Third article attribute declaration

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.