EntityFramework 4.1 代码优先 TPT 和数据完整性
阅读本文,
并自己做了一个简单的测试,我觉得数据库不是很安全(从数据完整性来看)。
我的意思是,让我们以该页面中的示例为例:
1 个基类和 2 个子类: 基础 = 账单明细 子类 = CreditCards、BankAccounts
3 个表。 BillingDetail 与信用卡、银行帐户共享主键。
从应用程序中说,我们创建一个信用卡实体并保存到数据库。我们在 BillingDetail 中得到一行 + 在 CreditCards 中得到一行具有相同 id 的行。
如果我们随后转到数据库,并在 BankAccounts 中手动创建具有相同 ID 的行,那么它现在也是应用程序中的 BankAccounts 实体。
我的意思是这种行为……错了吗?
(并不是说我过于担心这一点,但是在数据库中插入触发器或约束之类的东西来确保完整性不是更好吗?)
Reading this article,
and doing a simple test myself, I feel that the database isn't very safe (from data integrity).
I mean, lets take the example in that page:
1 base class and 2 subclasses:
base = BillingDetail
subclasses = CreditCards, BankAccounts
3 tables. BillingDetail has primary key shared with CreditCards, BankAccounts.
Say from the application, we create a CreditCards entity and save to database. We get a row in BillingDetail + a row with same id in CreditCards.
If we then go to database, and manually create a row in BankAccounts with the same ID, it now is also a BankAccounts entity in the application.
I mean is this behavior.... wrong?
(Not that I am that overly concerned about this, but wouldn't it have been better to insert something like trigger or constraint in database to ensure integrity?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您觉得需要额外的完整性,您可以随时创建自己的触发器。您所描述的方法确实在数据库中是允许的,但是一旦您进行了此类更改,您的 EF 应用程序在尝试读取此类损坏的记录时将始终失败。
EF 这样做的原因是,使用代码优先或模型优先适用于数据库仅适用于您的应用程序的场景,因此您不必处理来自其他地方的此类修改。
You can always create your own trigger if you feel that you need additional integrity. Your described approach will be indeed allowed in the database but once you do such change your EF application will always fail when trying to read such corrupted record.
The reason why EF does it this way is that using either code first or model first is for scenarios where database is only for your application so you don't have to deal with such modifications from elsewhere.