Into
Under .NET, there are two native ORMs developed and supported by Microsoft, the Entity Framework and Linq2Sql. However, the Entity Framework continues to develop at an impressive pace, and nothing really is known about the future of Linq2Sql.
Entity Framework offers a convenient designer, a huge number of mapping options, autogeneration of model classes, but there is a fat minus for all of this - giant and bloated generated classes, which also cannot be changed manually - because every time the model changes in the designer, everything will be recreated again . Compare this to the pure classes, and the attributes added to them, as in Linq2Sql, and you will understand why so many people claim that Linq2Sql is lightweight and EF is monstrous.
Of course, every problem has a solution, and this is no exception. Partial classes will allow you to add the necessary functionality, and specially created classes with validation rules, marked with the [MetadataType] attribute, will allow you to use validation attributes for model classes. But together it turns out not very nice - the classes spread over the project, the increase in their number, and all the same difficulty in support.
')
Don't forget about the working conditions of the model classes: they must either inherit from EntityObject or implement the interfaces of EntityWithKey, IEntityWithChangeTracker and IEntityWithRelationships
So what do those who want to get the most simple classes to work within ORM?
ROSO and Code-First
For the first time, support for ROSO (Plain Old CLR Objects), that is, work with simple classes, as with models appeared in EF 4.
Note
Despite the stated support in Visual Studio SP1, I still had to install
Visual Studio Tools for SQL Server Compact 4 and the
EF Code First Package to work correctly.
For example, here is a class
public class Author { public int AuthorID { get; set; } public string Email { get; set; } public string Name { get; set; } }
Describes the full-featured “author” entity, and it does not need a base class, no interfaces, no attributes or metadata.
The simplest entry point would look like this:
public class Library : DbContext { public DbSet<Author> Authors { get; set; } }
And that's it! Immediately after creating the Library object, you can work with it using all the advantages of EF.
Code-First is a new feature that is on a par with Model-First and Database-First. Now you can first write code that describes the model classes, and then the framework will automatically create a database using such a code.
The most pleasant thing is that the types of relationships will be recognized - it is enough to define just the object references for 1: 1, the ICollection for the 1: n relationship, and the mutual ICollection for m: n. In this case, an intermediate table will also be created automatically. To ensure a “lazy” load, the virtual keyword in the property definition will suffice.
Attributes from the
System.ComponentModel.DataAnnotations
namespace are also fully supported.
The main ones are:
• [Key] - indicates that this property will be key.
• [NotMapped] - do not display the property on columns in the database.
• [Column (“columnName”, TypeName = ”typeName”] - indicates the column in the database, which displays the field and its type. Useful when the class has a byte [] type property that stores, for example, a drawing, and in the database it is displayed on special type image.
UPD However, you cannot specify a subset of the type in this way: for example, for strings, the base type nvarchar (MAX) is selected and, using this attribute, you cannot explicitly specify the length of the nvarchar field (30)
• [MinLength], [MaxLength], [Required] and others to provide data verification
Code-First is designed to work with the new version of SQL Server CE 4.0. Support for this database is included in MVC3, and some people are already
transferring sites to work with such a database, and not standard SQL Express.
By default, the database generator tries to create a SQL Express database, if it does not work, then SQL Server CE. This behavior is easily overridden in app.config
<connectionStrings> <add name="Library" connectionString="Data Source=Library.sdf" providerName="System.Data.SqlServerCe.4.0"/> </connectionStrings>
Note that the name of the connection string must match the name of the access point class (DbContext).
You can also define a database creation strategy (primary generation or update). Every strategy is a class that implements the IDatabaseInitializer interface, i.e. You can define your strategy, which enters any data in the database during the active development of the program.
Predefined strategies - CreateDatabaseIfNotExists (create a database if it does not exist), DropCreateDatabaseAlways (the database will always be deleted and re-created), DropCreateDatabaseIfModelChanges (re-created with changes in the model)
Changing the strategy of working with the database call
Database.SetInitializer (new DropCreateDatabaseAlways ());
When creating a database, the framework creates another EdmMetadata service table, which stores the model hash imprint at the time of creation. This is inconvenient when actively developing the database and classes, but there is a solution - overloading the OnModelCreating method of the DbContext class.
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove<IncludeMetadataConvention>(); }
In general, Conventions is a pretty powerful thing. They allow you to configure the display of properties on the database based on a variety of signs, enable / disable namespaces, and more. Read more about them
here.
Code-First really provides many opportunities for managing the code of models in the most convenient way for you. I believe that especially for small projects, as well as for mobile devices (yes, SQL Server CE 4.0 will be supported in Windows Phone Mango), this approach will be maximally justified.