Reputation: 7341
I have a Parent
and Child
class/table and I'm using Fluent API to configure my mapping. The tables are joined on non primary key fields in each table, so according to what I've read, I can't configure the join in Entity Framework. Because of that, I'm going to load my Parent.Children
property manually (parent.Children = (from x in context.Children...
)
However, I'm getting an exception on my mapping of Parent
. My classes look like
public class Parent
{
// Unique primary key
public int ParentPrimaryKey { get; set; }
// These two fields together compose the foreign key to join to Child
public string ParentForeignKey1 { get; set; }
public string ParentForeignKey2 { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
// Unique primary key
public int ChildPrimaryKey { get; set; }
// These two fields together compose the (non-unique) foreign key to join to Parent
public string ChildForeignKey1 { get; set; }
public string ChildForeignKey2 { get; set; }
}
When I try to query context.Parents
, I get an exception because the SQL that Entity Framework generates is trying to join Parent
to Child
and is looking for a property on Child
called Child_ParentPrimaryKey
.
If I add modelBuilder.Entity<Parent>().Ignore(p => p.Children);
I get the exception System.NotSupportedException: The specified type member 'Packages' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
How can I keep Children
as a non-mapped property on Parent
without getting errors?
Upvotes: 0
Views: 172
Reputation: 7341
Thanks to everyone who responded. Your comments made me realize the problem was that I was using the Parent.Children
property in my LINQ query. I removed that, and it's working now.
Upvotes: 0
Reputation: 9463
You can exclude a property from EF mapping by adding the [NotMapped]
attribute:
using System.ComponentModel.DataAnnotations.Schema;
public class Parent {
// ...
[NotMapped]
public List<Child> Children { get; set; }
}
DataAnnotations - NotMapped Attribute
Upvotes: 1