Note On <Programming Entity Framework 2nd Edition> -04

来源:互联网 发布:应用市场软件 编辑:程序博客网 时间:2024/05/16 18:22

Setting Default Values




"You can also set default values in your database, so why define defaults directly in the model? The CustomerTypeID property is non-nullable. Not only is this defined in the database, but it is also defined in the conceptual model. Because it’s non-nullable in the conceptual model, you must provide a value for this property; otherwise, when you call SaveChanges, a run-time exception will be thrown. Therefore, setting the default in the model ensures that some value is provided even when the developer doesn’t specifically assign the property value."




Mapping Stored Procedure







The mapping of DeletePayment stored procedure is different from this textbook.





Really? How?When a payment is updated, the database will automatically update the RowVersion field. The UpdatePayment procedure returns the new value.


Many-To-Many Relationhips


These tables did not appear in the model as entities. The EDM has the ability to represent many-to-many relationships while hiding the join in the mappings. But it can do this only when the join table has just the relevant keys and no additional fields. These two tables meet that criterion, as they have only the IDs of the items they are joining. If the join tables had additional properties, such as DateCreated, the EDM would have created entities for them.


Building Assembly






When a project containing an EDMX is compiled, the compiler extracts the StorageModels, ConceptualModels, and Mappings sections of the EDMX file and creates individual schema files from them. In this case, the files are BAModel.ssdl, BAModel.csdl, and BAModel.msl. By default, these files are embedded into the assembly that is built from the project.


If you look at the metadata portion of the EntityConnection string that the Entity Data Model Wizard inserted into the app.config file, you’ll see the following notation:

res://*/BAModel.csdl|res://*/BAModel.ssdl|res://*/BAModel.msl

Much of the functionality in the Entity Framework depends on its ability to read the schema files. The * in the metadata of the connection string indicates that you can find the files in an assembly. Entity Framework will search all loaded assemblies until it finds the one with these embedded files.




0 0