C# 使用EntityFramework CodeFirst 创建PostgreSQL数据库

农民  金牌会员 | 2023-7-26 08:44:45 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 873|帖子 873|积分 2619

1.先创建一个ASP.Net Web应用程序,选择Web API

 2、创建EntityLib、EF、AppService三个类库。EntityLib用于存放数据库表所对应的实体,AppService用于编写用户对实体的一些操作方法,如增删改查等操作。

 创建好所有类库之后,需要添加引用库EntityFramework6.Npgsql,右击项目中的引用———》管理NuGet程序包———》搜索EntityFramework6.Npgsql添加到项目中,这个库会自动添加EntityFramework6和Npgsql的引用。需要给EF、AppService和API都添加这个引用。添加完成之后就开始写各个类库相应的代码了。
 
EFContext类
  1. public class EFContext : DbContext
  2.     {
  3.         public EFContext() : base("Postgresql")//连接字符串名称
  4.         {
  5.         }
  6.         protected override void OnModelCreating(DbModelBuilder modelBuilder)
  7.         {
  8. <connectionStrings>
  9.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  10.     </connectionStrings>modelBuilder.HasDefaultSchema("public");//EF默认创建到dbo架构中,而PostgreSQL默认为public架构
  11. <connectionStrings>
  12.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  13.     </connectionStrings>modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
  14. <connectionStrings>
  15.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  16.     </connectionStrings>base.OnModelCreating(modelBuilder);
  17.         }
  18.         public DbSet<User> Users { get; set; }
  19.     }
复制代码
 
 User类
  1. [Table("User",Schema ="public")]
  2.     public class User
  3.     {
  4.         [Key]
  5.         [Required]
  6.         [Column("ID")]
  7.         public int ID { get; set; }
  8.         public string Name { get; set; }
  9.         public string Account { get; set; }
  10.         public string Password { get; set; }
  11.     }
复制代码
 
AppService类
  1. public class UserService
  2.     {
  3.         public void AddUser(User user)
  4.         {
  5. <connectionStrings>
  6.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  7.     </connectionStrings>try
  8. <connectionStrings>
  9.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  10.     </connectionStrings>{
  11. <connectionStrings>
  12.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  13.     </connectionStrings>    using (var db = new EFContext())
  14. <connectionStrings>
  15.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  16.     </connectionStrings>    {
  17. <connectionStrings>
  18.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  19.     </connectionStrings>        db.Users.Add(user);
  20. <connectionStrings>
  21.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  22.     </connectionStrings>        db.SaveChanges();
  23. <connectionStrings>
  24.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  25.     </connectionStrings>    }
  26. <connectionStrings>
  27.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  28.     </connectionStrings>}
  29. <connectionStrings>
  30.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  31.     </connectionStrings>catch (Exception e)
  32. <connectionStrings>
  33.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  34.     </connectionStrings>{
  35. <connectionStrings>
  36.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  37.     </connectionStrings>    string msg = e.Message;
  38. <connectionStrings>
  39.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  40.     </connectionStrings>}
  41.         }
  42.         public void DeleteUserByID(int id)
  43.         {
  44. <connectionStrings>
  45.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  46.     </connectionStrings>try
  47. <connectionStrings>
  48.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  49.     </connectionStrings>{
  50. <connectionStrings>
  51.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  52.     </connectionStrings>    using (var db = new EFContext())
  53. <connectionStrings>
  54.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  55.     </connectionStrings>    {
  56. <connectionStrings>
  57.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  58.     </connectionStrings>        db.Users.Remove(db.Users.Find(id));
  59. <connectionStrings>
  60.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  61.     </connectionStrings>        db.SaveChanges();
  62. <connectionStrings>
  63.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  64.     </connectionStrings>    }
  65. <connectionStrings>
  66.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  67.     </connectionStrings>}
  68. <connectionStrings>
  69.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  70.     </connectionStrings>catch (Exception e)
  71. <connectionStrings>
  72.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  73.     </connectionStrings>{
  74. <connectionStrings>
  75.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  76.     </connectionStrings>    string msg = e.Message;
  77. <connectionStrings>
  78.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  79.     </connectionStrings>}
  80.         }
  81.         public void EditUser(User user)
  82.         {
  83. <connectionStrings>
  84.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  85.     </connectionStrings>try
  86. <connectionStrings>
  87.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  88.     </connectionStrings>{
  89. <connectionStrings>
  90.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  91.     </connectionStrings>    using (var db = new EFContext())
  92. <connectionStrings>
  93.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  94.     </connectionStrings>    {
  95. <connectionStrings>
  96.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  97.     </connectionStrings>        
  98. <connectionStrings>
  99.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  100.     </connectionStrings>        User u = db.Users.Find(user.ID);
  101. <connectionStrings>
  102.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  103.     </connectionStrings>        u.Name = user.Name;
  104. <connectionStrings>
  105.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  106.     </connectionStrings>        u.Account = user.Account;
  107. <connectionStrings>
  108.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  109.     </connectionStrings>        u.Password = user.Password;
  110. <connectionStrings>
  111.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  112.     </connectionStrings>        db.SaveChanges();
  113. <connectionStrings>
  114.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  115.     </connectionStrings>    }
  116. <connectionStrings>
  117.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  118.     </connectionStrings>}
  119. <connectionStrings>
  120.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  121.     </connectionStrings>catch (Exception e)
  122. <connectionStrings>
  123.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  124.     </connectionStrings>{
  125. <connectionStrings>
  126.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  127.     </connectionStrings>    string msg = e.Message;
  128. <connectionStrings>
  129.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  130.     </connectionStrings>}
  131.         }
  132.         public User FindUserByID(int id)
  133.         {
  134. <connectionStrings>
  135.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  136.     </connectionStrings>try
  137. <connectionStrings>
  138.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  139.     </connectionStrings>{
  140. <connectionStrings>
  141.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  142.     </connectionStrings>    using (var db = new EFContext())
  143. <connectionStrings>
  144.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  145.     </connectionStrings>    {
  146. <connectionStrings>
  147.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  148.     </connectionStrings>        User user = db.Users.Find(id);
  149. <connectionStrings>
  150.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  151.     </connectionStrings>        return user;
  152. <connectionStrings>
  153.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  154.     </connectionStrings>    }
  155. <connectionStrings>
  156.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  157.     </connectionStrings>}
  158. <connectionStrings>
  159.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  160.     </connectionStrings>catch (Exception e)
  161. <connectionStrings>
  162.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  163.     </connectionStrings>{
  164. <connectionStrings>
  165.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  166.     </connectionStrings>    string msg = e.Message;
  167. <connectionStrings>
  168.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  169.     </connectionStrings>    return null;
  170. <connectionStrings>
  171.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  172.     </connectionStrings>}
  173.         }
  174.         public List<User> FindAll()
  175.         {
  176. <connectionStrings>
  177.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  178.     </connectionStrings>try
  179. <connectionStrings>
  180.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  181.     </connectionStrings>{
  182. <connectionStrings>
  183.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  184.     </connectionStrings>    using (var db = new EFContext())
  185. <connectionStrings>
  186.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  187.     </connectionStrings>    {
  188. <connectionStrings>
  189.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  190.     </connectionStrings>        List<User> users = db.Users.ToList();
  191. <connectionStrings>
  192.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  193.     </connectionStrings>        return users;
  194. <connectionStrings>
  195.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  196.     </connectionStrings>    }
  197. <connectionStrings>
  198.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  199.     </connectionStrings>}
  200. <connectionStrings>
  201.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  202.     </connectionStrings>catch (Exception e)
  203. <connectionStrings>
  204.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  205.     </connectionStrings>{
  206. <connectionStrings>
  207.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  208.     </connectionStrings>    string msg = e.Message;
  209. <connectionStrings>
  210.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  211.     </connectionStrings>    return null;
  212. <connectionStrings>
  213.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  214.     </connectionStrings>}
  215.         }
  216.     }
复制代码
完成类的编写之后,需要添加数据库连接字符串,打开API的web.config文件添加:
  1. <connectionStrings>
  2.         <add name="Postgresql" connectionString="PORT=502;DATABASE=postgres;HOST=10.60.140.1;PASSWORD=007;USER ID=postgres" providerName="Npgsql" />
  3.     </connectionStrings>
复制代码
 3.进行数据迁移,通过命令将实体类导入数据库生成相应的数据库表。先打开VS的工具,然后点击NuGet包管理器 ,选择程序包管理器控制台,默认项目改为EF,输入以下三行命令

1-》enable-migrations  
2-》add-migration 
3-》update-database
 
去PostgreSQL数据库去查看发现已经生成好了数据库和对应的表了,这里因为我postgres数据库已经有User表了,所以修改了一下数据库连接字符串

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

农民

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表