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