加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 有哪些好方法可以存储大量的对象,这些对象会被持久修改/搜

发布时间:2020-12-15 23:33:07 所属栏目:百科 来源:网络整理
导读:所以我为了学习而在C#中编写某种银行软件.我有不同的课程 class Client { int userID; string firstName; string lastName; string description; DateTime birthdate; string type; ....}class Account { int accountID; int userID; string type; DateTime
所以我为了学习而在C#中编写某种银行软件.我有不同的课程

class Client {
    int userID;
    string firstName;
    string lastName;
    string description;
    DateTime birthdate;
    string type;
    ....
}

class Account {
    int accountID;
    int userID;
    string type;
    DateTime runtime;
    DateTime opened;
    string description;
    ....
}

现在我想在图形界面(WinForms)中显示所有不同的数据,我认为DataGridView是正确的选项,并构建表单,以便用户可以添加新的客户端和帐户以及修改或删除它们.

我的问题是我不确定如何保存我的对象及其数据,所以它在退出和重新启动程序后仍然可用,而不会失去在基于对象的基础上处理数据的能力(比如使用setter方法修改数据) ).如果它可以在不设置大量内容的情况下轻松发货,那将是很好的,因此我可以将我的应用程序发送给某人进行代码审查.有点像你可以在Visual Studio中设置本地数据库

也许我只是困惑自己,我在想错误的方向,只是让我知道.谢谢你的帮助.

解决方法

在我看来,实体框架是一个更好的选择.它为Windows应用程序提供了功能齐全的持久层

https://msdn.microsoft.com/en-gb/data/ef.aspx

边注

EF目前处于6.1版本,因此它是一个成熟的产品,并且微软建议在新的Windows应用程序中创建持久层.实体框架核心(将取代6.1)目前是RC2,所以不久之后,EF也将可用于Linux和MAC系统.

如何安装

右键单击您的项目,单击“管理NuGet包…”,然后在搜索栏中键入“实体框架”.安装它,嘿嘿,你的应用程序(几乎)已准备好与Entity Framework持久层一起使用.

示例代码

public class Client {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Description { get; set; }
    public DateTime Birthday { get; set; }
    public string Type { get; set; }
    ...
}

public class Account {
    public int Id { get; set; }
    public Client Client { get; set; }
    public string Type { get; set; }
    public DateTime RunTime { get; set; }
    public DateTime Opened { get; set; }
    public string Description { get; set; }
    ...
}

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext() : base("name=DefaultConnection")
    {
    }

    public DbSet<Account> Accounts { get; set; }
    public DbSet<Client> Clients { get; set; }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读