Entity Framework 4.1: Basics (1)

简介:   As its name suggest, with code-first, you start with the code.  You can create the corresponding database directly from the code, but you could also be working from an existing DB.

  As its name suggest, with code-first, you start with the code.  You can create the corresponding database directly from the code, but you could also be working from an existing DB.  The advantage of code-first is that your entity-classes don’t have any EF artefacts on them:  they don’t derive from a specific class and they do not have funky attributes on them.  Well…  for the attributes, as we’ll see, that’s optional!

  Let’s start with a simple entity model:  Order and OrderDetail.  We start by modelling it as classes:

public class Order
{
    public int OrderID { get; set; }
    public string OrderTitle { get; set; }
    public string CustomerName { get; set; }
    public DateTime TransactionDate { get; set; }

    public List<OrderDetail> OrderDetails { get; set; }
}

public class OrderDetail
{
    public int OrderDetailID { get; set; }
    public int OrderID { get; set; }
    public decimal Cost { get; set; }
    public string ItemName { get; set; }

    public Order Order { get; set; }
}

  

  Note the following about those classes:

  • They do not derive from any EF classes
  • They do not use EF attributes
  • An Order contains a list of OrderDetail and an OrderDetail contains a reference to its Order
  • Each property is either
    • A simple CLR Type (e.g. string, int, etc.)
    • An entity-type (e.g. Order)
    • A list of entity type (e.g. List<OrderDetail>)

In order to map those class into a DB, we need a container, a database-context:

public class MyDomainContext : DbContext
{
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }

    static MyDomainContext()
    {
        Database.SetInitializer<MyDomainContext>(new DropCreateDatabaseIfModelChanges<MyDomainContext>());
    }
}

This class is EF-aware.  It doesn’t have to sit in the same assembly than your model classes though.

At least a context must satisfy the following:

  • It derives from System.Data.Entity.DbContext
  • It has a property for each entity set we want to expose
  • Each property is of type System.Data.Entity.DbSet<T> where T is the type of the entity
  • Each property is read / write (get / set)

 

With that in place在这里, the DbContext base class uses reflection to pick-up the entity-sets and a bunch of convention to infer the underlying database model it should map to.

The conventions take decisions such as for the entity of type Order, the property OrderID must be its primary key.  Other convention will infer the column-name (by default the property name), the column type (e.g. a string maps to nvarchar(128) and should be nvarchar(MAX) in the final version), if a column is nullable (by default, all column but primary and foreign keys are nullable), etc.  .  We’ll see that there are ways to override those conventions.

We’ve added a static constructor here.  The instruction in the static constructor sets a standard for the entire app-domain:  when a database context is initialized, check if the schema in the database is conform to the model and if not drop the DB and recreate it.  The way this is done is that EF creates a table dbo.EdmMetadata and persist a hash of the model schema in it.

If the database doesn’t exist, EF will create it.  Which database?  By default, it creates a DB with the name of your context on the local machine.  You can override that in many ways.  The simplest is to add a connection string in your configuration file with the name of the context (in my case, "MyDomainContext").  Another way is to implement a constructor and call the non-default base-class constructor in it.

In the next blob entry, I’ll show how to override the conventions.

目录
相关文章
|
BI Windows
Qt 报表实现简述(一)
Qt 报表实现简述(一)
627 1
|
存储 监控 负载均衡
走向IPv6,阿里巴巴IPv6规模化部署实践
IPv6是互联网升级演进的必然趋势,我国主流APP也正式进入到IPv4和IPv6的双栈时代。本文将从APP及云产品的角度,和大家分享一下我们在这个过程中的经验积累,为进一步推动IPv6规模化部署提供参考。
走向IPv6,阿里巴巴IPv6规模化部署实践
|
调度 索引
NR PUCCH(四) UL data operation
UE 在connected mode 需要实时和网络进行上下行通信,在UE有UL data要发送但是没有UL grant时,就需要向网络端发送SR请求资源,网络收到SR就会在激活的BWP上发送 UL DCI给UE,UE 根据UL DCI 信息 获得UL grant ,然后在PUSCH对应的资源上就可以发送UL data给网络,最后网络端通过HARQ 过程指示是否有收到对应的data。这是UL data 的基本流程,下面通过实际log分别看下UL data operation的各个过程。
deb官方源、国内源
deb官方源、国内源
2074 0
|
JSON Java 数据格式
Java如何发起http的get请求的实现
Java如何发起http的get请求的实现
602 0
|
人工智能 自然语言处理 算法
智能体来了:阿里云×黎跃春共建AI教育与产业融合新生态
2025年被视为“智能体元年”,AI正从工具创新迈向生态融合。阿里云携手黎跃春教授推动“大学生智能体开发实训”,通过零基础教学、产教融合与创业孵化,培养能驾驭AI的新型人才,助力学生从学习到创新、创业的全链条成长,开启教育与产业协同发展的新时代。
740 10
|
机器学习/深度学习 边缘计算 算法
2025年度机器视觉系统公司综合推荐指南:从技术实力到落地案例的全方位选型参考
在智能制造背景下,机器视觉成质量管控核心。本文发布2025年企业综合推荐,从技术、场景、实施等维度评估,解析领先企业方案与落地案例,提供选型建议与发展展望,助力制造企业科学决策,提升质效竞争力。
534 0
|
存储 人工智能 自然语言处理
基于MCP的桥梁设计规范智能解析与校审系统构建实践
本文探讨了在桥梁设计领域应用AI技术的实践与思考,针对传统设计中规范查阅效率低、理解偏差大等问题,提出基于MCP协议构建智能解析与校审系统。系统通过PDF解析、知识图谱构建及自然语言处理等技术,实现规范条文的结构化存储和智能化应用,大幅提升设计效率与准确性。开发过程中克服了多模态文档解析、专业术语理解等挑战,并通过迭代优化持续改进性能。未来该系统有望扩展至更多工程领域,推动设计流程智能化升级。
641 8
|
安全 网络安全 数据安全/隐私保护
内网/局域网IP地址申请https证书方法
为内网/局域网IP地址申请HTTPS证书,可增强数据传输的安全性。首先确定固定的内网IP地址,选择可信的证书颁发机构,注册并申请免费或付费SSL证书,提交相关信息,支付费用(如有)。证书申请成功后,下载并配置于服务器,确保通过浏览器访问时显示为安全连接。注意定期更新证书,确保持续的安全保障。此过程适用于局域网内部通信加密,提升内网服务的安全水平。
|
机器学习/深度学习 资源调度 Java
RT-DETR改进策略【注意力机制篇】| 2024 SCI TOP FCAttention 即插即用注意力模块,增强局部和全局特征信息交互
RT-DETR改进策略【注意力机制篇】| 2024 SCI TOP FCAttention 即插即用注意力模块,增强局部和全局特征信息交互
670 2