0

Here is an interresting question, we have 2 classes (note that these are just nonsense to clarify my problem)

class Foo
{
    public int Id { get; set; }
}

class Bar
{
    public int Id { get; set;}
    public int FooId { get;set;}
}

Now using Code First in EF 4.1, i want the following: Foo has 0 or more Bars with cascade delete enabled. This is easily doable by making actual properties in the models (A collection of Bar in foo) but the models are also DTO's and therefore cant carry on extra properties on them (e.g. if you want all the Bar's for a given Foo, you need to ask the Service in a specific query).

Is there any way to accomplish this? Currently i have a replica of Foo and Bar special for the EF where my repository translates between the replica and the actual DTO.

I'm hoping there are better ways, if so, please teach me.

Thanks!

Polity
  • 14,734
  • 2
  • 40
  • 40

1 Answers1

0

As described here you always need navigation property on at least one side if you want to create FK realtion using code first approach. If your class is DTO you can mark it with attributes for serialization. For example when using WCF with DataContractSerializer you can use:

public class Bar
{
    public int Id { get; set; }
    public int FooId { get; set; }
    [IgnoreDataMember]
    public Foo Foo { get; set; }
}

If you are using entity as DTO there should be no problem with using additional attributes.

Community
  • 1
  • 1
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670