My models:
class FooEntity
{
[Key]
int Id { get; set; }
[ForeignKey("Bar"), Column("other_id")]
int OtherId { get; set; } // <-- This should be the FK
virtual BarEntity Bar { get; set; }
}
class BarEntity
{
[Key]
int Id { get; set; }
[Column("other_id")]
int OtherId { get; set; } // <-- This is the other side of the FK
}
Essentially I want to reproduce this SQL:
select *
from foo f
left join bar b on b.other_id = f.other_id -- AND other conditions to "guarantee" uniqueness
But with the model building configuration:
modelBuilder.Entity<FooEntity>()
.HasOptional(f => f.Bar)
.WithRequired()
.Map(m => m.MapKey("other_id"))
.WillCascadeOnDelete(false);
I end up with the error: "Each property name in a type must be unique. Property name 'other_id' is already defined." But when I add:
modelBuilder.Entity<BarEntity>().Ignore(b => b.OtherId);
before the other configuration, I get the error: "the specified type member 'OtherId' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported."
So how can I get this working? Changing the underlying data structure is absolutely not an option.
I recommend you omit the FK relationship and when you need to retrieve both entities manually include the join in your Linq/Lambda statement.
There's no shortcut for what you want to accomplish; you will need to create the query manually, something along the lines of:
dbContext.Foos
.GroupJoin(
dbContext.Bars,
f => f.OtherId,
b => b.OtherId,
( foo, bars ) => new { foo, bars } )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.