C#------EntityFramework實體載入資料庫

來源:互聯網
上載者:User

標籤:

一.SQLServer資料庫建立表Company,包含ID,CName,IsEnabled三列

二.(1)VS建立一個DXApplication工程,名為CompanyManageSystem

    (2)在工程下,右鍵點擊“引用” -> "管理NuGet程式包" -> "瀏覽" -> 找到"EntityFramework" -> 點擊“安裝”

    (3)建立一個類PSContext.cs

namespace CompanyManageSystem{    class PSContext : DbContext    {        public PSContext() : base("name=PSContext"){}        public virtual DbSet<Company> Companys { get; set; }        protected override void OnModelCreating(DbModelBuilder modelBuilder)        {        }    }}

     (4)建立一個類Company.cs

namespace CompanyManageSystem.Model{    [Table("Company")]    public class Company    {        public Guid ID { get; set; }        public String CName { get; set; }        public bool IsEnabled { get; set; }    }}

      (5)在App.config添加資料庫的代碼

<?xml version="1.0" encoding="utf-8"?><configuration>  <connectionStrings>    <add name="PSContext" connectionString="data source=X450V-PC;initial catalog=date;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />  </connectionStrings></configuration>

      (6)添加表單Frm_ShowInfo.cs    (注意:FiledName裡面的名字要和資料庫的列名相同,(Name)裡面的名字盡量不取Name)

添加控制項:

 

namespace CompanyManageSystem{    public partial class Frm_ShowInfo : Form    {        PSContext _db = new PSContext();        public Frm_ShowInfo()        {            InitializeComponent();        }        private void Frm_ShowInfo_Load(object sender, EventArgs e)        {            CompanyGrid.DataSource = _db.Companys.AsNoTracking().ToList();        }    }}

         (7)在Program.cs啟動Frm_ShowInfo.cs視窗

namespace CompanyManageSystem{    static class Program    {        /// <summary>        /// The main entry point for the application.        /// </summary>        [STAThread]        static void Main()        {            Application.EnableVisualStyles();            Application.SetCompatibleTextRenderingDefault(false);            BonusSkins.Register();            SkinManager.EnableFormSkins();            UserLookAndFeel.Default.SetSkinStyle("DevExpress Style");            Application.Run(new Frm_ShowInfo());        }    }}

 

C#------EntityFramework實體載入資料庫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.