Search code examples
entity-framework-4mappingforeign-keyscode-firstentity-framework-ctp5

EF4 CTP5 - Map foreign key without object references?


I feel like this should have a simple answer, but I can't find it.

I have 2 POCOs:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public int CategoryId { get; set; }
}

Notice that there are no object references on either POCO. With Code-First, how do I make EF4 CTP5 define a relationship between the two database tables?

(I know this is an unusual scenario, but I am exploring what's possible and what's not with Code-First)


Solution

  • No, this is not possible. As you can see below, all of the fluent API methods for setting up associations require specifying the Navigation Property as their parameter.

    HasMany<TTargetEntity>(Expression<Func<TEntityType, ICollection<TTargetEntity>>> navigationPropertyExpression) 
    
    HasOptional<TTargetEntity>(Expression<Func<TEntityType, TTargetEntity>> navigationPropertyExpression) 
    
    HasRequired<TTargetEntity>(Expression<Func<TEntityType, TTargetEntity>> navigationPropertyExpression)