{"id":5805,"date":"2022-05-21T17:45:07","date_gmt":"2022-05-21T09:45:07","guid":{"rendered":"https:\/\/damogame.cn:15443\/wordpress\/?p=5805"},"modified":"2022-05-21T17:45:07","modified_gmt":"2022-05-21T09:45:07","slug":"%e4%bb%80%e4%b9%88%e6%98%afentity-framework","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/5805","title":{"rendered":"\u4ec0\u4e48\u662fEntity Framework"},"content":{"rendered":"<div class=\"article-header-box\">\n<div class=\"article-header\">\n<div class=\"article-title-box\"><\/div>\n<div class=\"article-info-box\">\n<div class=\"article-bar-top\">\n<div class=\"bar-content\"><a href=\"https:\/\/blog.csdn.net\/qq_40741855\/article\/details\/83624330\">\u539f\u6587\u5730\u5740<\/a><\/div>\n<\/div>\n<div class=\"blog-tags-box\">\n<div class=\"tags-box artic-tag-box\"><\/div>\n<\/div>\n<div class=\"operating\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n<article class=\"baidu_pl\">\n<div id=\"article_content\" class=\"article_content clearfix\">\n<div id=\"content_views\" class=\"htmledit_views\">\n<h1><a name=\"t0\"><\/a>\u4ec0\u4e48\u662fEntity Framework<\/h1>\n<p>Entity Framework\u662f\u4e00\u4e2a\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04O\/RM<a class=\"hl hl-1\" href=\"https:\/\/so.csdn.net\/so\/search?q=%E6%A1%86%E6%9E%B6&amp;spm=1001.2101.3001.7020\" target=\"_blank\" rel=\"noopener\" data-report-click=\"{&quot;spm&quot;:&quot;1001.2101.3001.7020&quot;,&quot;dest&quot;:&quot;https:\/\/so.csdn.net\/so\/search?q=%E6%A1%86%E6%9E%B6&amp;spm=1001.2101.3001.7020&quot;}\" data-tit=\"\u6846\u67b6\" data-pretit=\"\u6846\u67b6\">\u6846\u67b6<\/a>\u3002<\/p>\n<p>Entity Framework\u8ba9\u5f00\u53d1\u8005\u53ef\u4ee5\u50cf\u64cd\u4f5c\u9886\u57df\u5bf9\u8c61(domain-specific objects)\u90a3\u6837\u64cd\u4f5c\u5173\u7cfb\u578b\u6570\u636e(relational data)\u3002<\/p>\n<p>Entity Framework\u51cf\u5c11\u4e86\u5927\u90e8\u5206\u901a\u5e38\u9700\u8981\u7f16\u5199\u7684\u6570\u636e\u64cd\u4f5c\u4ee3\u7801\u3002<\/p>\n<p>Entity Framework\u4e2d\u53ef\u4ee5\u4f7f\u7528LINQ\u6765\u67e5\u8be2\u6570\u636e\uff0c\u4f7f\u7528\u5f3a\u7c7b\u578b(strongly typed objects)\u6765\u68c0\u7d22\u548c\u64cd\u4f5c\u6570\u636e\u3002<\/p>\n<p>Entity Framework\u63d0\u4f9b\u4e86\u4ee5\u4e0b\u670d\u52a1\uff0c\u4f7f\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u52a0\u4fa7\u91cd\u4e8e\u7a0b\u5e8f\u4e1a\u52a1\u903b\u8f91\uff0c\u800c\u975e\u6570\u636e\u8bbf\u95ee\u7684\u57fa\u672c\u64cd\u4f5c\u3002<\/p>\n<p>1. \u72b6\u6001\u6216\u53d8\u66f4\u8ddf\u8e2a(change tracking)<\/p>\n<p>2.\u00a0\u8eab\u4efd\u6216\u4e3b\u952e\u8bc6\u522b(identity resolution)<\/p>\n<p>3.\u00a0\u61d2\u52a0\u8f7d(lazy loading)<\/p>\n<p>4.\u00a0\u67e5\u8be2\u7ffb\u8bd1(query translation)<\/p>\n<p>Entity Framework\u662fADO.NET\u7684\u52a0\u5f3a\uff0c\u5b83\u7ed9\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u6570\u636e\u5e93\u8bbf\u95ee\u548c\u5b58\u50a8\u7684\u81ea\u52a8\u5316\u673a\u5236\u3002<\/p>\n<p>Entity Framework\u662f\u4e00\u4e2a\u5f00\u6e90\u6846\u67b6\u3002<\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t1\"><\/a>\u4ec0\u4e48\u662fO\/RM<\/h1>\n<p>O\/RM\u662f\u4e00\u79cd\u5de5\u5177\uff0c\u53ef\u4ee5\u81ea\u52a8\u5730\u628a\u9886\u57df\u5bf9\u8c61\u6570\u636e\u5b58\u50a8\u5230\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08\u5982MS SQL Server\uff09\uff0c\u800c\u4e0d\u9700\u8981\u5927\u91cf\u7684\u7f16\u7801\u3002<\/p>\n<p>O\/RM\u5305\u542b\u4e09\u4e2a\u91cd\u8981\u7684\u90e8\u5206\uff1a<\/p>\n<p>1. \u9886\u57df\u5bf9\u8c61(Domain class objects)\uff1a\u6211\u4eec\u5b9a\u4e49\u7684\u7c7b\u3002<\/p>\n<p>2. \u5173\u7cfb\u578b\u6570\u636e\u5e93\u5bf9\u8c61(Relational database objects)\uff1a\u6570\u636e\u5e93\u8868\uff0c\u89c6\u56fe\uff0c\u5b58\u50a8\u8fc7\u7a0b\u7b49\u3002<\/p>\n<p>3. \u6620\u5c04\u4fe1\u606f(Mapping information)\uff1a\u9886\u57df\u5bf9\u8c61\u4e0e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5bf9\u8c61\u4e4b\u95f4\u8f6c\u6362\u7684\u4fe1\u606f\u3002<\/p>\n<p>O\/RM\u5141\u8bb8\u5f00\u53d1\u8005\u628a\u6570\u636e\u5e93\u8bbe\u8ba1\u548c\u9886\u57df\u5bf9\u8c61\u8bbe\u8ba1\u72ec\u7acb\u5f00\uff0c\u8ba9\u7a0b\u5e8f\u66f4\u5177\u6709\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<p>\u5b83\u8fd8\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u589e\u5220\u6539\u67e5\u7684\u529f\u80fd\uff0c\u5f00\u53d1\u8005\u4e0d\u9700\u8981\u624b\u52a8\u518d\u7f16\u5199\u8fd9\u90e8\u5206\u4ee3\u7801\u3002<\/p>\n<p>\u4e00\u4e2a\u5178\u578b\u7684\u6570\u636e\u5e93\u4e0e\u5e94\u7528\u7a0b\u5e8f\u7684O\/RM\u4ea4\u4e92\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161101154818940-57204923.png\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t2\"><\/a>Entity Framework\u7684\u7ed3\u6784<\/h1>\n<p>Entity Framework\u7684\u603b\u4f53\u7ed3\u6784\u5982\u4e0b\u56fe\u6240\u793a\u3002<\/p>\n<p><img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161101155507252-1799148279.png\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<p><strong>EDM<\/strong><strong>(Entity Data Model)\uff1a<\/strong>EDM\u5305\u542b\u4e09\u4e2a\u4e3b\u8981\u7684\u90e8\u5206 &#8211; \u6982\u5ff5\u6a21\u578b(Conceptual model)\u3001\u5b58\u50a8\u6a21\u578b(Storage model)\u548c\u6620\u5c04(Mapping)\u3002<\/p>\n<p>1. Conceptual model\uff1a\u6982\u5ff5\u6a21\u578b\u5305\u542b\u4e86\u6a21\u578b\u7684\u7c7b\u5b9a\u4e49\uff0c\u4ee5\u53ca\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u3002\u6982\u5ff5\u6a21\u578b\u7684\u8bbe\u8ba1\u72ec\u7acb\u4e8e\u6570\u636e\u5e93\u8868\u8bbe\u8ba1\u3002<\/p>\n<p>2. Storage model\uff1a\u5b58\u50a8\u6a21\u578b\u662f\u6570\u636e\u5e93\u8bbe\u8ba1\u6a21\u578b\uff0c\u5305\u542b\u4e86\u6570\u636e\u5e93\u8868\uff0c\u89c6\u56fe\uff0c\u5b58\u50a8\u8fc7\u7a0b\uff0c\u4ee5\u53ca\u5b83\u4eec\u7684\u4e4b\u95f4\u7684\u5173\u7cfb\u548c\u952e\u3002<\/p>\n<p>3. Mapping\uff1a\u6620\u5c04\u5305\u542b\u4e86\u6982\u5ff5\u6a21\u578b\u6620\u5c04\u5230\u5b58\u50a8\u6a21\u578b\u7684\u76f8\u5173\u4fe1\u606f\u3002<\/p>\n<p><strong>LINQ to Entities\uff1a<\/strong>\u4e00\u79cd\u57fa\u4e8e\u5bf9\u8c61\u6a21\u578b\u7f16\u5199\u7684\u67e5\u8be2\u8bed\u8a00\uff0c\u5b83\u5c06\u8fd4\u56de\u6982\u5ff5\u6a21\u578b\u4e2d\u8bbe\u8ba1\u7684\u5b9e\u4f53\u3002<\/p>\n<p><strong>Entity SQL\uff1a<\/strong>\u53e6\u4e00\u79cd\u548cLINQ to Entities\u76f8\u4f3c\u7684\u67e5\u8be2\u8bed\u8a00\uff0c\u4f46\u662f\u5b83\u4eec\u8fd8\u662f\u6709\u4e00\u4e9b\u5dee\u5f02\u7684\uff0c\u5f00\u53d1\u8005\u8fd8\u662f\u9700\u8981\u5355\u72ec\u82b1\u65f6\u95f4\u53bb\u5b66\u4e60\u5b83\u3002<\/p>\n<p><strong>Object Service\uff1a<\/strong>\u6570\u636e\u5e93\u6570\u636e\u8bbf\u95ee\u7684\u4e3b\u8981\u5165\u53e3\uff0c\u4e3b\u8981\u804c\u8d23\u662f\u7269\u5316(materialization)\uff0c\u628aEntity Client Data Provider\u8fd4\u56de\u7684\u6570\u636e\u8f6c\u6362\u6210\u5b9e\u4f53\u5bf9\u8c61\u7ed3\u6784\u3002<\/p>\n<p><strong>\u3000\u3000Entity Client Data Provider:<\/strong>\u628aLINQ to Entities\u6216Entity SQL\u8f6c\u6362\u6210\u6570\u636e\u5e93SQL\u3002\u548cADO.Net Data Provider\u8fdb\u884c\u901a\u8baf\uff0c\u53d1\u9001\u6216\u68c0\u7d22\u6570\u636e\u5e93\u6570\u636e\u3002<\/p>\n<p><strong>\u3000\u3000ADO.Net Data Provider:<\/strong>ADO.Net Data Provider\u4f7f\u7528\u6807\u51c6\u7684ADO.Net\u548c\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t3\"><\/a>Entity Framework\u7684\u5f00\u53d1\u6a21\u5f0f<\/h1>\n<p>Entity Framework\u63d0\u4f9b\u4e86\u4e09\u79cd\u5f00\u53d1\u6a21\u5f0f\uff1a<\/p>\n<p>1.\u00a0Code First<\/p>\n<p>2.\u00a0Database First<\/p>\n<p>3.\u00a0Model First<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Code First\uff1a<\/strong><\/p>\n<p><strong>\u3000\u3000<img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161103182937690-2109727367.png\" alt=\"\" \/><\/strong><\/p>\n<p>\u5728Code First\u7684\u5f00\u53d1\u6a21\u5f0f\u4e2d\uff0c\u8981\u907f\u514d\u4f7f\u7528\u89c6\u89c9\u6a21\u578b\u8bbe\u8ba1\u5668(EDMX)\uff0c\u4e00\u822c\u662f\u5148\u7f16\u5199POCO\u7c7b\uff0c\u7136\u540e\u6839\u636e\u8fd9\u4e9b\u7c7b\u53bb\u751f\u6210\u6570\u636e\u5e93\u3002<\/p>\n<p>\u90a3\u4e9b\u9075\u5faa\u9886\u57df\u9a71\u52a8\u5f00\u53d1(DDD)\u539f\u5219\u7684\u5f00\u53d1\u8005\uff0c\u66f4\u503e\u5411\u4e8e\u4e00\u5f00\u59cb\u5148\u7f16\u5199\u81ea\u5df1\u7684\u9886\u57df\u7c7b\uff0c\u7136\u540e\u518d\u751f\u6210\u6570\u636e\u5e93\u6765\u5b9e\u73b0\u6570\u636e\u6301\u4e45\u5316\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Database First\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161103183900596-1206526235.png\" alt=\"\" \/><\/p>\n<p>\u901a\u8fc7\u5df2\u6709\u7684\u6570\u636e\u5e93\u6765\u751f\u6210EDMX(Entity Data Model)\u7684\u5f00\u53d1\u6a21\u5f0f\u5c31\u662fDatabase First\u7684\u5f00\u53d1\u6a21\u5f0f\u3002<\/p>\n<p>\u5982\u679c\u6570\u636e\u5e93\u53d8\u66f4\u4e86\uff0cEDMX(Entity Data Model)\u4e5f\u4f1a\u66f4\u65b0\u3002\u540c\u65f6\uff0cDatabase First\u4e5f\u652f\u6301\u5b58\u50a8\u8fc7\u7a0b\uff0c\u89c6\u56fe\u7b49\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Model First\uff1a<\/strong><\/p>\n<p><img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161103185124018-1547354236.png\" alt=\"\" \/><\/p>\n<p>Model First\u662fCode First\u548cDatabase First\u7684\u4e00\u79cd\u6298\u4e2d\u5f00\u53d1\u6a21\u5f0f\uff0c\u5b83\u63d0\u4f9b\u89c6\u89c9\u6a21\u578b\u8bbe\u8ba1\u5668(EDMX)\u6765\u8bbe\u8ba1\u6570\u636e\u6a21\u578b\uff0c\u7136\u540e\u6839\u636e\u6570\u636e\u5e93\u6a21\u578b\u6765\u751f\u6210\u6570\u636e\u5e93\u4ee5\u53ca\u9886\u57df\u7c7b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u603b\u7ed3\uff1a<\/p>\n<p>1.\u00a0Code First\u662f\u5148\u7f16\u5199\u9886\u57df\u7c7b\uff0c\u7136\u540e\u6839\u636e\u7c7b\u6765\u751f\u6210\u6570\u636e\u5e93\uff0c\u65e0\u89c6\u89c9\u6a21\u578b\u8bbe\u8ba1\u5668(EDMX)\u3002<\/p>\n<p>2. Database First\u662f\u6839\u636e\u6570\u636e\u5e93\u751f\u6210\u89c6\u89c9\u6a21\u578b\u8bbe\u8ba1\u5668(EDMX)\u53ca\u9886\u57df\u7c7b\u3002<\/p>\n<p>3. Model First\u662f\u5148\u751f\u6210\u89c6\u89c9\u6a21\u578b\u8bbe\u8ba1\u5668(EDMX)\uff0c\u7136\u540e\u6839\u636eEDMX\u751f\u6210\u6570\u636e\u5e93\u53ca\u9886\u57df\u7c7b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t4\"><\/a>\u9009\u62e9Entity Framework\u5f00\u53d1\u6a21\u5f0f<\/h1>\n<p><img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161103190609346-1040401896.png\" alt=\"\" \/><\/p>\n<p>1. \u5982\u679c\u4f60\u6709\u4e00\u4e2a\u73b0\u6210\u7684\u7a0b\u5e8f\uff0c\u5e76\u4e14\u5df2\u7ecf\u5b9a\u4e49\u4e86\u9886\u57df\u7c7b\uff0c\u90a3\u4e48\uff0c\u53ef\u4ee5\u4f7f\u7528Code First\u7684\u5f00\u53d1\u6a21\u5f0f\u6765\u751f\u6210\u6570\u636e\u5e93\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<p>2. \u5982\u679c\u4f60\u6709\u5df2\u4e2a\u73b0\u6210\u7684\u6570\u636e\u5e93\uff0c\u90a3\u4e48\uff0c\u53ef\u4ee5\u4f7f\u7528Database First\u7684\u5f00\u53d1\u6a21\u5f0f\u6765\u751f\u6210EDM\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<p>3. \u5982\u679c\u4f60\u6ca1\u6709\u73b0\u6210\u7684\u6570\u636e\u5e93\uff0c\u4e5f\u6ca1\u6709\u5b9a\u4e49\u597d\u7684\u9886\u57df\u7c7b\uff0c\u800c\u4f60\u66f4\u503e\u5411\u4e8e\u4f7f\u7528\u56fe\u5f62\u5316\u754c\u9762\u6765\u8bbe\u8ba1\u6570\u636e\u5e93\u6a21\u578b\uff0c\u90a3\u4e48\uff0c\u53ef\u4ee5\u4f7f\u7528Model First\u7684\u5f00\u53d1\u6a21\u5f0f\u8fdb\u884c\u5f00\u53d1\u3002<\/p>\n<p>\u6211\u4e2a\u4eba\u7684\u8bdd\uff0c\u4efb\u4f55\u60c5\u666f\u90fd\u4f1a\u4f7f\u7528Code First\u7684\u5f00\u53d1\u6a21\u5f0f\uff0c\u56e0\u4e3a\u5b83\u6bd4\u8f83\u7075\u6d3b\uff0c\u4f46\u662f\u5bf9\u5f00\u53d1\u8005\u672c\u8eab\u7684\u8981\u6c42\u4f1a\u66f4\u9ad8\u4e00\u4e9b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t5\"><\/a>DbContext<\/h1>\n<p><img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161103191427143-1489629184.png\" alt=\"\" \/><\/p>\n<p>DbContext\u662fEntity Framework\u7684\u4e00\u4e2a\u91cd\u8981\u90e8\u5206\uff0c\u5b83\u662f\u9886\u57df\u6216\u5b9e\u4f53\u7c7b\u4e0e\u6570\u636e\u5e93\u4e4b\u95f4\u7684\u6865\u6881\u3002<\/p>\n<p>DbContext\u662f\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u7c7b\uff0c\u4e3b\u8981\u804c\u8d23\u662f\u4ee5\u5bf9\u8c61\u7684\u65b9\u5f0f\u548c\u6570\u636e\u8fdb\u884c\u4ea4\u4e92\uff0c\u5b83\u5305\u542b\u4ee5\u4e0b\u6d3b\u52a8\uff1a<\/p>\n<p><strong>EntitySet\uff1a<\/strong>DbContext\u5305\u542b\u5b9e\u4f53\u96c6\u5408\uff08DbSet&lt;TEntity&gt;\uff09\uff0c\u628a\u5b9e\u4f53\u6620\u5c04\u5230\u6570\u636e\u5e93\u8868\u3002<\/p>\n<p><strong>\u3000\u3000Querying\uff1a<\/strong>DbContext\u628aLINQ to Entities\u67e5\u8be2\u8f6c\u6362\u6210SQL\u67e5\u8be2\uff0c\u5e76\u53d1\u9001\u5230\u6570\u636e\u5e93\u3002<\/p>\n<p><strong>\u3000\u3000Change Tracking\uff1a<\/strong>DbContext\u4f1a\u8ddf\u8e2a\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u51fa\u6765\u7684\u5b9e\u4f53\u7684\u72b6\u6001\u53d8\u66f4\u3002<\/p>\n<p><strong>\u3000\u3000Persisting Data\uff1a<\/strong>DbContext\u6839\u636e\u5b9e\u4f53\u7684\u72b6\u6001\u63d0\u4f9b\u63d2\u5165\uff0c\u66f4\u65b0\u548c\u5220\u9664\u7b49\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u3000\u3000Caching\uff1a<\/strong>DbContext\u9ed8\u8ba4\u5b9e\u73b0\u4e00\u7ea7\u7f13\u5b58\uff0c\u5728Context\u7c7b\u7684\u751f\u547d\u5468\u671f\u671f\u95f4\uff0c\u5b83\u4f1a\u4fdd\u5b58\u68c0\u7d22\u51fa\u6765\u7684\u5b9e\u4f53\u3002<\/p>\n<p><strong>\u3000\u3000Manage Relationship\uff1a<\/strong>Database First\u6216Model First\u4e2d\uff0c\u4f7f\u7528CSDL\uff0cMSL\uff0cSSDL\u6765\u7ba1\u7406\u5173\u7cfb\uff0c\u5728Code First\u4e2d\u4f7f\u7528Fluent API\u6765\u7ba1\u7406\u5173\u7cfb\u3002<\/p>\n<p><strong>\u3000\u3000Object Materialization\uff1a<\/strong>DbContext\u628a\u8868\u539f\u59cb\u6570\u636e\u8f6c\u6362\u6210\u5b9e\u4f53\u5bf9\u8c61\u3002<\/p>\n<p><strong>\u3000\u3000<\/strong><\/p>\n<h1><a name=\"t6\"><\/a>\u5b9e\u4f53\u751f\u547d\u5468\u671f<\/h1>\n<p>\u5728\u5b9e\u4f53\u7684\u751f\u547d\u671f\u4e2d\uff0c\u6bcf\u4e2a\u5b9e\u4f53\u90fd\u6709\u4e00\u4e2a\u57fa\u4e8e\u4e0a\u4e0b\u6587(DbContext)\u7684\u64cd\u4f5c\u7684\u5b9e\u4f53\u72b6\u6001\u3002<\/p>\n<p>\u5b9e\u4f53\u72b6\u6001\u662f\u4e00\u4e2aSystem.Data.Entity.EntityState\u7c7b\u578b\u7684\u679a\u4e3e\uff0c\u5b83\u5305\u542b\u4ee5\u4e0b\u7684\u503c\uff1a<\/p>\n<p>Added\uff1a\u5b9e\u4f53\u6b63\u5728\u88ab\u4e0a\u4e0b\u6587\u8ddf\u8e2a\uff0c\u4f46\u8fd8\u4e0d\u5b58\u5728\u4e8e\u6570\u636e\u5e93\u4e2d\u3002<\/p>\n<p>Deleted\uff1a\u5b9e\u4f53\u6b63\u5728\u88ab\u4e0a\u4e0b\u6587\u8ddf\u8e2a\u5e76\u5b58\u5728\u4e8e\u6570\u636e\u5e93\uff0c\u4f46\u88ab\u6807\u8bb0\u4e3a\u4ece\u6570\u636e\u5e93\u5220\u9664\u3002<\/p>\n<p>Modified\uff1a\u5b9e\u4f53\u6b63\u5728\u88ab\u4e0a\u4e0b\u6587\u8ddf\u8e2a\u5e76\u5b58\u5728\u4e8e\u6570\u636e\u5e93\uff0c\u800c\u4e14\u5b9e\u4f53\u7684\u4e00\u4e9b\u6216\u6240\u6709\u5c5e\u6027\u7684\u503c\u88ab\u4fee\u6539\u4e86\u3002<\/p>\n<p>Unchanged\uff1a\u5b9e\u4f53\u6b63\u5728\u88ab\u4e0a\u4e0b\u6587\u8ddf\u8e2a\u5e76\u5b58\u5728\u4e8e\u6570\u636e\u5e93\uff0c\u4f46\u5b9e\u4f53\u7684\u6240\u6709\u5c5e\u6027\u503c\u90fd\u6ca1\u88ab\u4fee\u6539\u3002<\/p>\n<p>Detached\uff1a\u5b9e\u4f53\u4e0d\u88ab\u4e0a\u4e0b\u6587\u8ddf\u8e2a\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e0b\u56fe\u8bf4\u660e\u4e86\u5b9e\u4f53\u72b6\u6001\u5982\u4f55\u5f71\u54cd\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<p><img decoding=\"async\" class=\"has\" src=\"https:\/\/images2015.cnblogs.com\/blog\/182190\/201611\/182190-20161104065027377-1404364903.png\" alt=\"\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>1. \u65b0\u7684\u5b9e\u4f53\u5177\u6709Added\u7684\u72b6\u6001\uff0cDbContext\u540e\u7eed\u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u63d2\u5165\u64cd\u4f5c\u3002<\/p>\n<p>2. \u901a\u8fc7LINQ\u68c0\u7d22\u51fa\u6765\u7684\u5b9e\u4f53\u5177\u6709Unchanged\u7684\u72b6\u6001\uff0c\u4f46\u5982\u679c\u8c03\u7528\u4e86AsNoTracking()\u65b9\u6cd5\uff0c\u5176\u72b6\u6001\u4e3aDetached\u3002<\/p>\n<p>3. \u4fee\u6539\u4e86\u68c0\u7d22\u51fa\u6765\u7684\u5b9e\u4f53\u7684\u5c5e\u6027\u503c\uff0c\u5b9e\u4f53\u4f1a\u4fee\u6539\u72b6\u6001\u4e3aModified\uff0cDbContext\u540e\u7eed\u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n<p>4. \u9700\u8981\u5220\u9664\u7684\u5b9e\u4f53\u4f1a\u5177\u6709Deleted\u7684\u72b6\u6001\uff0cDbContext\u540e\u7eed\u4f1a\u5728\u6570\u636e\u5e93\u4e2d\u6267\u884c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<p>5. \u5bf9\u4e8eDbContext\u4e2d\u5df2\u6709\u7684\u5b9e\u4f53\uff0c\u53ef\u4ee5\u901a\u8fc7dbContext.Entry(entity).State = EntityState.Detached\u7684\u65b9\u5f0f\u628a\u72b6\u6001\u8bbe\u7f6e\u4e3aDetached\u3002<\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t7\"><\/a>Entity Framework\u7248\u672c<\/h1>\n<div class=\"table-box\">\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td><strong>\u7248\u672c<\/strong><\/td>\n<td><strong>\u5f15\u5165\u529f\u80fd<\/strong><\/td>\n<\/tr>\n<tr>\n<td>EF 3.5<\/td>\n<td>Database First\u6a21\u5f0f\u4e0b\u57fa\u672c\u7684O\/RM\u652f\u6301\u3002<\/td>\n<\/tr>\n<tr>\n<td>EF 4.0<\/td>\n<td>POCO\u7684\u652f\u6301, \u61d2\u52a0\u8f7d, \u53ef\u6d4b\u8bd5\u6027\u63d0\u5347\uff0c\u5b9a\u5236\u5316\u4ee3\u7801\u751f\u6210\uff0c\u4ee5\u53ca\u5f15\u5165Model First\u5f00\u53d1\u6a21\u5f0f\u3002<\/td>\n<\/tr>\n<tr>\n<td>EF 4.1<\/td>\n<td>\u5728ObjectContext\u7684\u57fa\u7840\u4e0a\u7b80\u5316\u4e86DBContext API\uff0c\u5f15\u5165Code First\u5f00\u53d1\u6a21\u5f0f\u3002<\/td>\n<\/tr>\n<tr>\n<td>EF 4.3<\/td>\n<td>\u5f15\u5165Code First Migrations\uff0c\u53ef\u4ee5\u6839\u636e\u5b9a\u4e49\u7684Code First\u6a21\u578b\u6765\u521b\u5efa\u6216\u4fee\u6539\u6570\u636e\u5e93\u3002<\/td>\n<\/tr>\n<tr>\n<td>EF 5.0<\/td>\n<td>\u5ba3\u5e03EF\u4e3a\u5f00\u6e90\u9879\u76ee\u3002\u5f15\u5165\u4e86\u679a\u4e3e\u652f\u6301\uff0c\u8868\u503c\u51fd\u6570\uff0c \u7a7a\u95f4\u6570\u636e\u7c7b\u578b\uff0c\u6a21\u578b\u591a\u56fe\u8868\uff0c\u8bbe\u8ba1\u754c\u9762\u7740\u8272\u5f62\u72b6\uff0c\u6279\u91cf\u5bfc\u5165\u5b58\u50a8\u8fc7\u7a0b\uff0cEF Power Tools\uff0c\u4ee5\u53ca\u5404\u79cd\u6027\u80fd\u63d0\u5347\u3002<\/td>\n<\/tr>\n<tr>\n<td>EF 6.0<\/td>\n<td>\u5f15\u5165\u4e86\u8bb8\u591aCode First &amp; EF\u8bbe\u8ba1\u76f8\u5173\u7684\u65b0\u529f\u80fd\uff0c\u5982\u5f02\u6b65\u64cd\u4f5c(asynchronous)\uff0c\u5f39\u6027\u8fde\u63a5(connection resiliency)\uff0c\u4f9d\u8d56\u89e3\u6790(dependency resolution)\u7b49\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p>\u6ce8\uff1aEntity Framework Core\u4e0d\u5728\u672c\u6587\u8ba8\u8bba\u8303\u56f4\u3002<\/p>\n<p>&nbsp;<\/p>\n<h1><a name=\"t8\"><\/a>\u53c2\u8003\u8d44\u6599<\/h1>\n<p>\u672c\u6587\u5927\u90e8\u5206\u5185\u5bb9\u6765\u81ea\uff1a<a href=\"http:\/\/www.entityframeworktutorial.net\/\" target=\"_blank\" rel=\"noopener\">Entity Framework Tutorial<\/a><\/p>\n<p>\u5c11\u91cf\u5185\u5bb9\u53c2\u8003\uff1a<a href=\"https:\/\/www.asp.net\/entity-framework\" target=\"_blank\" rel=\"noopener\">Entity Framework\u5b98\u7f51<\/a><\/p>\n<\/div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/article>\n","protected":false},"excerpt":{"rendered":"<p>\u539f\u6587\u5730\u5740 \u4ec0\u4e48\u662fEntity Frame<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[24],"tags":[121],"class_list":["post-5805","post","type-post","status-publish","format-standard","hentry","category-value_docs","tag-05-"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=5805"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/5805\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=5805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=5805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=5805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}