Database Reference
In-Depth Information
txtCurrency.AppendText("\t");
txtCurrency.AppendText("\n");
}
As you can see, the EntityContainer object exposes the columns names through IntelliSense. Or, if
you put a . (dot), you will see all the fields of the Sales.Currency table, which is simpler than doing the
DataReader's rdr[0], rdr[1] technique, as you experimented with in the previous chapter. In other
words, the Entity Framework has “mapped” each record from the Sales.Currency table into an object.
The properties have the same names as the columns of the table, but working with an object fits the
object-oriented coding style.
Try It: Schema Abstraction Using an Entity Data Model
In the previous exercise, you created an Entity Data Model named AWCurrencyModel (because this is
the name of your .edmx file); in this exercise, you will see how this Entity Data Model will help developers
achieve schema abstraction and modify the database without touching the data access code throughout
the project or in the data access layer (DAL). That is, the developer can simply remove the table
reference from the model and then add it back. The columns would be realigned, and the code could
then be updated to reference the corresponding properties.
1. Start SQL Server Management Studio, expand the Databases node, expand the
AdventureWorks database node, and then expand the Tables node. In the list
of tables, expand the Sales.Currency node and then expand the Columns folder.
2. Select the Name column, right-click, and select the Rename option. Rename
the Name column to CurrencyName.
3. Select the ModifiedDate column, right-click, and select the Rename option.
Rename the ModifiedDate column to ModifiedCurrencyDate.
4. So, basically, we added the Currency term in these two columns. Now exit
from SQL Server Management Studio by selecting File Exit.
5. As you can imagine, our PublishCurrency form and the database have a
column name mismatch, so we will now view the exception that the
application will report because of this recent column name change. To do so,
we will add a TRY…CATCH block to report the issue. Modify the
PublishCurrency.cs code to look like Listing 19-2.
Listing 19-2. Adding TRY…CATCH to PublishCurrency.cs to Show Exception Details
try
{ AWCurrencyEntities currContext = new AWCurrencyEntities();
foreach (var cr in currContext.Currencies)
{
txtCurrency .AppendText(cr.ModifiedDate.ToString());
txtCurrency.AppendText("\t\t");
txtCurrency.AppendText(cr.CurrencyCode.ToString());
txtCurrency.AppendText("\t\t");
 
Search WWH ::




Custom Search