Note that there are some explanatory texts on larger screens.

plurals
  1. POOne-To-One relationship with fluent api. A Hacky way?
    primarykey
    data
    text
    <p>EF 4.3.1. I have defined <code>User</code> and <code>Box</code> entities. Each box may or may not be assigned to a user.<br> What I'd like to achieve is to have a <code>OwnBox</code> property in <code>User</code> class, and an <code>Owner</code> property in <code>Box</code> class.<br> in Database, I have defined <code>OwnerId</code> foreignkey in <code>Boxes</code> (<code>Boxes.OwnerId</code> has relation with <code>Users.UserId</code>).<br> To define the relationship with fluent api, I have defined the following classes: </p> <pre><code>public partial class User { public int UserId {get; set;} public virtual Box OwnBox { get; set; } } public partial class Box { public int? OwnerId { get; set; } public virtual User User { get; set; } } </code></pre> <p>Then in my Mapping class for <code>Box</code>, I have defined the relations as follows: </p> <pre><code>this.HasOptional(t =&gt; t.User).WithOptionalDependent(d =&gt; d.OwnBox). Map(m =&gt; m.MapKey("OwnerId")).WillCascadeOnDelete(true); </code></pre> <p>But by firing up the project, I got the error: </p> <blockquote> <p>Schema specified is not valid. Errors: (56,6) : error 0019: Each property name in a type must be unique. Property name 'OwnerId' was already defined.</p> </blockquote> <p>So I had to tell EF to forget about the <code>OwnerId</code> column first: </p> <pre><code>this.Ignore(t =&gt; t.OwnerId); </code></pre> <p>Now the project works fine. But I'm still doubtful if this is a good approach and will everything work fine on CRUD operations with foreign key associations. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload