C#学习速记_数据库

使用数据库

使用 Code First 方法在 C# 中创建对象,存储在数据库中,并使用 LINQ 查询对象

Entity Framework

  • 实体关系模型
  • EF 将 C# 程序中的对象映射到关系数据库的实体上
  • EF 建立在 ADO.NET 的基础上,而 ADO.NET 是基于 .NET 的低层数据访问库。

Code First 数据库

using System.Data.Entity;
using System.ComponentModel.DataAnnotations;

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    [Key] public int Code { get; set; } // [Key] 数据注解,告诉 C# 使用这个字段做为数据库中每个对象的唯一标识符
}

public class BookContext : DbContext
{
    public DbSet<Book> Books { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (var db = new BookContext())
        {
            Book book1 = new Book { Title = "Beginning Visaul C# 2015", Author = "Perkins, Reid, and Hammer" };
            db.Books.Add(book1);

            Book book2 = new Book { Title = "Beginning XML", Author = "Fawcett, Auin, and Ayers" };
            db.Books.Add(book2);

            db.SaveChanges();

            var query = from b in db.Books
                        orderby b.Title
                        select b;

            foreach (var b in query)
            {
                WriteLine($"{b.Title} by {b.Author}, code={b.Code}");
            }

        }

        ReadKey();
    }
}

导航数据库关系

public class Book
{
    public string Title { get; set; }
    public string Author { get; set; }
    [Key]
    public int Code { get; set; }
}

public class Store
{
    [Key]
    public int StoreId { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public virtual List<Stock> Inventory { get; set; }
}

public class Stock
{
    [Key]
    public int StockId { get; set; }
    public int OnHand { get; set; }
    public int OnOrder { get; set; }
    public virtual Book Item { get; set; }
}

public class BookContext : DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Store> Stores { get; set; }
    public DbSet<Stock> Stocks { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        using (var db = new BookContext())
        {
            Book book1 = new Book
            {
                Title = "Beginning Visaul C# 2015",
                Author = "Perkins, Reid, and Hammer"
            };
            db.Books.Add(book1);

            Book book2 = new Book
            {
                Title = "Beginning XML",
                Author = "Fawcett, Auin, and Ayers"
            };
            db.Books.Add(book2);

            var store1 = new Store
            {
                Name = "Main St Books",
                Address = "123 Main St",
                Inventory = new List<Stock>()
            };
            db.Stores.Add(store1);

            Stock store1book1 = new Stock
            {
                Item = book1,
                OnHand = 4,
                OnOrder = 6
            };
            store1.Inventory.Add(store1book1);

            Stock store1book2 = new Stock
            {
                Item = book2,
                OnHand = 1,
                OnOrder = 9
            };
            store1.Inventory.Add(store1book2);

            var store2 = new Store
            {
                Name = "Campus Books",
                Address = "321 College Ave",
                Inventory = new List<Stock>()
            };
            db.Stores.Add(store2);

            Stock store2book1 = new Stock
            {
                Item = book1,
                OnHand = 7,
                OnOrder = 23
            };
            store2.Inventory.Add(store2book1);

            Stock store2book2 = new Stock
            {
                Item = book2,
                OnHand = 2,
                OnOrder = 8
            };
            store2.Inventory.Add(store2book2);

            db.SaveChanges();

            var query = from store in db.Stores
                        orderby store.Name
                        select store;

            foreach (var store in query)
            {
                WriteLine($"{store.Name} located at {store.Address}");

                foreach (Stock stock in store.Inventory)
                {
                    WriteLine($"{stock.Item.Title}");
                    WriteLine($"{stock.OnHand}");
                    WriteLine($"{stock.OnOrder}");
                }
            }

        }

        ReadKey();
    }
}

处理迁移

// 工具 - NuGet包管理器 - 程序包管理器控制台

Enable-Migrations -EnableAutomaticMigrations

Update-Database

在已有的数据库中创建和查询XML

 using (var db = new BookContext())
{
    var query = from store in db.Stores
                orderby store.Name
                select store;

    foreach (var s in query)
    {
        XElement storeElement = new XElement("store",
            new XAttribute("name", s.Name),
            new XAttribute("address", s.Address),
            from stock in s.Stocks
            select new XElement("stock",
                new XAttribute("StockID", stock.StockId),
                new XAttribute("onHand", stock.OnHand),
                new XAttribute("onOrder", stock.OnOrder),
                new XElement("book",
                    new XAttribute("title", stock.Books.Title),
                    new XAttribute("author", stock.Books.Author)
                )
            )
        );
        Console.WriteLine(storeElement);
    }

}

Console.ReadKey();

转载于:https://my.oschina.net/moyung2014/blog/1820458

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值