1. Background
Example: the relationship between the destination class (Desctination) and the pub (lodging) is 1 destinations that can have multiple inns. Then the relationship is a 1-to-many relationship.
2.Code
/// <summary> ///tourist Destinations/// </summary> Public classDestinationcontract {/// <summary> ///Destination PRIMARY Key/// </summary> Public LongDestinationId {Get;Set; } /// <summary> ///Destination Name/// </summary> Public stringName {Get;Set; } /// <summary> ///the country where the destination is located/// </summary> Public stringCountry {Get;Set; } /// <summary> ///Destination Profile/// </summary> Public stringDescription {Get;Set; } /// <summary> ///Promotional Pictures/// </summary> Public byte[] Photo {Get;Set; } /// <summary> ///all the inns in the destination/// </summary> PublicList<lodgingcontract> Lodgings {Get;Set; } }
/// <summary> ///Inns in Destination city/// </summary> Public classLodgingcontract {/// <summary> ///the primary key of the inn/// </summary> Public LongLodgingid {Get;Set; } /// <summary> ///the primary key of the city where the Inn is located/// </summary> Public LongLgdestinationid {Get;Set; } /// <summary> ///name of the inn/// </summary> Public stringName {Get;Set; } /// <summary> ///the price of the inn/// </summary> Public decimalPrice {Get;Set; } /// <summary> ///Inn Destination/// </summary> PublicDestinationcontract Destination {Get;Set; } }
3. How to configure
How to use the fluent API to configure the relationship, without generating redundant database fields, only the relational configuration, in lodging (more) entitytypeconfiguration<> write the following code
hasrequired (L = l.destination). Withmany (). Hasforeignkey (L = l.lgdestinationid);
This allows the program to run normally, but the database will still generate extra fields, such as:
It is obviously possible to insert the desired foreign key in the foreign key lgdestinationid of the lodging table, but it will automatically generate the Destinationcontract_destinationid field, which is obviously not our intended effect.
4. Improvements
After a constant attempt, we need to configure the following configuration classes in the destination entitytypeconfiguration:
Hasmany (d = d.lodgings). withrequired (L = l.destination). Hasforeignkey (t = t.lgdestinationid); // This is correct, no extra fields are generated
The effect of the operation is as follows:
5. Fundamentals of relational configuration for the Codefirst Fluent API
There are three types of relationships between tables and tables:
1.Optional (a property can have a single instance or not) the corresponding method: Hasoptional
2.Required (a property must have a single instance) corresponds to the method: hasrequired
3.Many (a property can have a collection of a single type) corresponds to the method: Hasmany
In most cases, you also need to add the With method after the has method. Withoptional, withrequired, Withmany
First of all, the time is not early, everyone good night, if you have help, trouble a praise it.
About EntityFramework one-to-many Fluent API configuration issues