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

本文探讨了在数据库设计中设置默认值的重要性,并解释了为何需要在模型层面定义这些默认值。此外,还讨论了存储过程映射的不同方式以及如何处理多对多关系,特别是在实体数据模型(EDM)中如何隐藏连接表。

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.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值