1
votes

I have 2 tables, Products (my base) and OffShelfItems (by child)

I have a Foreign key setup on the ID field, here is a screenshot of this setup:

enter image description here

In my EDMX, I have imported the tables and have set OffShelfItem to be a member of the product class, here is a screen shot of that setup:

enter image description here

However, when I try to save an object with this test code:

OffShelfItem osi = new OffShelfItem();
            osi.WhenAdded = DateTime.Now;
            osi.LastModified = DateTime.Now;
            osi.IsDeleted = false;
            osi.Title = "TEST ITEM";
            osi.RetailPrice = 9.99M;
            osi.DealerPrice = 7.99M;
            ent.Products.Add(osi);
            ent.SaveChanges();

I get this error:

Schema specified is not valid. Errors: App_Code.Model.ssdl(75,6) : error 0113: Multiplicity is not valid in Role 'AA_OffShelfItems' in relationship 'FK_AA_OffShelfItems_AA_Products'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be 1.

I feel like I must be really close to getting this to work, I just need help getting there!

1
There is no navigation properties in the edmx, which usually means that the foreign key is somewhat not defined. Ive seen that error before when trying to add entities directly into the xml of the edmx, forgetting to set some connection properties. - thsorens
In this case there should be none, I want the OffShelfItems class to inherit from product, not be a child object of it - box86rowh

1 Answers

1
votes

id needs to be the Primary Key on the AA_OffShelfItems table.

Nice explanation here:

https://leftlobed.wordpress.com/2011/03/02/getting-to-know-entity-framework-table-per-type-tpt-inheritance/

[UPDATE:]

You can then access the OffShelfItems by extending your context as described in this answer:

EF Database First with TPT Inheritance only creates DbSet<T> for base clases

with something like:

context.Products.OfType<OffShelfItem>()

or:

partial class Context
{
    public DbSet<OffShelfItem> OffShelfItem{ get; set; }
}