关系数据丢失时可以使用实体框架吗? [英] Can Entity Framework be used when relational data is missing?

查看:59
本文介绍了关系数据丢失时可以使用实体框架吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个网站,数据将从一个外部应用程序发送,该应用程序具有一个充满数据的数据库,该网站只需要其中的一部分.部分"是指每个表中的某些行,但不是全部.

I am creating a web site, and the data will be sent in from an external application that has a database full of data, only parts of which are needed for the web site. By "parts" I mean some of the rows in each table, but not all of them.

手头上的业务是管理慈善凭单的业务,他们拥有与客户有账户的客户.慈善机构可能希望登录该网站并查看有关已付钱款的信息,而客户可能希望登录并查看有关其帐户的信息.

The business in hand is one that manages charity vouchers, and they have customers who have accounts with them. Charities may want to log in to the web site and see info about money paid to them, and customers may want to log in and see info about their account.

但是,想要在线登录的客户数量仅占拥有帐户数量的一小部分.我们希望避免上载所有客户和慈善机构的信息,因为这将是大量的数据,这些数据必须保持最新,并且几乎不使用.

However, the number of customers who will want to log in online will only be a small percentage of those who have accounts. We want to avoid uploading all customer and charity info, as that would be a huge amount of data, which would have to be kept up-to-date, and which would mostly not be used.

当我们有一张已发行的代金券,但是客户的帐户中没有足够的钱来支付时,就会出现问题.在这种情况下,将保留凭证,等待客户存款.慈善机构希望查看持有的优惠券列表,但未显示客户名称,他们只看到优惠券详细信息,例如日期,金额和编号.

The problem arises when we have a voucher that has been issued, but the customer does not have sufficient money in their account to cover it. In such a case, the voucher is held, awaiting a customer deposit. The charities want to see a list of held vouchers, but are not shown the customer names, they only see voucher details, such as date, amount and number.

理想情况下,我希望网站数据库是完全相关的,但这将导致缺少数据的问题.例如,如果我们上载持有凭证的详细信息,而客户的详细信息不在网站数据库中(因为他们没有通过网站的访问权限),则插入到HeldVouchers表的操作将失败,因为外键参考客户"表的指向将指向不在数据库中的客户.

Ideally, I would like the web site database to be fully relational, but this will cause a problem with missing data. For example, if we upload details of a held voucher where the customer's details are not in the web site database (as they do not have access via the web site), then the insert to the HeldVouchers table would fail, as the foreign key reference to the Customers table would be pointing at a customer who isn't in the database.

我可以使所有外部引用都为空,但这仍然无济于事,因为客户ID不会为空,它将在主数据库中包含客户的ID.

I could make all foreign references nullable, but that still doesn't help, because the customer ID would not be null, it would contain the ID of the customer in the main database.

我可以在上载时进行检查,并且是否为不在数据库中的客户上​​载了持有的凭证,那么我可以将CustomerID设置为null.很好,除非该客户随后想要访问该网站,然后我们上载他的详细信息,然后必须更新HeldVouchers表.这将导致大量的额外工作,以及大量的额外上传.

I could check when uploading, and if a held voucher were uploaded for a customer who was not in the database, then I could set the CustomerID to be null. That would be fine, except that if that customer then wants access to the web site, and we upload his details, we would then have to update the HeldVouchers table. This is going to cause a whole lot of extra work, and a whole lot of extra uploads.

有人知道处理此问题的方法吗?到目前为止,我唯一的想法是使Web数据库完全不相关(我真的不喜欢这样做),然后向实体中添加扩展方法以模拟EF生成的实体,除非它们直接进入适当的DbSet并拉出与外键"引用匹配的所有实体.

Anyone any idea of a way to handle this? My only thought so far was to make the web database totally non-relational (which I really don't like doing), and then adding extension methods to the entities that would simulate the EF-generated ones, except they would go directly to the appropriate DbSet and pull out any entities that matched the "foreign key" reference.

我尝试了这一点,并提出了一个通用的扩展方法,该方法可以工作,但至少有两个非常严重的缺点...

I tried this, and came up with a generic extension method that works, but has at least two very serious drawbacks...

public static List<T> NavColl<T>(this EntityInterface entity, Func<T, bool> f)
  where T : class, EntityInterface {
  return Ctx.Set<T>().ToList().Where(f).ToList();
}

(此方法返回的等效属性是一个导航属性,它是一个集合.我有一个相似的属性,它在寻找单个实体.)

(This method returns the equivalent of a navigation property that is a collection. I have a similar one that looks for a single entity).

如果您有一个Charity对象(巧妙地命名为"charity"),并且想要获取所持有的代金券,则可以执行以下操作...

If you had a Charity object (cunningly named "charity"), and wanted to get the held vouchers, you could do something like this...

charity.NavColl<HeldVoucher>(ca => ca.CharityID == charity.ID)

第一个缺点是,我看到允许将Func传递到扩展方法中并且不让Linq-To-Entities发出嘶嘶声的唯一方法是枚举DbSet 之前,然后应用功能如果其中有很多数据,这可能会大大降低查询速度.

The first drawback is that the only way I could see to allow passing a Func into the extension method and not having Linq-To-Entities throw a hissy fit was to enumerate the DbSet before applying the Func. If there is a lot of data in there, this could slow down the query significantly.

也许更严重的是(因为我不希望性能成为主要问题),事实是扩展方法需要一个可以在其上工作的上下文.目前,我将实体与EF模型放在一个单独的项目中,因为这使我可以引用解决方案中任何项目的实体,而这些项目不需要引用该模型的项目.这有助于使各层保持分离,并允许进行测试等.

Perhaps more serious (as I don't expect performance to be a major issue) is the fact that the extension method needs a context on which to work. At the moment, I have the entities in a separate project from the EF model, as this allows me to reference the entities from any project in the solution, without those projects needing a reference to the model's project. This helps keep the layers separated, and allows for testing, etc.

但是,如果扩展方法需要上下文,则实体项目需要对模型项目的引用,这会导致循环引用(因为模型需要了解实体).我不能将扩展方法放在模型项目中,因为那样的话,我必须在每个要使用实体的项目中都引用它,这违背了将实体拆分为自己的项目的整个目的.

However, if the extension method needs a context, the the entities project needs a reference to the model's project, which causes a circular reference (as the model needs to know about the entities). I can't put the extension method in the model project, as then I would have to reference that from every project that wanted to use the entities, which defeats the whole purpose of splitting off the entities into their own project.

有人有什么想法吗?抱歉,这是一个很长的问题,但是我想确保我清楚地解释了这个问题.

Anyone any ideas? Sorry this has been a bit of a long question, but I wanted to make sure I explained the problem clearly.

推荐答案

我们遇到的第一个问题是术语.缺少外键的数据库不是非关系的.它仍然是一个关系数据库,只是没有DRI(声明性参照完整性).

The first problem we have is one of terminology. A database that is lacking foreign keys is not non-relational. It's still a relational database, it just doesn't have DRI (declarative referential integrity).

我认为在数据可能丢失的情况下,您必须设计不带外键的数据库.但这不会阻止您在EF中定义这些实体之间的关系.我们一直在做.您没有指出您使用的是EDMX还是Code First.我们使用EDMX,过去我们所有实体都映射到视图.就EF而言,由于视图映射,我们的数据库中没有外键关系,因此我们将它们自己绘制出来,从而获得了实体之间的完全导航属性支持.

I think you've going to have to design the db without foreign keys in the cases where data might be missing. That won't prevent you from defining relationships between those entities in EF though. We do it all the time. You don't indicate whether you're using an EDMX or Code First. We use an EDMX and in the past all our entities were mapped to views. As far as EF is concerned there were no foreign key relationships in our database (because of the view mapping) so we just drew them in ourselves and we got full navigation property support between the entities.

这篇关于关系数据丢失时可以使用实体框架吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆