• EF Code First 入门


    1、安装 Entity Framework

    在 VS 编辑中点击 Tools -> Library Package Manager -> Package Manager Console

    在 Package Manager Console 窗口中执行下面语句,安装最新版 Entity Framework:

    PM> Install-Package EntityFramework 

    安装完成之后,将自动添加对 EntityFramework 的引用,并自动添加 App.config 和 packages.config 文件。

    2、简单配置

    在生成的配置文件中,加入如下代码,修改数据库连接信息:

    View Code 
      <connectionStrings>
        <add name="EFTestContext" connectionString="Data Source=127.0.0.1;Database=EFTest;User=sa;Pwd=sapassword;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    3、实体类 

    View Code 
        public class Category
        {
            public string CategoryId { setget; }
            public string Name { setget; }
            public virtual ICollection<Product> Products { setget; }
        }

        public class Product
        {
            public int ProductId { setget; }
            public string Name { setget; }
            public string CategoryId { setget; }
            public virtual Category Category { setget; } 

    4、数据库 Context 

    View Code 
    using System.Data.Entity; // 引用命名空间

    namespace EFTest
    {
        public class EFTestContext : DbContext
        {
            public EFTestContext()
                : base("EFTestContext"// 配置文件中连接字符串 Key 值
            {
            }

            public DbSet<Category> Categories { setget; }
            public DbSet<Product> Products { setget; }
        }

    5、读写数据 

    View Code 
            static void Main(string[] args)
            {
                //新增 Category
                using (var db = new EFTestContext())
                {
                    var food = new Category
                    {
                        CategoryId = "A002",
                        Name = "文具"
                    };
                    db.Categories.Add(food);
                    int recordsAffected = db.SaveChanges();
                }

                using (var db = new EFTestContext())
                {
                    //查询 Category
                    var category = db.Categories.Find("A003");
                    if (category == null)
                    {
                        category = new Category { CategoryId="A003", Name="体育" };
                        db.Categories.Add(category);
                    }

                    //新增 Product
                    var product = new Product { Name="篮球", Category=category };
                    db.Products.Add(product);
                    int recordsAffected = db.SaveChanges();

                    //使用LINQ查询 Product
                    var allProduct = from p in db.Products
                                     where p.CategoryId == "A003"
                                     orderby p.Name
                                     select p;
                    foreach (var item in allProduct)
                    {
                        Console.WriteLine(item.Name);
                    }                
                }
                Console.ReadKey();

    6、数据初始化或实体类改变 

    View Code 
        public class DbInitailizer 
        {
            /// <summary>
            
    /// 执行对数据库的初始化操作。
            
    /// </summary>
     
            public static void Initialize()
            {
                using (EFTestContext db = new EFTestContext())
                {
                    if (db.Database.Exists())
                    {
                        db.Database.Delete();                   
                    }
                    db.Database.Create();
                    //数据初始化操作
                }
            }      

    7、数据注释 Annotations 

    View Code 
    //使用注解,需引用该命名空间
    using System.ComponentModel.DataAnnotations;

    namespace EFTest.EFCodeFirst
    {
        public class Supplier
        {
            [Key]
            public string SupplierCode { getset; }
            public string Name { getset; }
        }

    EF 所支持的 Annotations 如下:

      KeyAttribute

    StringLengthAttribute 

      MaxLengthAttribute

    ConcurrencyCheckAttribute 

    RequiredAttribute 

    TimestampAttribute 

    ComplexTypeAttribute 

    ColumnAttribute  :  Placed on a property to specify the column name, ordinal & data type  

    TableAttribute  :  Placed on a class to specify the table name and schema

    InversePropertyAttribute  :  Placed on a navigation property to specify the property that represents the other end of a relationship

    ForeignKeyAttribute  :  Placed on a navigation property to specify the property that represents the foreign key of the relationship

    DatabaseGeneratedAttribute  :  Placed on a property to specify how the database generates a value for the property (Identity, Computed or None)

    NotMappedAttribute  :  Placed on a property or class to exclude it from the database

    8、Fluent API

    View Code 
    //引用命名空间
    using System.Data.Entity; 
    using System.Data.Entity.ModelConfiguration.Conventions;
    using System.Data.Entity.Infrastructure;

    namespace EFTest
    {
        public class EFTestContext : DbContext
        {
            public EFTestContext()
                : base("EFTestContext"// 配置文件中连接字符串 Key 值,不写默认为类名(EFTestContext)
            {           
            }

            public DbSet<Category> Categories { setget; }
            public DbSet<Product> Products { setget; }
            public DbSet<Supplier> Suppliers { getset; }

            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                //移除复数表名的契约
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
                //防止黑幕交易 要不然每次都要访问 EdmMetadata这个表
                modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
                
                modelBuilder.Entity<Supplier>()
                  .Property(s => s.Name)
                  .IsRequired();
            }
        }

    9、附加

    操作存储过程或SQL语句: 

    View Code 
            static void Main(string[] args)
            {
                using (var db = new EFTestContext())
                {
                    //执行存储过程(含有参数,如无参数,去掉最后的参数)
                    int recordsAffected = db.Database.ExecuteSqlCommand("sp_DeleteProductById @id"new SqlParameter("@id"1));
                    //执行SQL语句
                    string sql = string.Format("INSERT INTO [dbo].[Product] VALUES ('测试','A003')");
                    recordsAffected = db.Database.ExecuteSqlCommand(sql);
                }

    注解:

    View Code 
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    namespace EFTest
    {
        //自定义表名
        [Table("MyPerson")]
        public class Person
        {
            //主键,默认情况下属性被命名为ID、id或者[ClassName]Id
            [Key]
            public int Id { setget; }

            //设置字段不能为空
            [Required]
            public string Name { setget; }

            //设置字段的长度范围
            [MaxLength(20),MinLength(5)]
            public string ClassName { setget; }

            //不会创建的数据表字段
            [NotMapped]
            public int MyProperty { setget; }

            [Column("PersonPhoto",TypeName="image")]
            public byte[] Image { setget; }
        }

    谢谢。。。 

  • 相关阅读:
    布局页js文件问题
    sqlite如何更改表结构
    css各种样式
    layUI订单实现思路
    layUI使用总结
    easyui点击行内编辑,怎么获取行数据并赋值
    404
    PTA C语言作业
    python一行代码格式化日期
    校园网跨网段共享文件Samba+SSH
  • 原文地址:https://www.cnblogs.com/pengfei/p/2641589.html
Copyright © 2020-2023  润新知