I am specifying my own database table names and schemas in the OnModelCreating() method in my strongly-typed DbContext as follows:
modelBuilder.Entity<MyAssociativeClass>().ToTable("MYASSOCIATIVECLASS", schemaName: "MYSCHEMA");
modelBuilder.Entity<MyAssociativeClass>()
.HasKey(x => new { x.ClassA.ID, ClassBID = x.ClassB.ID });
However, I get the following error:
The properties expression ‘x => new <>f__AnonymousType1`2(ID = x.MyClassA.ID, MyClassBID = x.MyClassB.ID)’ is not valid. The expression should represent a property: C#: ‘t => t.MyProperty’ VB.Net: ‘Function(t) t.MyProperty’. When specifying multiple properties use an anonymous type: C#: ‘t => new { t.MyProperty1, t.MyProperty2 }’ VB.Net: ‘Function(t) New From { t.MyProperty1, t.MyProperty2 }’.
As you can see, both MyClassA and MyClassB have an ID property. I am specifying a different name for MyClassB.ID in the anonymous type so there isn’t a conflict with using the same property name twice. BUt this still causes an issue. Why won’t it allow this?
I think you’re class MyAssociativeClass will have to expose the foreign keys as properties, such that you could then do: