Domanda

I have one table that has a Code and a Type property. I have another table which has a foo_id property. The foo_id is a Code of Type == foo so when creating a constraint between these two tables I need to match Code to foo_id and Type to the constant Foo.

Is there a way to do this? I don't want to add an Type column to my second table that is going to have the same value for every single row because that seems like a waste.

Table 1                    Table 2
Code     <---------------  Foo_id                   Foo_id maps to Code
Type     <---"Foo"                                  But Table 2 doesn't have a property 
                                                    that maps to Type. But it should be 
                                                    constant 

I'm creating an association between Table 1 and Table 2 in my .edmx file. When I click on that association and then click on the Referential Constraint, I can see Code and Type for the principle key, but I only have foo_id I can use for the Dependent Property. So I want to specify that the constraint for Type should be a constant.

There are, of course, other values for Type other than Foo, but Table 2 in particular is only concerned with Foo types.

I can work around it by just doing something like:

var x = from i in Table2
        select new { someT2Prop = i.Table2Prop,
                     someT1Prop = (from r in Table1 where r.Code == i.Foo_id 
                                   && r.Type == "Foo" select r.Table1Prop).FirstOrDefault() };

But that's kind of messy. I'd like to just have a navigation property from Table 2 to Table 1, so I could do something like this:

var x = from i in Table2
        select new { someT2Prop = i.Table2Prop,
                     someT1Prop = i.Table1.Table1Prop };
È stato utile?

Soluzione

If I understand your question then:

Create a view:

SELECT Code 
FROM [Table 1]
WHERE Type = "Foo"

Then constrain on Code values from this view.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top