EntityFramework6(EF6)连接Postgresql数据库

EntityFramework6(EF6)连接Postgresql数据库


 

1.新建一个MVC框架的ASP.NET Web应用程序项目:

 

2.打开菜单栏工具下的扩展与更新,找到Npgsql PostgreSQL Interaction插件并安装,这样才能在server explorer中连接到postgresql数据库:

 

 

3.使用Nuget程序包管理器控制台安装连接所需要的Nuget包,输入如下代码:

  1. install-package npgsql -version 3.2.7
  2. install-Package EntityFramework6.Npgsql -Version 3.1.1

安装完后即可在解决方案管理器的引用下看到如下所添加的引用:

 

4.做完这些准备后,接下来开始连接postgresql,打开菜单栏工具下的“连接到数据库”,选择postgresql,输入所要连接的数据库名,完成数据库连接:

 

5.接下来创建一个实体数据模型,选择Models文件夹右击添加/新建项,然后选择数据栏里的ADO.NET实体数据模型:

 

选择来自数据库的EF设计器,进行如下操作:

 

 

 注:连接设置另存为的文件名同时也是后续连接所要创建的类名

模型建立完成后会在Models文件夹下生成Model1.edmx文件,如下:

    

 

6.建立好模型后,我们在建立一个模型的控制器,选择Conteoller文件夹右击添加/控制器,选择空的MVC 5控制器:

 

 

7.在Web config 里添加如下代码:

  1. <data>
  2. <DbProviderFactories>
  3. <remove invariant="Npgsql" />
  4. <add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" support="FF" />
  5. </DbProviderFactories>
  6. </data>

若没有添加则会发生如下错误:

 

8.在控制器的Index方法里编辑如下代码(以及相应的using语句):

  1. usingModels;
  2. using System;
  3. usingCollections.Generic;
  4. usingLinq;
  5. usingWeb;
  6. usingWeb.Mvc;
  7. namespace Controllers
  8. {
  9. public class TestController : Controller
  10. {
  11. // GET: Test
  12. public ActionResult Index()
  13. {
  14. Database myDatabase = new Database();
  15. //查询
  16. var list = from s instudent
  17. select s;
  18. return View(list);
  19. }
  20. }
  21. }

 

9.接着添加一个视图来显示我们查询的数据,鼠标点击View()函数右击添加视图:

 

 

10.编辑视图,在<div>里添加table,以及添加相应的引用,代码如下:

  1. @model IQueryable<Models.student>
  2. @using Test.Models
  3. @{
  4. Layout = null;
  5. }
  6. <!DOCTYPE html>
  7. <html>
  8. <head>
  9. <meta name="viewport" content="width=device-width" />
  10. <title>Index</title>
  11. </head>
  12. <body>
  13. <div>
  14. <table border="1">
  15. <tr>
  16. <th>学号</th>
  17. <th>姓名</th>
  18. </tr>
  19. @foreach (student s in Model)
  20. {
  21. <tr>
  22. <td>@s.sno</td>
  23. <td>@s.sname</td>
  24. </tr>
  25. }
  26. </table>
  27. </div>
  28. </body>
  29. </html>

11.最后将默认的控制器改成刚刚我们建立的控制器,编辑App_Start文件夹下的RouteConfig.cs文件,将controlre的参数改成Test(即控制器名),运行即可查看查询结果。

注:若出现依赖错误,可能是因为Nuget包版本问题,只需更新后即可解决

  1. MapRoute(
  2. name: "Default",
  3. url: "{controller}/{action}/{id}",
  4. defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional }
  5. );

https://blog.csdn.net/danger_z/article/details/80466350

 

 

 

博文最后更新时间:


评论

  • Merziuz

    JMX9RF http://pills2sale.com/ viagra online

  • Guillermo

    Wonderfull great site <a href="https://www.dans-hobbies.com/crestor-et-augmentation-des-gamma-gt-jbqq">crestor off patent</a> At Sharp, the situation is more dire. "Sharp has no core products other than LCD," according to Morikawa. LCD accounted for 18.5 percent of company sales in 2011. The figure had risen to 26.2 percent by 2012, with sales rising by 276.32 billion yen ($2.84 billion).

  • Mackenzie

    I'm interested in this position <a href="http://jajharkhand.in/wp/obat-hajar-jahanam-mesir-jakarta-1rp4">testimoni hajar jahanam asli</a> Other new or surging consumer issues included fake fronts called skimmers on gas pumps and automated teller machines. The skimmers collect users' financial information, which can later be used to gain access to the victim's credit or bank accounts.

  • Mya

    How would you like the money? <a href="https://museuvictormeirelles.museus.gov.br/cheapest-clomid-no-prescription-qop3">ovulation after taking clomid</a> If lip balm doesn't do the trick you should consult a doctor. "Cracks at the corners of your mouth may be a sign of a vitamin deficiency," Dr. Raj says. "Have a doctor check your levels—and consider taking a multivitamin."

  • Harley

    Which university are you at? <a href="http://magnocentrojoyero.com/blog/centre-spcialis-pour-sclrose-en-plaque-vmy1">cialis online bestellen per nachnahme</a> It&#39;s not much, but it&#39;s a start &mdash; and a darned sight better than the 18 months beforehand, which the euro zone spent languishing in the longest recession it&#39;s ever seen. Not everyone contributed equally to this little victory, however: Germany and France are doing most of the heavy lifting, while Spain, Italy, the Netherlands and others continue to struggle.

  • Jacques

    What do you do for a living? <a href="http://www.wepsolutions.co.in/avanafil-farmaco-generico-qrnf">havana club 7 anos anejo </a> A newly married man, in some difficulty with his wife, asked an astroleger when his troubles would be over. The astroleger told him to wait for seven and a half years. Anxiously the guy asked again: &#8220;Will everything be all right then?&#8221; &#8220;No, you will get used to them!&#8221; replied the astroleger. Nothing will come out of the discussion. We will be back in square one.

发表评论

博客统计

访问量:3043547

博文总数:750 评论总数:320333

原创126 翻译20 转载604