MVC + linqtosql repository mode (3) Repository mode for unified curd operations and find primary keys in EF

Source: Internet
Author: User

namespace data. test
{< br> ///


// Data Universal operation implementation
///
///
public class repository : testrepositorybase, irepository where tentity: Class, ientity
{< br> # region irepository member

Public void Update (tentity entity)
{< br> tentity originalentity = This. find (entity. primarykey);
emitmapper. objectmappermanager. defaultinstance. getmapper (). map (entity, originalentity);
base. submitchanges ();
}

Public void Update (ilist List)
{< br> tentity originalentity = NULL;
list. tolist (). foreach (entity =>
{< br> originalentity = This. find (entity. primarykey);
emitmapper. objectmappermanager. defaultinstance. getmapper (). map (entity, originalentity);
});
base. submitchanges ();
}

Public void insert (tentity entity)
{< br> This. _ dB. gettable (). insertonsubmit (entity);
base. submitchanges ();
}

Public void insert (ilist List)
{< br> list. tolist (). foreach (entity =>
{< br> This. _ dB. gettable (). insertonsubmit (entity);
});
base. submitchanges ();
}

Public tentity insertgetidentity (tentity entity)
{
This. _ dB. gettable <tentity> (). insertonsubmit (entity );
Base. submitchanges ();
Return this. Find (entity. primarykey );
}

Public void Delete (tentity entity)
{
This. _ dB. gettable <tentity> (). deleteonsubmit (entity );
Base. submitchanges ();
}

Public iqueryable GetModel ()
{< br> This. _ dB. refresh (system. data. LINQ. refreshmode. overwritecurrentvalues, this. dbset); // solves the problem of frequent data refresh
return this. dbset;
}

Public tentity find (Params object [] keyvalues)
{
VaR mapping = _ dB. Mapping. gettable (typeof (tentity ));
VaR keys = mapping. rowtype. identitymembers. Select (M, I) => M. Name + "= @" + I). toarray ();
Return this. dbset. Where (string. Join ("&", keys), keyvalues). firstordefault ();
}

/// <Summary>
/// Attributes of a generic data table
/// </Summary>
Private iqueryable <tentity> dbset
{

Get
{
Return this. _ dB. gettable <tentity> ();
}

}
# Endregion
}
}

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.