in the ORM Entity Framework, many of the Inheritance, association, etc.
in the Hibernater entities that need to be configured in the XML file to maintain relationships of different entities
and in EF This is a visual graphical operation that controls the relationship between:
Association Mappings
If you have passed Powerdeesigner The relationship between entities has been drawn. , entity relationships are imported into a physical model into a database. importing from a database through the model models of EF Figure 2
Choose a good database, choose a good Entity Framework, note that the framework here is and Framework corresponding to the
Here 's the point.
we see have (include foreign key columns in the model) If you select the column, this adds the foreign key column to the entity that is associated with the relationship . Then Add the entity to manually add the maintenance of the value, the first thing I chose is not included. We look at the specific relationship
do not show foreign key associations
So how do we code it?
First we Create a customer class
Testefentities db = new testefentities (); Customers cus = new Customers { cusname= "Hanhanxml", Custerid = Guid.NewGuid (). ToString ()//a0818e4d-9ccd-48ea-b70a-88a78bf3113c }; Db. Customers.add (cus); Db. SaveChanges ();
This creates a customer message.
Here is the customer order for shopping
Disadvantages:
This association that does not show foreign keys is not possible to add a single table, only to implement the associated table add (perhaps I know shallow, I hope the master guidance)
Foreign Key Associationdisplaying foreign key properties in an association table Mode 1 Add an association to the model
Add Way 2 database is related, import directly from the database
Advantages:
In this way, a single table can be added, or the associated table is added simultaneously.
Instance Code
Testefentities db = new testefentities (); Customers cus = new Customers { cusname = "Hanhan", Custerid = Guid.NewGuid (). ToString ()//678d0b30-321a-4c72-9ec3-1f8c7f3c254b }; Orderinfoes order = new orderinfoes//one-way add data { content= "Hanhan", Orderid=guid.newguid () to the order table. ToString (), ordername= "Hanhamxml", customerscusterid=cus. custerid,//Query association Property FOREIGN key value }; Orderinfoes order2 = new orderinfoes//Insert two tables simultaneously, order and Customer table (customer data inserted in Customer table) { content = "Hanhan", OrderId = Guid.NewGuid (). ToString (), ordername = "Hanhamxml", Customers = cus }; Db. Customers.add (cus); Db. Orderinfoes.add (order); Db. SaveChanges ();
Inheritance Mappings
in the EF model, inheritance is a one-to-one mapping, and to the database is a table for each subclass, which holds the attribute instance graph that is unique to the child class .
Advantages and Disadvantages:
This design method fully conforms to the design principle of the relational model, and there is no redundancy. maintenance is more convenient, the modification of each class only needs to modify its corresponding table, flexibility is very good, is completely reference to the way the object inherits the configuration, for the parent class query needs to use the left outer link, for the subclass query needs to use the inner link, for the subclass of the persistence of at least two tables to process
Model Diagram
Database diagrams
Two tables are associated with a unique primary key, which is to insert two table data at the same time
Code
Testefentities db = new testefentities (); Customers cus = new Customers { cusname= "hanhan3", custerid=guid.newguid (). ToString () }; Orderinfoes order = new Orderinfoes { content= "hanhan2", Custerid = cus. Custerid, cusname = cus. Cusname, orderid=guid.newguid (). ToString (), ordername= "Hanhan5" }; Db. Orderinfoes.add (order);//Add both table db at the same time . SaveChanges (); var orders=db. Orderinfoes.asqueryable ();//query subclass all Data foreach (var item in orders) { Console.WriteLine (item). Cusname);//query data, name of parent class }
Summary:
From the above two points we see that the Entity Mapping Framework is almost identical, If you know Hibernate, then for EF The entity mapping aspect is very easy to get started
EntityFramework Entity Mappings