📜 ⬆️ ⬇️

ADO.NET Entity Framework close use

Those who write requests in the code page is dedicated to ...

Greetings to all!

On Habré there is some information that the next version of VisualStudio 2008 will be ADO.NET EntityFramework. (I will reveal the secret, this version has already appeared.) This development is a universal framework that allows you to create a datalogy of your project in a couple of mouse clicks.
So far, working with datalogics, I have come across 2 types of projects. The first ones were created on the famous NHibernate framework, others were implemented by datalogics by programmers. I have been writing and developing various systems for 3 years already and all this time I have been developing the logic of working with data exclusively with pens.
And so, the other day, after I installed a new Windows, I downloaded VisualStudio WebDeveloper Express, and was happy to find the ADO.NET EntityFramework bundled. After some time, I registered a domain, created a simple website, and began to train my forces in writing programs for this framework.


I was a little unpleasantly surprised that most of the articles on the ADO.NET EF were narrowly focused. It was impossible to find complete information, from beginning to end, on how to properly set up the database, create entities, and perform basic operations on these entities. Most articles cover only one of these topics. Well, while studying, I decided to share my experience with you% username%. At the same time, using my code, you will not be able to raise the system of work with business processes, but still, you will understand which side to approach.
')


First you need to create a simple Web project with a database. It would also be nice to connect to the database immediately via DataBase Explorer. Just then it will be more convenient.



After that, a new element “ADO.NET Entity Data Model” should be added to the project.



The system will need to specify a string to connect to the database, as well as specify where the first ADO.NET EF model will come from.



In my database, I already have two very simple tables, Post and User, so without further ado, I made the system create a model based on my database. After all these very simple actions, I got a working database model. Moreover, having studied this model visually, I did not forget to look into the code, and see how the framework describes all my classes?

namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  1. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  2. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  3. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  4. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  5. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  6. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  7. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  8. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  9. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  10. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  11. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  12. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  13. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  14. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  15. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  16. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  17. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  18. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  19. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  20. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  21. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  22. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  23. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  24. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  25. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  26. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  27. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  28. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  29. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  30. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
  31. namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .
namespace DataBaseCore { /// <summary> /// There are no comments for DbModel in the schema. /// </summary> public partial class DbModel : global::System.Data.Objects.ObjectContext { /// <summary> /// Initializes a new DbModel object using the connection string found in the 'DbModel' section of the application configuration file. /// </summary> public DbModel() : base ( "name=DbModel" , "DbModel" ) { this .OnContextCreated(); } /* */ [global::System.Data.Objects.DataClasses.EdmEntityTypeAttribute(NamespaceName= "DataBaseCore" , Name= "Post" )] [global::System.Runtime.Serialization.DataContractAttribute(IsReference= true )] [global::System. Serializable ()] public partial class Post : global::System.Data.Objects.DataClasses.EntityObject { /// <summary> /// Create a new Post object. /// </summary> /// <param name="id">Initial value of Id.</param> public static Post CreatePost( int id) { Post post = new Post(); post.Id = id; return post; } * This source code was highlighted with Source Code Highlighter .


The trained eye of a datalogician showed the presence of a fairly simple and elegant class, which allowed working with posts in the system and with users.

Well, well, we already have the code, it remains only to start using it. And here, we open all the charms and capabilities of ASP.NET. Among the considerable number of data sources on the page, I saw the Entity Data Source, which is happy to provide data on request from our class. We drag it onto the form, launch the setup wizard, and quickly grab the datasource to our table of posts.



Undoubtedly, the datasore description in ASPX code has become much nicer.
  1. < asp: EntityDataSource ID = "dsPosts" runat = "server" ConnectionString = "name = DbModel"
  2. DefaultContainerName = "DbModel" EntitySetName = "Post" >
  3. </ asp: EntityDataSource >
* This source code was highlighted with Source Code Highlighter .

You can say - shines with grace.

After appearing on the data provider form, a consumer is also needed. Without being clever, I added a simple code that simply displays all the posts in sequence.

  1. < asp: Repeater runat = "server" ID = "repPosts" DataSourceID = "dsPosts" >
  2. < HeaderTemplate >
  3. </ HeaderTemplate >
  4. < ItemTemplate >
  5. < div >
  6. < h3 >
  7. < asp: Label ID = "lblHeader" runat = "server" Text = '<% # Eval ("Header")%>' > </ asp: Label >
  8. </ h3 >
  9. < p >
  10. < asp: Label ID = "lblText" runat = "server" Text = '<% # Helpers.TypographText (Eval ("Text"). ToString ())%>' > </ asp: Label >
  11. </ p >
  12. </ div >
  13. </ ItemTemplate >
  14. </ asp: Repeater >
* This source code was highlighted with Source Code Highlighter .


And here ends the simplest part of my story. Until now, everything that has been done can be repeated with the mouse. It was simple enough. We have just created an object-oriented database view, and using this view, we began to output data from the database to the page. At the same time, we never wrote a single request, did not receive data from the database directly, etc ...

But, what to display to the user, if we do not have anything in the database? Not good. We must write a form to fill the base. Now we will stop mouse-programming, and we will be engaged in writing of the code.

I have done the two most basic steps for working with posts in the system - this is adding and deleting. Making editing by analogy with this code is not difficult for anyone.

  1. namespace DBW
  2. {
  3. public class Post
  4. {
  5. public Post ()
  6. {
  7. }
  8. public static void New ( String PostText, String PostHeader, Int32 UserId)
  9. {
  10. DataBaseCore.DbModel m = new DataBaseCore.DbModel ();
  11. DataBaseCore.Post p = new DataBaseCore.Post ();
  12. p.Header = PostHeader;
  13. p.Text = Helpers.TypographText (PostText);
  14. p.PublishDate = DateTime .Now;
  15. p.User = ( from usr in m.User where usr.Id == UserId select usr) .First ();
  16. m.AddToPost (p);
  17. m.SaveChanges ();
  18. }
  19. public static void Delete ( Int32 PostId)
  20. {
  21. DataBaseCore.DbModel m = new DataBaseCore.DbModel ();
  22. DataBaseCore.Post p = new DataBaseCore.Post ();
  23. p = ( from pst in m. Post where pst. Id == PostId select pst) .First ();
  24. m.DeleteObject (p);
  25. m.SaveChanges ();
  26. }
  27. }
  28. }
* This source code was highlighted with Source Code Highlighter .


It would seem that everything is simple, and yes, it is. But there are a couple of nuances.
First is LINQ. Without it in ADO.NET anywhere. So you shouldn’t shirk and generally score on SQL or LINQ, you still have to write queries.
Secondly - this code is automatically generated by the framework, so you will not have to expect special convenience in some moments, and you should always be ready to change the code already created by the studio. For example, here in line 16 it would be more convenient to use not an object of type user, which I had to choose from the database, but to immediately transfer the value of the user identifier. It would be more convenient for this code, but it is not universal. Therefore, the code needs to be refined and rethought. Maybe you just need to pass not a user ID, but an object of type user.

What's next? Further I will continue to write the project, going deep into the wilds of the ADO.NET Entity Framework, and I will be happy to share my research with you, dear habrayuzer. Accordingly, there will be new articles with more serious and deep data.

UPD. The topic is very extensive. Not a percentage of possibilities is disclosed here, but the sequel will be 8-)

Source: https://habr.com/ru/post/47028/


All Articles