2

I have 2 tables in database: ReceivedGoods and ReceivedGoodsProperties ReceivedGoods contains ReceivingId as PK and must have its extending data in ReceivedGoodsProperties which contains ReceivingId as FK referencing to the ReceivedGoods's ReceivingId. Current ReceivedGoodsProperties, however, has its own PK Id and is therefore distinct from FK. So I have following:

public class ReceivedGoods
{
    ...
    public int ReceivingId { get; set; }
    ...
    public virtual ReceivedGoodsProperties properties { get; set; }
}

public class ReceivedGoodsProperties
{
    ...
    public int Id { get; set; } // This is PK
    public int ReceivingId { get; set; } // This is FK
    ...
    public virtual ReceivedGoods goods { get; set; }
}

I would like to get ReceivedGoods object and have properties automatically loaded as well but I am not able to figure out, how to set up this within EF. I've tried something like this (from the ReceivedGoodsProperties side mapping):

this.HasRequired(p => p.goods)
    .WithRequiredDependent(d => d.properties)
    .Map(m => m.MapKey("ReceivingId"));

but I am ending up with following error:

ReceivingId: Name: Each property name in a type must be unique. Property 
name 'ReceivingId' is already defined.

When commenting out ReceivingId in ReceivedGoodsProperties, upper exception is not thrown, ReceivedGoods is loaded correctly except the properties property.

Can somebody explain me, how to do one-to-one mapping in situation like this?

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
esgaldir
  • 823
  • 9
  • 11
  • 2
    Personally I'd make the FK the PK. Is there a reason you need a separate PK? The only reason I can think of is if you wanted a one to zero or one relationship, but then your FK would need to be nullable. – juharr Jul 18 '18 at 16:48
  • Unfortunately I got database designed already. One to zero or one is not my case, but I'm not sure if changing PK right now would be possible. – esgaldir Jul 18 '18 at 17:05

2 Answers2

0

Could you try:

public class ReceivedGoods
{
    ...
    public int ReceivingId { get; set; }
    ...
    public virtual ReceivedGoodsProperties properties { get; set; }
}

public class ReceivedGoodsProperties
{
    ...
    public int Id { get; set; } // This is PK
    [ForeignKey( "goods " )]
    public int ReceivingId { get; set; } // This is FK
    ...
    [Required]
    public virtual ReceivedGoods goods { get; set; }
}

BTW, in C# the standard guidelines is to PascalCase members, so Goods and Properties

Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118
0

Try defining the relationship this way:

this.HasRequired(p => p.goods)
    .WithRequiredDependent(p => p.properties)
    .HasForeignKey(p => p.ReceivingId);

If you follow the standard EF naming conventions, it can usually figure out these relationships on its own. You only really run in to trouble when your navigation property names don't correspond to the class name, or if you have multiple FKs to the same destination in the source table.

If you want the navigation properties to get filled out "automatically", use the Include extension method on the query, as in:context.Goods.Include(g=>g.properties). You don't have to declare them as virtual unless you want to make use of lazy loading.

You may need to come at this from the other entity:

this.HasRequired(p => p.properties)
    .WithRequiredPrincipal(p => p.goods)
    .HasForeignKey(p => p.ReceivingId);
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • 1
    Not marking them as `virtual` does not mean it will eager load them, it just means it will never lazy load them as the proxy won't be able to override it. But if you want to eager load you will still have to `.Include()` – Michal Ciechan Jul 18 '18 at 16:38
  • Unfortunately, HasForeignKey() is not available in proposed chaining. Only map() and WillCascadeOnDelete() will popup. – esgaldir Jul 18 '18 at 17:02
  • Hmm.. Try configuring it from the other entity, and using `WithRequiredPrincipal` instead; that one should give you the `HasForeignKey` option. – Bradley Uffner Jul 18 '18 at 17:06
  • @BradleyUffner Still no luck... just trying to configure it from the side of ReceivedGoods as you suggested but HasForeignKey() is not available. – esgaldir Jul 18 '18 at 17:12