One of Entity Framework 4.1: Basic

Source: Internet
Author: User
Original Name: Entity Framework 4.1: BASICS (1) original address: http://vincentlauzon.wordpress.com/2011/04/03/entity-framework-4-1-basics-1/

We can see the English tutorial recommended for Entity Framework 4.1. To help you look more convenient, we can translate it briefly. This is a series of 8 articles, and this is 1st articles.

    1. One of Entity Framework 4.1: Basic
    2. Entity Framework 4.1 II: overwrite the default conventions
    3. Entity Framework 4.1 3: greedy loading and delayed Loading
    4. Entity Framework 4.1 4: complex types
    5. Entity Framework 4.1-5: Multi-to-many relationships
    6. Entity Framework 4.1: Optimistic Concurrency
    7. Entity Framework 4.1 7: Inheritance
    8. Entity Framework 4.1: bypassing EF query ing

From the code first name, you can guess that using code-first, you needCodeTo start data processing, you can directly generate the corresponding database through code or use an existing database. The advantage of using code first is that your entity class does not require any EF content: it does not need to be derived from a specific base class or any annoying labels attached to it. Okay. For tags, as we will see, they are optional.

Let's start with a simple entity model: order and order details. We use the following classes for modeling. Public Class Order
{
Public Int Orderid { Get ; Set ;}
Public String Ordertitle { Get ; Set ;}
Public String Customername { Get ; Set ;}
Public Datetime transactiondate { Get ; Set ;}

PublicList<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 ;}

PublicOrder order {Get;Set;}
}Note:

    • Not derived from any EF class
    • EF tag not used
    • Order details include orderdetail, which contains a reference pointing to an order.
    • Each attribute is:
      • Simple CLR types, such as string and int
      • Entity type, for example, order
      • List set of entities, for example: List <orderdetail>
You need to map these classes to the database through a container, which is called database-context: Public Class Mydomaincontext: dbcontext
{
Public Dbset < Order > Orders { Get ; Set ;}
Public Dbset < Orderdetail > Orderdetails { Get ; Set ;}

StaticMydomaincontext ()
{
Database. setinitializer<Mydomaincontext>(
NewDropcreatedatabaseifmodelchanges<Mydomaincontext>());
}
}This class is related to EF and does not need to appear in the same way as your model class.ProgramCentralized. Context must meet the following requirements:

    • Derived from system. Data. entity. dbcontext
    • Define an attribute for each object set you want to use
    • The type of each attribute is system. Data. entity. dbset <t>, and T is the object type.
    • Each attribute is read/write (get/set)
Here, the dbcontext base class obtains the entities mapped to the database through reflection. This follows a series of conventions. For example, for order, its attribute orderid must be the primary key. Other conventions will be used to deduce the column name and column type. By default, the column name in the database is the attribute name, use the string type to exclude nvarchar (128) in the database. If the attribute type is left empty, it indicates that null is allowed in the database. We can see that if these conventions are overwritten. We will add a static constructor, which establishes a standard for the entire application domain. When the context of the database is initialized, check whether the database architecture is consistent with the model. If not, delete the database and recreate it. EF will create a table named DBO. ed1_adata and save the hash of the model structure to it. If the database does not exist, EF will create it. What database will it create? By default, the context object name will be used on your local machine. There are many ways to override this behavior, the simplest way is to add a database connection string named "context Object Name" in the configuration file. Here, it is called "mydomaincontext" and you can also implement a constructor, then, call non-default base class constructor. In the next article, I will demonstrate how to override these conventions.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.