I'm using entity framework 4.1 and can't add the connection on linqpad, it says that my child entity doesn't have entity key defined. The message is: "Entity type "CashierPaymentType" has no key defined. Define the key for this entity type." I think this is happening when my child table has a composite key. Example:
Table: Cashier ID INT (Key) Name VARCHAR(100)
Table: PaymentType ID INT (Key) Name VARCHAR(100)
// I think this is the problem! Table: CashierPaymentType CashierID INT (key) (foreign key) PaymentTypeID INT (key) (foreign key) Price SMALLMONEY
I'm using linqpad 4.35.1
Any help ?
The problem is that you're telling LINQPad to new up your typed DbContext with a provider connection string instead of an Entity Framework connection string.
You'll get the same error in Visual Studio if you construct your typed DbContext with a provider connection string.
A provider connection string is valid only if you're doing code-first (in which case EF infers the model). In your case, the EDM is part of your project and is embedded in your assembly; therefore you must specify the full EF connection string as specified in your app.config so that EF can find it:
metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=TestDatabase;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"
Another solution is to use the following connection string, which defers to the app.config file:
name=TestDatabaseEntities
If you do this in LINQPad, make sure you tell it where the app.config file lives, in the textbox provided.
A more subtle point is that the T4 file for your DbContext is different to the default that VS creates, in that your DbContext is missing the following code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
If this override is present, it gives a more helpful error message if you specify the wrong kind of connection string.