EF Code First (4.3.1)

Nov 2, 2012 at 1:47 PM

Hi All,

following this 'step by step' example I've got stuck with the last point - "...Extend the domain service with Insert and Delete operations". After adding mentioned three methods to my Domain Service:

public IQueryable<ProductOrder GetProductOrders() {.. }
public void InsertProductOrder(ProductOrder ProductOrder) {...}
public void DeleteProductOrder(ProductOrder ProductOrder) {...}

I've got a build error:

"Parameter 'ProductOrder' of domain method 'InsertProductOrder' must be an entity type exposed by the DomainService. The entity type can be exposed either directly in a query operation, or indirectly through an association ..."

This is because ProductOrder class is empty and HAS NO PROPERTY WITH [Key] ATTRIBUTE (known problem - I'cant map this attribute by Fluent API '.HasKey(..)' but only by [Key] attribute). 

When I manually add two properties: 'OrderId' and 'ProductId' to the ProductOrder class (both with [Key] attriburtes): 

public partial class ProductOrder : LinkTable<Product, Order>
        public int ProductId { get; set; }

        public int OrderId { get; set; }

I've got another two build errors (one for _orderId and one for _productId):

 'Test.Services.Web.Models.Entities.ProductOrder' does not contain a definition for '_orderId' and no extension method '_orderId' accepting a first argument of type 'Test.Services.Web.Models.Entities.ProductOrder' could be found (are you missing a using directive or an assembly reference?) C:\Sources\Test\Test.Services\Generated_Code\Test.Services.Web.g.cs ...

Could anybody help me with this issue? 

Thanks in advance,




Nov 2, 2012 at 10:06 PM


Are you sure you added the link table views to your data model correctly and that you fluent metadata is also correctly configured?

You don't need to provide primary keys in your link table entities. These are provided in a generic way by the base class LinkTable.

If you think everything is set up correctly and it still doesn't work, perhaps you can share your code with me.

I  hope this helps.

Good luck,


Nov 6, 2012 at 8:35 PM

Hi Merijn,

Thank you for a quick response.

Yes, I'm sure that the link table was correctly applied (corresponding table with foreign keys exists in my DB (DB is created by EF Code First initializer).

I'm almost sure, that [Key] attribute is a 'key' problem, because I had to add this attribute to my other entities. Without [Key] attribute over Id property, the compilation showed an error (as above). Fluent API configuration ( .HasKey(...) ) didn't work in my case.

I'm using Windows 8, VS2012, EntityFramework 4.3.1 Code First.

I'll prepare a simple example soon.



Nov 9, 2012 at 9:03 PM

Hi Zbig,

I was referring to the link table _views_ not the link tables in your database. The link table views are a requirement by M2M4RIA and are described in the second step under Extend your data model with link table views. This will typically add a property 'Product_ProductOrderSet' to your product entity and 'Order_ProductOrderSet' to your Order entities. (these properties will not be stored in your database, that's why I call them 'views').

Can you verify that you added these views to your entities?

The Key annotations are added by the fluent configuration for M2M, so you don't need to add this by hand. I.e., a fluent configuration like this:

.Projection(x => x.OrderSet)
            .M2M(x => x.Product_ProductOrderSet, x => x.Order_ProductOrderSet, x => x.ProductSet);

Will automatically add the property Key annotations to your ProductOrder entity type (magic, right?)

You can also download the M2M4RIA code and have a look at the sample application that is included.

Otherwise, just send me your sample code and I will have a look at it.

Good luck,
Dec 1, 2012 at 5:09 PM

Hi Merijn,

Thank you for your next detailed explanation. Appreciate and apologise for such a long silence. The solution is OK now.

When I was preparing a simple solution to sent you an example, then everything 'magically' started working in a proper way.

After a short investigation I've found the problem which was slightly different than I've earlier described.

I've added unecessary entity set for my DbContext :

public DbSet<ProductOrder> ProductOrderSet { get; set; }

and during DB initialization by the initializer (CreateDatabaseIfNotExsists), the database creator showed an error message:

"... One or more validation errors were detected during model generation ... EntityType 'ProductOrder' has no key defined. Define the key for this EntityType ..."

After removing this line from DbContext, the db has been created sucessfuly (solution was build sucessfuly).

So, thanks once again for your time and valuable help.