Thursday, April 14, 2011

ADO Entity Framework creating unwanted Entity Key

Hi, I need to use tables from a DB which I cannot alter (using linked server). So part of my schema is a view on these table and I cannot create an FK in my DB.

When I come to creating the association in ADO.NET Entity Framework I am getting problems because a second column on the table from the external DB has an index on it and the EF is creating an Entity Key for it (it's the name descr of the record - I think they just wanted to speed the ordering on it).

When I take the Entity Key off this column in the EF entity it complains that I need it because the underlying table has a key on it. If I leave it in I cannot map it onto anything in the table mapping of EF.

Does anyone know what I should do please?

From stackoverflow
  • I have a similar situation occurring to me.

    I have a situation where I have a table that has a pk made up of two fields. The first one is related to the pk of my primary table, and the second is an identifier.

    Example

    Table1 SupplierId pk

    Table2 SupplierId pk AddressType pk (a value like main, shipping, mailing, etc) Line1 line2 etc

    Because the AddressType is part of the pk, I am unable to do a number of things. In this scenario, I am trying to create inherited entities like MainAddress, ShippingAddress and MailingAddress.

    I have been unable to assign a condition to the table since AddressType is a key field.

    Anyone know a way around this?

  • You will have to edit the XML and remove the column from the key. Find the <EntityType> tag in the <edmx:StorageModels> section (SSDL content). Delete any <PropertyRef> in the <Key> that is not actually part of the primary key.

    Once you do this, you can set "Entity Key" on the corresponding scalar property in the designer to false, and EF won't get mad. You will also not be asked to map this column in associations anymore.

0 comments:

Post a Comment