Entity Framework

Páginas: 5 (1213 palabras) Publicado: 6 de noviembre de 2012
Code First Conventions
Code First enables you to describe a model by using C# or Visual Basic .NET classes. The basic shape of the model is detected by using conventions. Conventions are sets of rules that are used to automatically configure a conceptual model based on class definitions when working with Code First. The conventions are defined in theSystem.Data.Entity.ModelConfiguration.Conventions namespace. You can further configure your model by using data annotations or the fluent API. Precedence is given to configuration through the fluent API followed by data annotations and then conventions. For more information see Data Annotations , Fluent API - Relationships , Fluent API - Types & Properties and Fluent API with VB.NET . A detailed list of Code First conventions is available inthe API Documentation . This topic provides an overview of the conventions used by Code First. Type Discovery
When using Code First development you usually begin by writing .NET Framework classes that define your conceptual (domain) model. In addition to defining the classes, you also need to let DbContext know which types you want to include in the model. To do this, you define a context classthat derives from DbContext and exposes DbSet properties for the types that you want to be part of the model. Code First will include these types and also will pull in any referenced types, even if the referenced types are defined in a different assembly. If your types participate in an inheritance hierarchy, it is enough to define a DbSet property for the base class, and the derived types will beautomatically included, if they are in the same assembly as the base class. In the following example, there is only one DbSet property defined on the SchoolEntities class (Departments). Code First uses this property to discover and pull in any referenced types.
public class SchoolEntities : DbContext { public DbSet Departments { get; set; } } public class Department { // Primary keypublic int DepartmentID { get; set; } public string Name { get; set; } // Navigation property public virtual ICollection Courses { get; set; } } public class Course { // Primary key public int CourseID { get; set; } public string Title { get; set; } public int Credits { get; set; } // Foreign key public int DepartmentID { get; set; } // Navigation propertiespublic virtual Department Department { get; set; } } public partial class OnlineCourse : Course { public string URL { get; set; } }
http://msdn.microsoft.com/en-us/data/jj679962
United States (English)
Sign in
1 of 409/10/2012 11:30 a.m.
Entity Framework Code First Conventions

36. 37. 38. 39. 40. 41.
1.
1. 2. 3. 4. 5. 6. 7. 8.
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16.public partial class OnsiteCourse : Course { public string Location { get; set; } public string Days { get; set; } public System.DateTime Time { get; set; } }
If you want to exclude a type from the model, use the NotMapped attribute or the DbModelBuilder.Ignore fluent API.
modelBuilder.Ignore();
Primary Key Convention
Code First infers that a property is a primary key if a property ona class is named “ID” (not case sensitive), or the class name followed by "ID". If the type of the primary key property is numeric or GUID it will be configured as an identity column.
public class Department { // Primary key public int DepartmentID { get; set; } . . . }
Relationship Convention
In the Entity Framework, navigation properties provide a way to navigate a relationshipbetween two entity types. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). Code First infers relationships based on the navigation...
Leer documento completo

Regístrate para leer el documento completo.

Estos documentos también te pueden resultar útiles

  • Entity Framework
  • ADO Entity Framework
  • Entity framework
  • entity framework
  • Entity Framework
  • Enfoques De Entity Framework
  • Framework
  • Que es framework

Conviértase en miembro formal de Buenas Tareas

INSCRÍBETE - ES GRATIS