English 中文(简体)
ADO.NET Entity Framework Association of Entities by Value Range
原标题:

I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber.

I want to create a many to many association between these elements based on whether or not HouseNumber falls between StartHouseNumber and EndHouseNumber. However, I can only seem to get an equals operator in the GUI.

Is there a way I m missing to accomplish this association by range?

最佳回答

In theory you can do this see this post on creating an Association backed by a view.

But as the others have said it s probably not a good idea.

Alex

问题回答

So after considerably more reading on this topic, the answer is that you can t do this, but also that it is an undesired behavior to have.

Associations in the entity framework must be read-write. If I create the association I described, what would happen if I added one of the entities to the other entity s association collection? What value would it choose for HouseNumber to make it fall between the Start and End values?

The correct way to do this is either to write a helper function that takes an argument for HouseNumber, and place this on a hand-coded partial class of the second entity. You d also either write a read-only property to do the look up or a getter method. Alternatively, you could write a stored procedure in your database that can do this and import it into the entity.

You can t do this with an association, as far as I know, but you can do it with a query.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签