Why do Code First classes need navigation properties?

I have some domain classes that look something like what I want to model using Code First (in EF 4.3).

public class Foo {
    // ...
}

public class Bar {
    // ...

    public Foo Foo { get; set; }
}

public class Baz {
    // ...

    public Foo Foo { get; set; }
}

In each example , I see links to external objects being added to the class Foo. Can a class Foobe an agnostic of a class Barand Baz, or do I really need to do something like this?

public class Foo {
    // ...
    public virtual Bar { get; set; }

    public virtual Baz { get; set; }
}

According to this answer, classes should have navigation properties. I'm new to Code First, so can anyone explain why this could be so? Is there a way to avoid getting my class infected Foolike this using the Fluent API?

, Foo , . - ?

+5
2

.

, -forst , , , .

, , , /sql. sql-.

, - . - ?

Foo - , , , .

+1

" ". " " EF . , FK - PK. , .

, :

modelBuilder.Entity<Bar>()
            .HasRequired(b => b.Foo)
            .WithOptional();

modelBuilder.Entity<Baz>()
            .HasRequired(b => b.Foo)
            .WithOptional();
+2

All Articles