Can Entity Framework be used when relational data is missing?

本秂侑毒 提交于 2020-01-06 07:00:12

问题


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.

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.

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.

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.

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).

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)

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.

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.


回答1:


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).

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.



来源:https://stackoverflow.com/questions/22358043/can-entity-framework-be-used-when-relational-data-is-missing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!