EFCore code first 创建数据库

此处记录学习efcore 过程中一些学习笔记:

      1.code first 创建数据库,一对多关系对应为一个对象拥有一个集合,这个集合使用virtual标识以便使用lazyLoading,另一方面被拥有的对象也有所属对象及所属对象ID。多对多关系除了两个对象分别用拥有对方集合方式表示外,还得有两个对象关系对象存储关系所属对象。类型关系展示如下所示:

一个Customer有多个订单:

    public partial class Customers
    {
        public Customers()
        {
            Orders = new HashSet();
        }

        [Key]
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }

        [InverseProperty("Customer")]
        public virtual ICollection Orders { get; set; }
    }

一个订单order属于一个Customer,并且一个订单包含多个商品product

    public partial class Orders
    {
        public Orders()
        {
            ProductOrders = new HashSet();
        }

        [Key]
        public int Id { get; set; }
        public DateTime OrderPlaced { get; set; }
        public DateTime? OrderFulfilled { get; set; }
        public int CustomerId { get; set; }

        [ForeignKey(nameof(CustomerId))]
        [InverseProperty(nameof(Customers.Orders))]
        public virtual Customers Customer { get; set; }
        [InverseProperty("Order")]
        public virtual ICollection ProductOrders { get; set; }
    }

一个商品product分布在多个订单中:

    public partial class Products
    {
        public Products()
        {
            ProductOrders = new HashSet();
        }

        [Key]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Column(TypeName = "decimal(18, 2)")]
        public decimal Price { get; set; }

        [InverseProperty("Product")]
        public virtual ICollection ProductOrders { get; set; }
    }

此关系对象为订单,商品多对多关系对象:

    public partial class ProductOrders
    {
        [Key]
        public int Id { get; set; }
        public int Quantity { get; set; }
        public int ProductId { get; set; }
        public int OrderId { get; set; }

        [ForeignKey(nameof(OrderId))]
        [InverseProperty(nameof(Orders.ProductOrders))]
        public virtual Orders Order { get; set; }
        [ForeignKey(nameof(ProductId))]
        [InverseProperty(nameof(Products.ProductOrders))]
        public virtual Products Product { get; set; }
    }

 code first创建对象之后,得再创建DbContext对象,将那些是表给用DbSet标识出来:

public class ContosoPetsContext:DbContext
    {
        public DbSet Customers { get; set; }
        public DbSet Orders { get; set; }
        public DbSet Products { get; set; }
        public DbSet ProductOrders { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=ContosoPets;Integrated Security=true");
        }
    }

之后用包管理控制台输入命令:Add-Migration InitialCreat 创建对象数据库。之后Update-Database将更新到数据库。加字段或者修改字段之后,执行Add-Migration AddOrModifyFiled进行同步更新到数据库。

你可能感兴趣的:(ef,core)