Okay, I've been staring at the screen for a couple of hours here and have no idea why I am getting this error. I've used Code First on a number of other projects and have had no problem with this before...
Here is the error:
System.InvalidOperationException was unhandled by user code
Message=The properties expression 'sci => sci.ShoppingCartItemId' 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 }'.
Source=EntityFramework
StackTrace:
at System.Data.Entity.ModelConfiguration.Utilities.ExpressionExtensions.GetSimplePropertyAccessList(LambdaExpression propertyAccessExpression)
at System.Data.Entity.ModelConfiguration.EntityTypeConfiguration`1.HasKey[TKey](Expression`1 keyExpression)
at BillingPlatform.DataLayer.BillingDb.OnModelCreating(DbModelBuilder modelBuilder) in [somepath]\BillingDb.cs:line 57
at System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
InnerException:
Here is the code that's throwing the error. The first line:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<ShoppingCartItem>().HasKey(sci => sci.ShoppingCartItemId);
modelBuilder.Entity<Product>().HasKey<Guid>(p => p.ProductId);
modelBuilder.Entity<DependentItemType>().HasKey<Guid>(dit => dit.DependentItemTypeId);
modelBuilder.Entity<ProductCategory>().HasKey<Guid>(pc => pc.ProductCategoryId);
base.OnModelCreating(modelBuilder);
}
Here is just the ShoppingCartItem class as a reference:
namespace BillingPlatform.Libraries
{
public class ShoppingCartItem
{
/// <summary>
/// The unique identifier of this shopping cart item.
/// </summary>
public Guid ShoppingCartItemId { get; set; }
public Product Product { get; set; }
public decimal Price { get; set; }
public decimal Tax { get; set; }
public Guid UserId { get; set; }
public bool InCart { get; set; }
public string ProductData { get; set; }
public DependentItemType DependentItemType { get; set; }
public string DependentItemId { get; set; }
}
}
Does anyone understand why Entity Framework would throw this error? My lambda expression:
modelBuilder.Entity<ShoppingCartItem>().HasKey(s => s.ShoppingCartItemId);
is super simple. I don't see what could be going wrong... Thank you for any help you can give!
Okay the problem was that my class members were originally just fields. Code First expects properties. After making the code change and rebuilding, I was still getting the same error. But once Visual Studio was forced to push the updated DLLs, everything worked fine.
Not the case here (landed here from a Google search), but might be worth posting that this exact exception can also occur if HasKey
uses an anonymous type that defines its own field names:
HasKey(t => new { KeyField1 = t.KeyField1, KeyField2 = t.KeyField2 });
Should be fixed to look like this:
HasKey(t => new { t.KeyField1, t.KeyField2 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With